In Amazon's financial team, an analyst is dealing with an infinite number of bags arranged in a line, each numbered from 1 to infinity. The task is to gather information about the amount of money in these bags, presented in the form of continuous segments. The objective is to select consecutive bags in such a way that the total amount of money in these bags is maximized.
The continuous segments provided to represent the amount of money in each bag do not intersect. Additionally, any bag included within a segment contains some amount of money, while bags not included in any segment are considered to have zero money.
Formally, given a 2D array segment
of size n x 3
, where n
represents the number of available segments, and an integer k
representing the number of consecutive bags you will take, and the 2D array segment
represents the range of bags included in this segment [segment[0], segment[1]]
and the amount of money inside the bags in the range segment[2]
.
Find the k
consecutive bags with the maximum total amount of money, since the answer can be large, return it modulo (10^9 + 7)
.
Function Description
Complete the function maxTotalAmount
in the editor.
maxTotalAmount
has the following parameters:
- 1.
int[][] segment
: a 2D array where each element is an array of three integers representing the range of bags and the amount of money - 2.
int k
: the number of consecutive bags
Returns
int
: the maximum total amount of money in k
consecutive bags, modulo (10^9 + 7)
βΈ(ο½‘Λ α΅ Λ)βΈβ‘ Feeling so lucky to have spike's help~~
Example 1:
Input: segment = [[1, 4, 2], [6, 6, 5], [7, 7, 7], [9, 10, 1]], k = 5
Output: 16
Explanation:The subsegment starting from the third bag and ending at the seventh bag has the maximum total amount of money, hence the answer is 16.
ππ

input:
output: