oneby7

1-D Dynamic Programming Take it or skip it

524. Training Schedule with No Repeated Activity

Medium

A training plan runs for n days. On each day you do exactly one of three activities, numbered 0, 1 and 2. Doing activity j on day i earns points[i][j] points.

You may not do the same activity on two days in a row. An activity may come back after a day of something else.

Return the most points a plan can earn over all n days.

Example 1

Input: points = [[4, 7, 2], [3, 9, 5], [8, 1, 6], [2, 5, 3]]

Output: 26

Explanation: Do activities 0, 1, 0, 1 for 4 + 9 + 8 + 5 = 26. Taking the best activity of each day in turn gives only 7 + 5 + 8 + 5 = 25, because the 7 on day 0 blocks the 9 on day 1.

Example 2

Input: points = [[0, 6, 0], [0, 6, 0], [0, 6, 0]]

Output: 12

Explanation: Activity 1 is the only one worth anything, but it cannot fill two days in a row. Doing it on days 0 and 2 gives 12.

Example 3

Input: points = [[1000000000, 0, 1000000000], [1000000000, 1000000000, 0], [0, 1000000000, 1000000000]]

Output: 3000000000

Explanation: Doing activities 0, 1 and 2 earns 109 on every day.

Check your understanding

Work out the answer for this input before you write any code.

points = [[5, 3, 1], [6, 2, 1], [4, 9, 0]]

Constraints

  • 1 ≤ points.length ≤ 105
  • points[i].length == 3
  • 0 ≤ points[i][j] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.