You are given n cars on an infinitely long road, numbered from 0 to n-1 from left to right, each at a unique position. You are also given a string directions of length n where each character represents the state of the car at that index:
All moving cars travel at the same speed. The total number of collisions can be calculated as follows:
After a collision, the cars involved become stationary at the collision point. Cars cannot change direction or state otherwise.
Return the total number of collisions that will happen on the road.
Step 1: Identify Collision Scenarios There are three cases where collisions can happen: Opposite direction collision: A car moving right ('R') collides with a car moving left ('L'), contributing 2 to the collision count. Right-moving car hitting a stationary car ('S'): A moving car 'R' collides with 'S', contributing 1. Left-moving car hitting a stationary car ('S'): A moving car 'L' collides with 'S', contributing 1. After a collision, the cars involved stop moving.
Step 2: Ignore Non-Colliding Cars at Extremes Any cars moving left ('L') at the start will never collide. Any cars moving right ('R') at the end will never collide. Trim these non-colliding cars from consideration.
Step 3: Iterate Through the String and Count Collisions Traverse through the filtered string and check where 'R' and 'L' meet. Track stationary cars ('S') that get hit. Keep a counter to track total collisions.
Step 4: Use a Single Pass and Stack Approach (Optimized) Use a stack or variables to track right-moving cars ('R'). When an 'L' car is encountered, pop from the stack and count collisions.
Step 5: Implement the Function Efficiently Use a loop to process the collisions in a single pass (O(n) time complexity). Return the total number of collisions.
A wiggle sequence is defined as a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if it exists) may be either positive or negative.
A subsequence of a sequence is obtained by deleting some (possibly zero) elements while keeping the remaining elements in their original order.
Given an integer array nums, return the length of the longest wiggle subsequence of nums.
Step 1: Understand the Wiggle Property A wiggle sequence requires the differences between consecutive elements to strictly alternate between positive and negative. For each pair of adjacent elements nums[i] and nums[i+1], we calculate: Positive difference: nums[i+1] - nums[i] > 0 Negative difference: nums[i+1] - nums[i] < 0 Zero difference: Does not contribute to a wiggle sequence
Step 2: Identify the Core Concept (Greedy Approach) Instead of checking all subsequences (O(2^n) time complexity), we use a greedy approach. We count "peaks" and "valleys" because each peak and valley contributes to the wiggle length.
Step 3: Initialize Variables for Tracking Peaks and Valleys Use prevDiff = 0 to track the previous difference. Use count = 1 because a single element is always a valid subsequence. Iterate through the array while checking for changes in sign.
Step 4: Traverse the Array and Count Wiggle Points Calculate diff = nums[i] - nums[i-1]. If diff > 0 and prevDiff <= 0, increase count. If diff < 0 and prevDiff >= 0, increase count. Update prevDiff = diff. After iterating through the array, return the final wiggle subsequence length

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What is recursion?