-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path394.coins-in-a-line.py
61 lines (55 loc) · 1.43 KB
/
394.coins-in-a-line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Tag: Dynamic Programming/DP, Game DP, Game Theory
# Time: O(N)
# Space: O(N)
# Ref: -
# Note: -
# There are `n` coins in a line.
# Two players take turns to take one or two coins from right side until there are no more coins left.
# The player who take the last coin wins.
#
# Could you please decide the **first** player will win or lose?
#
# If the first player wins, return `true`, otherwise return `false`.
#
# **Example 1:**
#
# ```
# Input: 1
# Output: true
# ```
#
# **Example 2:**
#
# ```
# Input: 4
# Output: true
# Explanation:
# The first player takes 1 coin at first. Then there are 3 coins left.
# Whether the second player takes 1 coin or two, then the first player can take all coin(s) left.
# ```
#
#
class Solution:
"""
@param n: An integer
@return: A boolean which equals to true if the first player will win
"""
def first_will_win(self, n: int) -> bool:
# write your code here
if n == 0:
return False
if n <= 2:
return True
dp = [False for i in range(1 + n)]
dp[1] = dp[2] = True
for i in range(3, n + 1):
dp[i] = not (dp[i - 1] and dp[i - 2])
return dp[n]
class Solution:
"""
@param n: An integer
@return: A boolean which equals to true if the first player will win
"""
def first_will_win(self, n: int) -> bool:
# write your code here
return n % 3 != 0