You are given a 0-indexed string directions of length n. Each character in directions[i] can be:
All moving cars travel at the same speed. Collisions are counted as follows:
After a collision, the cars involved stop at the collision point and cannot move further. Cars cannot change their direction or state otherwise.
Task: Return the total number of collisions that will occur on the road.
Step 1: Read the input string.
Step 2: Initialize variables. For example collisions: to keep track of the total number of collisions. moveRight: to count consecutive R cars moving right. stationaryFound: a boolean flag to check if any stationary car (S) is encountered.
Step 3: Iterate through each car direction. Used a for loop to iterate through the directions string.
Step 4: Handle R cars (moving right). If the car is R, increment moveRight to track right-moving cars.
Step 5: Handle L cars (moving left). If a L car is found: If there were previous R cars, they collide. Add moveRight + 1 collisions (current L + all previous R cars). Reset moveRight to 0. If a stationary car was previously found, add 1 collision (L hits S).
Step 6: Handle S cars (stationary). Add all previous R cars as collisions. Reset moveRight to 0. Set stationaryFound to true to track future L cars colliding with S.
Step 7: Print the result.Display the total number of collisions using System.out.print(collisions).
A wiggle sequence is a sequence of numbers 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 sequence with one element or two non-equal elements is trivially a wiggle sequence.
Examples:
Task: Given an integer array, determine the length of the longest wiggle subsequence.
Step 1: Import necessary libraries: java.util.*.
Step 2: Define the wiggleMaxLength function to calculate the longest wiggle subsequence.
Step 3: Handle edge cases: return the size if the list has fewer than 2 elements.
Step 4: Initialize variables: prevDiff = 0 and count = 1.
Step 5: Iterate through the list, calculating the difference between consecutive numbers.
Step 6: Check for alternating signs: If positive follows negative or vice versa, increment count. Update prevDiff with the current difference.
Step 7: Return the final count of the longest wiggle subsequence.

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
Providing input/output examples in your prompt is a technique called: