Build a Seven-Minute Playlist
Problem statement
Given the duration of each song in durations, build a playlist whose total playing time is exactly seven minutes, or 420 seconds.
For this exercise, each duration is a string in m:ss format. The minute part has one to three decimal digits, and the two-digit second part is between 00 and 59. Every duration is positive.
Use these practice rules:
- You may select any number of songs, but each input song may be used at most once. Songs with equal durations are still distinct songs.
- Return the selected songs as an increasing array of their zero-based input indices.
- If several selections total
420seconds, return the lexicographically smallest index array: at the first differing index, prefer the smaller value. If one array is a prefix of another, prefer the shorter array. - If no selection has the required total, return an empty array.
Function
buildPlaylist(durations: String[]) → int[]Examples
Example 1
durations = ["2:00","3:00","4:00","5:00"]return = [0,3]Both [0,3] and [1,2] total seven minutes. The first selection is lexicographically smaller.
Example 2
durations = ["1:00","2:00","4:00"]return = [0,1,2]All three songs are required: 60 + 120 + 240 = 420 seconds.
Example 3
durations = ["7:01","3:30"]return = []The first song is too long. The second song cannot be reused, so no selection reaches seven minutes.
Constraints
- For this exercise, assume
0 <= durations.length <= 200. - Every string follows the stated format, its minute value is between
0and999, and its total duration is positive. - The target is always exactly
420seconds.