Loading...
Loading...
Loading...
A dashboard shows the total value of the latest k orders. After each new order arrives, print the sum of the last k orders (or of all orders so far, if fewer than k have arrived).
Example
Input
5 3 10 20 30 40 50
Output
10 30 60 90 120
Sums: 10, 30, 60, then 20+30+40 = 90 and 30+40+50 = 120.
Try the problem first. Open one hint at a time.
Keep the last k values in a queue and a running sum.
On each new value: offer it and add it to the sum. If the queue is bigger than k, poll the oldest value and subtract it.
Keep the sum in a long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor