Skip to content

Commit

Permalink
[DP] Add a new solution to Flip Game II
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Apr 22, 2018
1 parent 3255ed3 commit 835f8d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
39 changes: 39 additions & 0 deletions DP/FlipGameII.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Question Link: https://leetcode.com/problems/flip-game-ii/
* Primary idea: Classic DP, using a map to memorize previous step
* Time Complexity: O(n), Space Complexity: O(n)
*/

class FlipGameII {
func canWin(_ s: String) -> Bool {
var winMap = [String: Bool]()

return helper(s, &winMap)
}

func helper(_ s: String, _ winMap: inout [String: Bool]) -> Bool {
guard s.count >= 2 else {
return false
}

if let sWin = winMap[s] {
return sWin
}

let sChars = Array(s)

for i in 0..<sChars.count - 1 {
guard sChars[i] == sChars[i + 1] && sChars[i] == "+" else {
continue
}

if !helper(String(sChars[0..<i] + "--" + sChars[i + 2..<sChars.count]), &winMap) {
winMap[s] = true
return true
}
}

winMap[s] = false
return false
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 240 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 241 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand Down Expand Up @@ -174,6 +174,7 @@
[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)| [Swift](./DP/MinCostClimbingStairs.swift)| Easy| O(n)| O(n)|
[Unique Paths](https://leetcode.com/problems/unique-paths/)| [Swift](./DP/UniquePaths.swift)| Medium| O(mn)| O(mn)|
[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)| [Swift](./DP/UniquePathsII.swift)| Medium| O(mn)| O(mn)|
[Flip Game II](https://leetcode.com/problems/flip-game-ii/)| [Swift](./DP/FlipGameII.swift)| Medium| O(n)| O(n)|
[Decode Ways](https://leetcode.com/problems/decode-ways/)| [Swift](./DP/DecodeWays.swift) | O(n)|O(n)|
[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [Swift](./DP/MinimumPathSum.swift)| Medium| O(mn)| O(mn)|
[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Swift](./DP/GenerateParentheses.swift)| Medium| O(2^n)| O(n)|
Expand Down Expand Up @@ -521,7 +522,7 @@
| | 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | Hard |
| [Swift](./Tree/UniqueBinarySearchTrees.swift) | 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) &hearts; | Hard |
| | 295 | [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | Hard |
| | 294 | [Flip Game II](https://leetcode.com/problems/flip-game-ii/) &hearts; | Medium |
| [Swift](./DP/FlipGameII.swift) | 294 | [Flip Game II](https://leetcode.com/problems/flip-game-ii/) &hearts; | Medium |
| [Swift](./String/FlipGame.swift) | 293 | [Flip Game](https://leetcode.com/problems/flip-game/) &hearts; | Easy |
| | 292 | [Nim Game](https://leetcode.com/problems/nim-game/) | Easy |
| | 291 | [Word Pattern II](https://leetcode.com/problems/word-pattern-ii/) &hearts; | Hard |
Expand Down
8 changes: 3 additions & 5 deletions String/FlipGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
*/

class FlipGame {
func generatePossibleNextMoves(s: String) -> [String] {
func generatePossibleNextMoves(_ s: String) -> [String] {
var res = [String]()
var sChars = Array(s.characters)
let sChars = Array(s)

guard sChars.count > 1 else {
return res
}

for i in 0..<sChars.count - 1 {
if sChars[i] == "+" && sChars[i + 1] == "+" {
var temp = sChars
(temp[i], temp[i + 1]) = ("-", "-")
res.append(String(temp))
res.append(String(sChars[0..<i] + "--" + sChars[i + 2..<sChars.count]))
}
}

Expand Down

0 comments on commit 835f8d8

Please sign in to comment.