forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DP] Add solutions to Paint House and Paint House II
- Loading branch information
Showing
3 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/paint-house/ | ||
* Primary idea: Dynamic Programming, dp[i][j] += min(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) | ||
* | ||
* Time Complexity: O(n), Space Complexity: O(n) | ||
*/ | ||
|
||
class PaintHouse { | ||
func minCost(_ costs: [[Int]]) -> Int { | ||
guard let colors = costs.first, !colors.isEmpty else { | ||
return 0 | ||
} | ||
|
||
var dp = costs | ||
|
||
for i in 1..<costs.count { | ||
for j in 0..<colors.count { | ||
dp[i][j] += min(dp[i - 1][(j + 1) % colors.count], dp[i - 1][(j + 2) % colors.count]) | ||
} | ||
} | ||
|
||
return dp[costs.count - 1].min()! | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/paint-house-ii/ | ||
* Primary idea: Dynamic Programming, keep update previous min cost with its corresponding color | ||
* and the second min cost. | ||
* | ||
* Note: DP with 2-D array will throw Time Limited Error | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
*/ | ||
|
||
class PaintHouseII { | ||
func minCostII(_ costs: [[Int]]) -> Int { | ||
guard let colors = costs.first, !colors.isEmpty else { | ||
return 0 | ||
} | ||
|
||
var preColor = -1, preMinCost = 0, preSecondMinCost = 0 | ||
|
||
for cost in costs { | ||
|
||
var currentColor = -1, currentMinCost = Int.max, currentSecondMinCost = Int.max | ||
|
||
for i in 0..<colors.count { | ||
let sum = cost[i] + (preColor == i ? preSecondMinCost : preMinCost) | ||
|
||
if sum < currentMinCost { | ||
currentSecondMinCost = currentMinCost | ||
currentMinCost = sum | ||
currentColor = i | ||
} else if sum < currentSecondMinCost { | ||
currentSecondMinCost = sum | ||
} | ||
} | ||
|
||
preColor = currentColor | ||
preMinCost = currentMinCost | ||
preSecondMinCost = currentSecondMinCost | ||
} | ||
|
||
return preMinCost | ||
} | ||
} |
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