362. Maximum Items from Consecutive Shelves
Maximum Items from Consecutive Shelves
There is an unlimited row of shelves numbered with positive integers. Most shelves are empty, while some non-overlapping shelf ranges contain items.
Each range is represented by the string "start,end,items". Every shelf from start to end, inclusive, contains exactly items items.
Select exactly k consecutive shelves and return the maximum total number of items they can contain.

Maximum Items

Implement the following method:
long maximumItems(List<String> ranges, int k)
Return the maximum number of items that can be collected from exactly k consecutive shelves.

Range Format

Every element of ranges has the following format:
"start,end,items"
Here, start and end are the first and last shelves in the range. The value items is the number of items stored on each shelf in that range.

Behavior Requirements

  • Every shelf not covered by a given range contains zero items.
  • The selected shelves must form one continuous range of exactly k shelf positions.
  • The given shelf ranges never overlap.
  • Return the maximum possible total as a long.

Constraints

  • 1 ≤ ranges.size() ≤ 100,000
  • Each element of ranges contains exactly three comma-separated integers.
  • 1 ≤ start ≤ end ≤ 1,000,000,000
  • 1 ≤ items ≤ 1,000,000,000
  • 1 ≤ k ≤ 1,000,000,000
  • The ranges do not overlap.

Examples

Example 1

maximumItems(ranges = ["1,2,4", "5,7,3", "9,9,8"], k = 4)
Output: 14
Selecting shelves 6 through 9 collects 3 + 3 + 0 + 8 = 14 items.

Example 2

maximumItems(ranges = ["4,6,7"], k = 2)
Output: 14
Any two consecutive shelves fully inside the given range contain 7 + 7 = 14 items.

Example 3

maximumItems(ranges = ["3,3,10", "8,10,4"], k = 3)
Output: 12
Selecting shelves 8 through 10 collects 4 + 4 + 4 = 12 items.


Please use Laptop/Desktop or any other large screen to add/edit code.