Show problems as
Classic problem #2140MediumArrayDynamic Programming

Solving Questions With Brainpower

Real-world version: Choosing Which Tasks to Take
Tracked in this browser only
Read Solving Questions With Brainpower on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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