Maximum Money in K Consecutive Bags

Instructions

An infinite row of bags is numbered 1, 2, 3, .... You are given non-overlapping segments segments, where segments[i] = [l, r, m] means every bag from l to r holds m money. All other bags hold 0.

Return the maximum total money in any k consecutive bags.

Example 1:

Input:  k = 5, segments = [[1,4,2],[6,6,5],[7,7,7],[9,10,1]]
Output: 16

Explanation: Bags 3 to 7 hold 2 + 2 + 0 + 5 + 7 = 16.

Example 2:

Input:  k = 3, segments = [[11,11,5],[1,10,1]]
Output: 7

Explanation: Bags 9 to 11 hold 1 + 1 + 5 = 7.

Constraints:

  • 1 <= segments.length <= 2 * 10^5
  • 1 <= k <= 10^9
  • 1 <= l <= r <= 10^9
  • 1 <= m <= 10^6
  • Segments do not overlap and may be in any order.

Function Signature

Online Judge

Loading editor...
Result will appear here after submission.