All problems
1052MediumArrayDynamic Programming

Choosing Which Tasks to Take

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2140Solving Questions With Brainpower

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A shift offers tasks in a fixed order, given as tasks, where tasks[i] = [points, rest].

Work through the tasks from the first to the last. At each task you may either take it, earning its points and then skipping the next rest tasks entirely, or leave it and move straight on to the next.

Return the most points the shift can earn.

Examples

Example 1

Input
tasks = [[1, 5], [9, 1], [1, 1]]
Output
9

Leaving the first task and taking the second earns nine, which beats taking the first for one and then being skipped past everything else.

Example 2

Input
tasks = [[5, 1], [5, 1], [5, 1]]
Output
10

Each task skips the one after it, so the first and the third can both be taken, earning ten.

Example 3

Input
tasks = [[10, 1]]
Output
10

One task, so take it: the rest period runs off the end of the shift and costs nothing.

Constraints

  • 1 <= tasks.length <= 10^5
  • tasks[i].length == 2
  • 1 <= tasks[i][0] <= 10^5
  • 1 <= tasks[i][1] <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def most_points(tasks: list[list[int]]) -> int:
Java
public long mostPoints(int[][] tasks)
September 7
Apply