← 返回 amazon 的题目列表Max Money from K Consecutive Bags
类型:qbank
OA algorithm problem: bags are laid on a line by range segments, each segment filling its positions with a fixed amount. Take any window of k consecutive positions and maximize the total money.
Requirements
k: number of consecutive bag positions you take.
segment[n][3]: each [start, end, money] means every position in [start, end] (end inclusive) holds money. Positions not covered by any segment hold 0.
Lay the bags out on a number line, take any window of k consecutive positions, and return the maximum total money.
Examples
k = 5, segments = [[1,4,2], [6,6,5], [7,7,7], [9,10,1]]
Positions: 1->2, 2->2, 3->2, 4->2, 5->0, 6->5, 7->7, 8->0, 9->1, 10->1
Best window = positions 3..7 -> 2 + 2 + 0 + 5 + 7 = 16
Answer: 16
Notes
Materializing the full position array and running a fixed-size sliding-window sum is the straightforward solution when coordinates are small.
If start / end can be large (a sparse line), do not expand the array — compress coordinates or sweep segment boundaries and slide the k-width window across event points so a large end does not blow up memory.
Clarify whether segments can overlap and, if so, whether overlapping money stacks; the worked example has no overlap.
Preparation
Drill fixed-window maximum-sum until automatic.
Practice the coordinate-compression variant where positions are sparse.