PracticeDynamic Programming

Maximize Combo Segments

MediumDynamic Programmingtraversalstate trackingmin/max tracking Time not estimated Not started

Context

In the arcade game Neon Dash, a combo meter has a target number of points to fill. Each legal move adds one of the permitted positive point amounts, and moves may be repeated. Determine the largest number of moves that fills the meter exactly. If no sequence reaches the target, report failure.

Problem

Write a function that finds the maximum number of moves whose point gains sum exactly to target_points. Each value in move_lengths is a permitted positive gain, and any permitted gain may be used repeatedly. The order of moves does not affect the result. Return the greatest possible move count for an exact total. If target_points cannot be formed, return -1. A target of zero is already complete and requires zero moves.

Examples

Example 1
Input: move_lengths = [2, 3, 5]target_points = 11
Output: 5
Explanation: For target_points 11 with move_lengths [2, 3, 5], the executed result is 5, and the final best_count entry at index 11 is 5.
Example 2
Input: move_lengths = [4, 6]target_points = 7
Output: -1
Explanation: For target_points 7 with move_lengths [4, 6], the final best_count entry at index 7 is -1, so the executed result is -1.
Example 3
Input: move_lengths = [1, 3, 5]target_points = 0
Output: 0
Explanation: For target_points 0 with move_lengths [1, 3, 5], the executed result is 0.

Constraints

  • 0 <= target_points <= 500
  • 1 <= len(move_lengths) <= 20
  • 1 <= move_lengths[i] <= 500 for every i
  • Types: target_points is int, move_lengths is int[]; result is int

Function signature

def max_combo_moves(target_points: int, move_lengths: list[int]) -> int
target_points int
The exact number of combo points to fill.
move_lengths list[int]
Positive point gains available for individual moves; each gain can be reused.
returns int
The maximum number of moves whose gains total exactly target_points, or -1 when no exact total is possible.

Adapted from MBPP problem task_971 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • Repeated move lengths are allowed and must not change the correct result.
  • A move length larger than the target may be ignored naturally.
Approved · c007r1-abs_371_v414 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1