Skip to content

Commit b6cf5d4

Browse files
committed
293_Flip_Game
294_Flip_Game_II
1 parent 0d58266 commit b6cf5d4

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/280_Wiggle_Sort.py) &hearts; | 1. Sort and insert (n - 1) / 2 from tail to correct position, O(nlogn) and O(1)<br>2. Sort and swap(i, i + 1) from 1 to n - 1, O(nlogn)<br>3. Iteratively check order and reverse order, if not satisfied, then swap i with i + 1, O(n) |
9797
| 286 | [Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/286_Walls_and_Gates.py) | BFS with queue, O(mn) and O(mn) |
9898
| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
99+
| 293 | [Flip Game](https://leetcode.com/problems/flip-game/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/293_Flip_Game.py) | Python string slicing |
100+
| 294 | [Flip Game II](https://leetcode.com/problems/flip-game-ii/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/294_Flip_Game_II.py) | 1. Backtracking to ensure that next step is False, O(n!!) and O(n!!)<br>2. Backtracking with memo, O(n!!) and O(n!)<br>3. DP based on [Sprague-Grundy Function](https://discuss.leetcode.com/topic/27282/theory-matters-from-backtracking-128ms-to-dp-0ms) |
99101
| 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/296_Best_Meeting_Point.py) | Think hard about Manhattan Distance in 1D case. Sort and find mean, O(mnlogmn) and O(1) |
100102
| 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/298_Binary_Tree_Longest_Consecutive_Sequence.py) | Bottom-up or top-down recursion, O(n) and O(n) |
101103
| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/305_Number_of_Islands_II.py) | Quick union find with weights, O(nlogn) and O(n) |
@@ -106,7 +108,7 @@
106108
| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) &hearts; |
107109
[Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) |
108110
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
109-
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n)|
111+
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n) |
110112
| 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/368_Largest_Divisible_Subset.py) | Sort and generate x subset with previous results, O(n^2) and O(n^2) |
111113
| 370 | [Range Addition](https://leetcode.com/problems/range-addition/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/370_Range_Addition.py) | Interval problem with cumulative sums, O(n + k) and O(n) |
112114
| 384 | [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/384_Shuffle_an_Array.py) | Fisher–Yates shuffle, O(n) and O(n) |

python/293_Flip_Game.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def generatePossibleNextMoves(self, s):
3+
"""
4+
:type s: str
5+
:rtype: List[str]
6+
"""
7+
# return [s[:i] + "--" + s[i+2:] for i in range(len(s) - 1) if s[i] == s[i + 1] == "+"]
8+
res = []
9+
if s is None or len(s) == 0:
10+
return res
11+
ls = len(s)
12+
for i in range(ls - 1):
13+
if s[i] == '+' and s[i + 1] == '+':
14+
res.append(s[:i] + '--' + s[i + 2:])
15+
return res

python/294_Flip_Game_II.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class Solution(object):
2+
# def canWin(self, s):
3+
# """
4+
# :type s: str
5+
# :rtype: bool
6+
# """
7+
# if s is None or len(s) < 2:
8+
# return False
9+
# for i in range(len(s) - 1):
10+
# if s[i:i + 2] == '++':
11+
# if not self.canWin(s[:i] + '--' + s[i + 2:]):
12+
# return True
13+
# return False
14+
15+
def canWin(self, s):
16+
if s is None or len(s) < 2:
17+
return False
18+
list_s = list(s)
19+
return self.canWin_helper(list_s)
20+
21+
def canWin_helper(self, s):
22+
for i in range(len(s) - 1):
23+
if s[i] == '+' and s[i + 1] == '+':
24+
s[i] = '-'
25+
s[i + 1] = '-'
26+
res = self.canWin_helper(s)
27+
s[i] = '+'
28+
s[i + 1] = '+'
29+
if not res:
30+
return True
31+
return False
32+
33+
# def canWin(self, s):
34+
# # backtracking with memo
35+
# memo = {}
36+
37+
# def can(s):
38+
# if s not in memo:
39+
# memo[s] = any(s[i:i + 2] == '++' and not can(s[:i] + '-' + s[i + 2:])
40+
# for i in range(len(s)))
41+
# return memo[s]
42+
# return can(s)
43+
# https://discuss.leetcode.com/topic/27282/theory-matters-from-backtracking-128ms-to-dp-0ms/3
44+
# def canWin(self, s):
45+
# g, G = [0], 0
46+
# for p in map(len, re.split('-+', s)):
47+
# while len(g) <= p:
48+
# g += min(set(range(p)) - {a^b for a, b in zip(g, g[-2::-1])}),
49+
# G ^= g[p]
50+
# return G > 0
51+
52+
# def canWin(self, s):
53+
# currlen, maxlen = 0, 0
54+
# board_ini_state = []
55+
# for i in range(len(s)):
56+
# if s[i] == '+':
57+
# currlen += 1
58+
# if i + 1 == len(s) or s[i] == '-':
59+
# if currlen >= 2:
60+
# board_ini_state.append(currlen)
61+
# maxlen = max(maxlen, currlen)
62+
# currlen = 0
63+
# g = [0] * (maxlen + 1)
64+
# for l in range(2, maxlen + 1):
65+
# gsub = set()
66+
# for len_first_game in range(l / 2):
67+
# len_second_game = l - len_first_game - 2
68+
# gsub.add(g[len_first_game] ^ g[len_second_game])
69+
# g[l] = firstMissingNumber(gsub)
70+
# g_final = 0
71+
# for state in board_ini_state:
72+
# g_final ^= g[state]
73+
# return g_final != 0
74+
75+
# def firstMissingNumber(lut):
76+
# ls = len(lut)
77+
# for i in range(ls):
78+
# if i not in lut:
79+
# return i
80+
# return ls
81+
82+
if __name__ == '__main__':
83+
s = Solution()
84+
print s.canWin("++++")

0 commit comments

Comments
 (0)