#3413

Maximum Coins From K Consecutive Bags

medium · 24.5% accepted · 198 likes · top 3%

array · binary search · greedy · sliding window · sorting · prefix sum

Description

There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins.

You are given a 2D array coins, where coins[i] = [li, ri, ci] denotes that every bag from li to ri contains ci coins.

The segments that coins contain are non-overlapping.

You are also given an integer k.

Return the maximum amount of coins you can obtain by collecting k consecutive bags.

Solution