Peak So Far
readings lists sensor readings in the order they were taken. Build a new list of the same length where item i is the highest reading among readings[0] through readings[i], the current reading included.
Return that list. An empty readings gives an empty list. Do not change readings.
Example 1
readings [3,1,4,1,5]
returns [3,3,4,4,5]
The highest reading seen so far is 3, then still 3, then 4 (the 4 beats it), still 4, then 5.
Example 2
readings [-2,-5,-1]
returns [-2,-2,-1]
Every reading is negative; the record only moves when a reading beats the previous record, so -1 replaces -2 at the end.
Constraints
0 <= len(readings) <= 100-1000 <= readings[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Running Sum of an ArrayBeginner
- Second AppearanceBeginner
- Shift Right and FillBeginner
- Shuffle the ArrayBeginner
- Sort by ParityBeginner
- Trend SignsBeginner
- Alternate the SignsEasy
- Drop the Lowest ScoresEasy