Online store~10 min2s · 256 MBtarget O(n) time, O(1) spaceclassic twin: lc-1672traversal
An online store works with many warehouses, its own and those of partners. Each warehouse reports how many orders it packed on each day of the last week.
Your team lead says: "Tell me which warehouse was busiest over the week, so we can plan the staff for next week."
The report lists the warehouses in a fixed order. When two warehouses packed the same total, the one listed first is the answer.
Write busiest_warehouse(orders).
orders[w] holds 7 numbers: the orders warehouse w packed on each day of the week.w of the warehouse with the largest weekly total.Input: orders = [[5, 3, 0, 2, 9, 1, 4], [7, 7, 7, 0, 0, 0, 0], [2, 2, 2, 2, 2, 2, 2]]
Output: 0Each row is a warehouse. The weekly total is the sum of its row.
| Warehouse | Daily orders | Weekly total |
|---|---|---|
| 0 | [5, 3, 0, 2, 9, 1, 4] | 24 |
| 1 | [7, 7, 7, 0, 0, 0, 0] | 21 |
| 2 | [2, 2, 2, 2, 2, 2, 2] | 14 |
Warehouse 0 has the largest total, 24.
Input: orders = [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0]]
Output: 0Both warehouses packed 1 order. The smaller index wins.
Input: orders = [[0, 0, 0, 0, 0, 0, 0]]
Output: 0One warehouse with no orders is still the busiest.
1 ≤ orders.length ≤ 100,000 (warehouses)orders[w].length = 7 (days of the week)0 ≤ orders[w][d] ≤ 1,000 (orders packed in one day)Comparing every warehouse with every other, and adding the weeks again each time, makes about 7 × 10,000,000,000 additions. Adding each week once makes 7 × 100,000. A weekly total is at most 7 × 1,000 = 7,000, so an int is enough.