295,745 views
9 votes
9 votes
You are given a sequence of n songs where the ith song is l minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD 1, CD 2, CD 3,... ,CD k) where each CD can hold m minutes. Furthermore, (1) The songs must be recorded in the given order, song 1, song 2,..., song n. (2) All songs must be included. (3) No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to find an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity

User BoJack Horseman
by
2.8k points

2 Answers

14 votes
14 votes

Final answer:

To find an optimal solution for this problem, we can use a greedy algorithm. We start by placing the first song on the first CD and then iterate through the remaining songs, checking if they can fit on the current CD. The algorithm guarantees the correct placement of songs and minimizes the number of CDs needed.

Step-by-step explanation:

To find an optimal solution for this problem, we can use a greedy algorithm. We can start by placing the first song on the first CD, and then iterate through the remaining songs. For each song, we check if it can fit on the current CD. If it can, we add the song to the CD. If it can't, we start a new CD and add the song to it. This process continues until all songs have been placed on CDs.

Here is the algorithm:

  1. Create an empty CD list.
  2. Create a new CD and add the first song to it.
  3. Iterate through the remaining songs:
  • If the current song can fit on the current CD, add it to the CD.
  • If the current song can't fit on the current CD, create a new CD and add the song to it.
Return the number of CDs created.

This algorithm is correct because it guarantees that all songs will be placed on CDs in the given order, with no song split across CDs. It also minimizes the number of CDs needed, as it tries to fit as many songs as possible on each CD before moving on to a new CD.

The time complexity of this algorithm is O(n), where n is the number of songs. This is because we iterate through each song once to determine which CD it should be placed on. The other operations, such as creating CDs and adding songs, have constant time complexity.

User Ondrej Skalicka
by
2.8k points
14 votes
14 votes

Answer:

This can be done by a greedy solution. The algorithm does the following:

Put song 1 on CD1.

For song 1, if there's space left on the present CD, then put the song on the present CD. If not, use a replacement CD.

If there are not any CDs left, output "no solution".

Step-by-step explanation:

The main thing is prove the correctness, do that by the "greedy stays ahead argument". For the primary song, the greedy solution is perfect trivially.

Now, let the optimal solution match the greedy solution upto song i. Then, if the present CD has space and optimal solution puts song (i+1) on an equivalent CD, then the greedy and optimal match, hence greedy is not any worse than the optimal.Else, optimal puts song (i + 1) on subsequent CD. Consider an answer during which only song (i + 1) is moved to the present CD and zip else is modified. Clearly this is often another valid solution and no worse than the optimal, hence greedy is not any worse than the optimal solution during this case either. This proves the correctness of the algorithm. As for every song, there are constantly many operations to try to do, the complexity is O(n).

User Ale Zalazar
by
3.1k points