Change Over k Days
prices[i] is the price of a ticket on day i. For every day that has a day k days before it in the list, work out how much the price changed in between: the price on that day minus the price k days earlier.
Return these changes in the order of the days, starting with day k. If the list has k days or fewer, there is nothing to compare, so return an empty list.
Example 1
prices [10,12,9,15]
k 1
returns [2,-3,6]
Each day is compared with the day before: 12 - 10 = 2, 9 - 12 = -3 and 15 - 9 = 6.
Example 2
prices [10,12,9,15]
k 2
returns [-1,3]
With k = 2, day 2 is compared with day 0 and day 3 with day 1: 9 - 10 = -1 and 15 - 12 = 3.
Example 3
prices [5,8]
k 3
returns []
Two days are not enough to look 3 days back.
Constraints
0 <= len(prices) <= 301 <= prices[i] <= 5001 <= k <= 30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Common StartBeginner
- Fixes to Make a MirrorBeginner
- Flip a StretchBeginner
- Fold and AddBeginner
- Lighten the ShelfBeginner
- Peel Matching EndsBeginner
- Sums of Every WindowBeginner
- Trim the ZerosBeginner