Skip to content

Commit

Permalink
[DP] Add solutions to Paint House and Paint House II
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Dec 30, 2019
1 parent f615c90 commit 5a1cbe9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
24 changes: 24 additions & 0 deletions DP/PaintHouse.swift
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()!
}
}
41 changes: 41 additions & 0 deletions DP/PaintHouseII.swift
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
}
}
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 312 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 314 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -236,6 +236,8 @@
[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)| [Swift](./DP/MaximumSumThreeNonOverlappingSubarrays.swift)| Hard| O(n)| O(n)|
[Coin Change](https://leetcode.com/problems/coin-change/)| [Swift](./DP/CoinChange.swift)| Medium| O(n^2)| O(n)|
[Coin Change II](https://leetcode.com/problems/coin-change-ii/)| [Swift](./DP/CoinChangeII.swift)| Medium| O(n^2)| O(n)|
[Paint House](https://leetcode.com/problems/paint-house/)| [Swift](./DP/PaintHouse.swift)| Easy| O(n)| O(n)|
[Paint House II](https://leetcode.com/problems/paint-house-ii/)| [Swift](./DP/PaintHouseII.swift)| Hard| O(n)| O(1)|
[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(nlogn)| O(n)|
[Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)| [Swift](./DP/PalindromicSubstrings.swift)| Medium| O(n^2)| O(n^2)|
[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)| [Swift](./DP/LongestPalindromicSubstring.swift)| Medium| O(n^2)| O(n^2)|
Expand Down Expand Up @@ -643,15 +645,15 @@
| [Swift](./Math/MissingNumber.swift) | 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | Easy |
| | 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) &hearts; | Medium |
| [Swift](./String/PalindromePermutation.swift) | 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) &hearts; | Easy |
| | 265 | [Paint House II](https://leetcode.com/problems/paint-house-ii/) &hearts; | Hard |
| [Swift](./DP/PaintHouseII.swift) | 265 | [Paint House II](https://leetcode.com/problems/paint-house-ii/) &hearts; | Hard |
| [Swift](./Math/UglyNumberII.swift) | 264 | [Ugly Number II](https://leetcode.com/problems/ugly-number-ii/) | Medium |
| [Swift](./Math/UglyNumber.swift) | 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | Easy |
| [Swift](./Sort/GraphValidTree.swift) | 261 | [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/) &hearts; | Medium |
| | 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | Medium |
| | 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) &hearts; | Medium |
| [Swift](./Math/AddDigits.swift) | 258 | [Add Digits](https://leetcode.com/problems/add-digits/) | Easy |
| [Swift](./Tree/BnaryTreePaths.swift) | 257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | Easy |
| | 256 | [Paint House](https://leetcode.com/problems/paint-house/) &hearts; | Medium |
| [Swift](./DP/PaintHouse.swift) | 256 | [Paint House](https://leetcode.com/problems/paint-house/) &hearts; | Medium |
| | 255 | [Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/) &hearts; | Medium |
| [Swift](./DFS/FactorCombinations.swift) | 254 | [Factor Combinations](https://leetcode.com/problems/factor-combinations/) &hearts; | Medium |
| [Swift](./Sort/MeetingRoomsII.swift) | 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) &hearts; | Medium |
Expand Down

0 comments on commit 5a1cbe9

Please sign in to comment.