← 返回 amazon 的题目列表Max Money From k Consecutive Bags With Piecewise-Constant Segments
类型:online_judge
Problem: Maximum Money From k Consecutive Bags (Piecewise-Constant Segments)
You are given an integer k, meaning you must take k consecutive bags.
You are also given a 2D array segments[n][3], where each entry is:
segments[i][0] = start: segment start index
segments[i][1] = end: segment end index (inclusive)
segments[i][2] = money: money value for every bag in this index range
Interpretation: for any index idx, if it lies within a segment [start, end], the bag value is money. If an index is not covered by any segment, its value is 0.
Return the maximum total money obtainable by choosing a consecutive window of length k, i.e., some [x, x+k-1].
Example
k = 5
segments = [[1,4,2], [6,6,5], [7,7,7], [9,10,1]]
Taking indices 3..7 yields 2+2+0+5+7 = 16, so return 16.
What to implement
Return an integer: the maximum achievable sum.
Sample test cases
Input: k=5, segments=[[1,4,2],[6,6,5],[7,7,7],[9,10,1]] Output: 16
Input: k=3, segments=[[0,2,1]] Output: 3
Input: k=2, segments=[[5,5,10]] Output: 10
Input: k=4, segments=[[1,1,5],[3,3,6]] Output: 11
Input: k=1, segments=[[2,4,7]] Output: 7
Example
Input
5
4
1 4 2
6 6 5
7 7 7
9 10 1
Output
16