Created
February 15, 2015 00:55
-
-
Save mjumbewu/e7923c3ae91c42c2062a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NewAlbum: | |
def leastAmountOfCDs(self, nSongs, length, cdCapacity): | |
# First, for kicks, let's just check whether all the songs fit on | |
# one CD | |
totalLength = nSongs * length | |
if totalLength <= cdCapacity: | |
return 1 | |
# If all the songs don't fit on one CD, start iterating through the | |
# songs. | |
numCDs = 1 # The total number of CDs that we need to fit the songs we've counted so far | |
lengthSoFar = 0 # The length of the songs on the current CD | |
for i in range(nSongs): | |
# Add the length of the current song on to the CD | |
lengthSoFar += length | |
# If we've exceeded the current CD's capacity, add another CD | |
if lengthSoFar > cdCapacity: | |
numCDs += 1 | |
# Since we exceeded the last CD's capacity, we have to remove one | |
# song and put it on the next CD. | |
lengthSoFar = length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment