The coding test is an important process for applicants to validate their programming skills. In this course, we will learn how to solve algorithm problems in Swift through the topic of Calculating the Amount of Water.
Problem Description
The problem is to calculate the amount of water that can be stored at each unit height when it rains, given an array of heights. For example, consider the following height array:
heights = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Calculate the amount of water that can be stored in this height array when it rains. The amount of water stored at each position is determined by the higher boundaries on both sides.
Approach to the Problem
The basic approach to solve this problem is as follows:
- Calculate the maximum heights on the left and right for each position.
- Determine if water can be stored based on the smaller of the two maximum heights that are lower than the current height.
- Accumulate the amount of water at each position to derive the result.
Algorithm Explanation
To implement the above algorithm, we use two arrays to store the maximum heights from the left and right at each position. We can then calculate the amount of water at each position.
Step 1: Analyzing the Height Array
This is the process of obtaining the maximum heights on both sides for each position in the given height array.
Creating the Left Maximum Height Array
First, we create an array to store the maximum height from the left. Each element of the array stores the maximum value of all building heights to the left of the current position.
var leftMax = [Int](repeating: 0, count: heights.count) leftMax[0] = heights[0] for i in 1..Creating the Right Maximum Height Array
Next, we create an array to store the maximum height from the right. This array is also created in the same manner.
var rightMax = [Int](repeating: 0, count: heights.count) rightMax[heights.count - 1] = heights[heights.count - 1] for i in (0..<(heights.count - 1)).reversed() { rightMax[i] = max(rightMax[i + 1], heights[i]) }Step 2: Calculating the Amount of Water
Now, we calculate the amount of water that can be stored at each position. The amount of water stored can be calculated as the minimum of the left maximum height and the right maximum height minus the current height.
var waterTrapped = 0 for i in 0..Complete Swift Code
func trap(_ heights: [Int]) -> Int { guard heights.count > 2 else { return 0 } var leftMax = [Int](repeating: 0, count: heights.count) var rightMax = [Int](repeating: 0, count: heights.count) leftMax[0] = heights[0] for i in 1..Result Analysis
When the above code is executed, the amount of water stored in the given height array is output as 6. This can be visualized as follows:
- Index 2 can store 1 unit of water.
- Index 4 can store 1 unit of water.
- Index 5 can store 3 units of water.
- Index 6 can store 1 unit of water.
- Index 8 can store 1 unit of water.
Conclusion
In this course, we covered an algorithm problem of calculating the amount of water using Swift. This problem can be solved using arrays, loops, and conditional statements, and requires comparing the maximum heights at each position. It is important to understand the structure of the problem well and organize the approach systematically when solving algorithm problems.
If you want to learn more algorithm problems and solutions, I recommend referring to various related materials for practice.