#C2521. Counting Waves
Counting Waves
Counting Waves
You are given an array of n integers representing the heights of people standing in a line. Your task is to determine the minimum number of waves needed such that each wave is a contiguous subarray. In each wave, the first person must be taller than or equal to all the people who follow in that wave.
The wave segmentation works as follows:
- If the current person is taller than or equal to the maximum height encountered so far in the current wave, a new wave begins.
- Otherwise, the current person continues the current wave, and the maximum height for that wave is updated to the current person’s height.
For example:
Input: 5
Array: 3 1 2 3 1
Output: 3
Explanation: The waves are split as [3, 1], [2, 3], [1].
This problem tests your ability to perform basic array manipulations and greedy segmentation based on conditions.
inputFormat
The input is given from stdin as follows:
- An integer n representing the number of people.
- A line containing n space-separated integers representing the heights.
outputFormat
Output a single integer to stdout denoting the minimum number of waves required.
## sample5
3 1 2 3 1
3