← back to unit
Medium
# breaking it down medium
The Running Total Trick
The shop asks: how much did we earn from day 3 to day 7? And day 1 to 4? And… stop re-adding, there's a trick! 🧠
You get daily sales, then queries like 3-7 (1-based, inclusive). First build a running-total list where entry i is the sum of the first i days. Then every query is ONE subtraction. Print each answer on its own line.
Example:
Input: sales = [5, 3, 8, 2, 7], queries = ["2-4", "1-5"]
Output:
13
25
The running totals are 5, 8, 16, 18, 25. Query 2-4 is 18 - 5 = 13 (days 2 through 4: 3+8+2), and query 1-5 is the whole total, 25.
💡 need a hint?
brk-running-total-trick.py🔒 given lines are locked — write your code in between
loading...