Solving algorithm problems is a very important process in preparing for coding tests. In this course, we will learn how to solve the problem titled ‘Creating Blu-rays’ using the Swift programming language. This problem is often encountered in actual job exams and will provide a good opportunity to understand both data structures and algorithms at the same time.
Problem Description
The given ‘Creating Blu-rays’ problem involves N movies, and the lengths of each movie are provided in the array filmLengths
. You need to fit these movies onto Blu-rays, with each Blu-ray having a maximum capacity of maxCapacity
. The challenge is to find a way to include all movies while minimizing the number of Blu-rays used.
The function signature is as follows:
func minimumBluRays(filmLengths: [Int], maxCapacity: Int) -> Int
Input Example
let films = [90, 85, 75, 60, 120]
let maxCap = 180
Output Example
minimumBluRays(filmLengths: films, maxCapacity: maxCap) // Result: 3
Approach
To solve this problem, we can consider two approaches.
- Greedy Algorithm: This involves sorting the movie lengths in ascending order and then placing the longest movie onto a Blu-ray with the remaining movies. This method allows us to maximize the number of movies added to the Blu-ray each time.
- Binary Search: This involves determining the maximum number of Blu-rays that can be used through binary search. We define a range for the possible maximum number of Blu-rays, and count the number needed based on a mid value. This method can also provide an efficient solution.
Solution Process
Here, we will take a closer look at how to solve the problem using the greedy algorithm.
Step 1: Sort Movies
First, sort the given movie lengths in ascending order. By doing this, you can place the shortest movies in order onto the Blu-ray, balancing the longest movie with the others.
let sortedFilms = filmLengths.sorted()
Step 2: Implement Blu-ray Placement Logic
We implement the logic to determine if movies can be placed onto each Blu-ray. While managing the current capacity of the Blu-ray, if the movie to be added does not exceed the capacity, we add it to that Blu-ray. If it exceeds the capacity, a new Blu-ray is created.
var bluRayCount = 1
var currentCapacity = 0
for film in sortedFilms {
if currentCapacity + film <= maxCapacity {
currentCapacity += film
} else {
bluRayCount += 1
currentCapacity = film
}
}
Step 3: Final Code
Combine the above steps to complete the final code.
func minimumBluRays(filmLengths: [Int], maxCapacity: Int) -> Int {
let sortedFilms = filmLengths.sorted()
var bluRayCount = 1
var currentCapacity = 0
for film in sortedFilms {
if currentCapacity + film <= maxCapacity {
currentCapacity += film
} else {
bluRayCount += 1
currentCapacity = film
}
}
return bluRayCount
}
// Example usage
let films = [90, 85, 75, 60, 120]
let maxCap = 180
print(minimumBluRays(filmLengths: films, maxCapacity: maxCap)) // Result: 3
Time Complexity
The time complexity of this solution is O(N log N). Sorting the movie lengths takes O(N log N), followed by a loop that takes O(N), making it an overall efficient solution.
Conclusion
This problem frequently appears in actual coding tests and can be effectively solved using the greedy algorithm. By understanding the problem and setting up the approach correctly, you can achieve good results in various coding tests. Continually practice solving these problems while familiarizing yourself with the features of the Swift language.
References
- Introduction to Algorithms, Thomas H. Cormen
- Algorithms, Robert Sedgewick
- Swift Programming: The Big Nerd Ranch Guide, Matthew Mathias