Interview Questions/Coding/Warehouse Restock Peak

Warehouse Restock Peak

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Warehouse Restock Peak

A fulfillment warehouse has $N$ shelves standing in a single row, numbered $1$ through $N$. At the start of the day every shelf is completely empty, holding a stock of $0$ units. During the day, $Q$ restock trucks arrive one after another. The $j$-th truck carries a delivery slip with three numbers $l_j$, $r_j$, and $x_j$: its crew walks down the row and adds $x_j$ units to every shelf from shelf $l_j$ to shelf $r_j$ inclusive. Deliveries overlap freely - a popular aisle may be restocked by many trucks. The warehouse manager needs to order safety railings for the most heavily loaded position. After all $Q$ deliveries are done, report the maximum stock held by any single shelf. ### Function Description Complete the function `maxStockLevel` provided in the editor. The function receives the following parameters:

ParameterTypeDescription
$N$integerthe number of shelves
$Q$integerthe number of restock operations
$ops$array of $Q$ triples of integers$ops_j = [l_j, r_j, x_j]$ means every shelf from $l_j$ to $r_j$ inclusive gains $x_j$ units

The function must return a single integer: the maximum stock on any shelf after all $Q$ operations are applied. ### Input Format - The first line contains two space-separated integers $N$ and $Q$. - Each of the next $Q$ lines contains three space-separated integers $l_j$, $r_j$, and $x_j$ describing one restock operation. ### Output Format Return a single integer: the maximum stock on any shelf after all operations. ### Notes - Shelves are $1$-indexed and both endpoints $l_j$ and $r_j$ are included in the restock. - Operations accumulate: the final stock of a shelf is the sum of $x_j$ over every operation whose range covers it. - The answer can be as large as $2 \times 10^9$, which exceeds a signed 32-bit integer - use a 64-bit type where applicable.

Examples

Example 1
Input:
```text
5 3
1 3 2
2 5 3
2 2 4
```
Output:
```text

Explanation: Start with stocks $[0, 0, 0, 0, 0]$. The first truck adds $2$ to shelves $1..3$: $[2, 2, 2, 0, 0]$. The second adds $3$ to shelves $2..5$: $[2, 5, 5, 3, 3]$. The third adds $4$ only to shelf $2$: $[2, 9, 5, 3, 3]$. The heaviest shelf is shelf $2$ with $2 + 3 + 4 = 9$ units.

Approach hint

Applying each delivery shelf-by-shelf touches up to $N$ shelves per operation - with $Q$ operations that is far too slow at the largest sizes. Think about what you actually need: only the final totals.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

Loading...
Input
```text 5 3 1 3 2 2 5 3 2 2 4 ```
Output
```text