Skip to content

Commit dba0f37

Browse files
committed
update 395
1 parent c5c3890 commit dba0f37

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4040
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4141
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4242
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
43-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 49/94 | 10 vips |
43+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 53/93 | 11 vips |
4444
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
4545

46-
**Solved**: 438 problems
46+
**Solved**: 439 problems
4747

4848
## 类型/Category
4949

@@ -279,11 +279,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
279279

280280
## Game Theory
281281

282-
| Link | Problem(3) | Solution | Tag | Time | Space | Ref |
282+
| Link | Problem(4) | Solution | Tag | Time | Space | Ref |
283283
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
284284
| [Leetcode-375](https://leetcode.com/problems/guess-number-higher-or-lower-ii/) | Guess Number Higher Or Lower II | [c++](./leetcode/375.guess-number-higher-or-lower-ii.cpp), [python3](./leetcode/375.guess-number-higher-or-lower-ii.py) | Game Theory | O\(N^3\) | O\(N^2\) | - |
285285
| [Leetcode-3283](https://leetcode.com/problems/maximum-number-of-moves-to-kill-all-pawns/) | Maximum Number Of Moves To Kill All Pawns | [c++](./leetcode/3283.maximum-number-of-moves-to-kill-all-pawns.cpp), [python3](./leetcode/3283.maximum-number-of-moves-to-kill-all-pawns.py) | Game Theory | O\(2^K \* K\) | O\(2^K \* K\) | - |
286286
| [Lintcode-394](https://www.lintcode.com/problem/coins-in-a-line/) | Coins In A Line | [c++](./lintcode/394.coins-in-a-line.cpp), [python3](./lintcode/394.coins-in-a-line.py) | Game Theory | O\(N\) | O\(N\) | - |
287+
| [Lintcode-395](https://www.lintcode.com/problem/coins-in-a-line-ii/) | Coins In A Line II | [c++](./lintcode/395.coins-in-a-line-ii.cpp), [python3](./lintcode/395.coins-in-a-line-ii.py) | Game Theory | O\(N\) | O\(N\) | - |
287288

288289
## Binary Search
289290

lintcode/395.coins-in-a-line-ii.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Tag: Dynamic Programming/DP, Game Theory, Game DP
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// There are n coins with different value in a line.
8+
// Two players take turns to take one or two coins from **left side** until there are no more coins left.
9+
// The player who take the coins with the most value wins.
10+
//
11+
// Could you please decide the **first** player will win or lose?
12+
//
13+
// If the first player wins, return `true`, otherwise return `false`.
14+
//
15+
// **Example 1:**
16+
//
17+
// ```
18+
// Input: [1, 2, 2]
19+
// Output: true
20+
// Explanation: The first player takes 2 coins.
21+
// ```
22+
//
23+
// **Example 2:**
24+
//
25+
// ```
26+
// Input: [1, 2, 4]
27+
// Output: false
28+
// Explanation: Whether the first player takes 1 coin or 2, the second player will gain more value.
29+
// ```
30+
//
31+
//
32+
33+
class Solution {
34+
public:
35+
/**
36+
* @param values: a vector of integers
37+
* @return: a boolean which equals to true if the first player will win
38+
*/
39+
bool firstWillWin(vector<int> &values) {
40+
// write your code here
41+
int n = values.size();
42+
vector<int> dp(n + 1, 0);
43+
dp[n - 1] = values[n - 1];
44+
for (int i = n - 2; i >= 0; i--) {
45+
int v = values[i];
46+
dp[i] = max(dp[i], v - dp[i + 1]);
47+
48+
v = values[i] + values[i + 1];
49+
dp[i] = max(dp[i], v - dp[i + 2]);
50+
}
51+
52+
return dp[0] > 0;
53+
}
54+
};

lintcode/395.coins-in-a-line-ii.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Tag: Dynamic Programming/DP, Game Theory, Game DP
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# There are n coins with different value in a line.
8+
# Two players take turns to take one or two coins from **left side** until there are no more coins left.
9+
# The player who take the coins with the most value wins.
10+
#
11+
# Could you please decide the **first** player will win or lose?
12+
#
13+
# If the first player wins, return `true`, otherwise return `false`.
14+
#
15+
# **Example 1:**
16+
#
17+
# ```
18+
# Input: [1, 2, 2]
19+
# Output: true
20+
# Explanation: The first player takes 2 coins.
21+
# ```
22+
#
23+
# **Example 2:**
24+
#
25+
# ```
26+
# Input: [1, 2, 4]
27+
# Output: false
28+
# Explanation: Whether the first player takes 1 coin or 2, the second player will gain more value.
29+
# ```
30+
#
31+
#
32+
33+
from typing import (
34+
List,
35+
)
36+
37+
class Solution:
38+
"""
39+
@param values: a vector of integers
40+
@return: a boolean which equals to true if the first player will win
41+
"""
42+
def first_will_win(self, values: List[int]) -> bool:
43+
# write your code here
44+
n = len(values)
45+
dp = [0 for i in range(n + 1)]
46+
dp[n - 1] = values[n - 1]
47+
48+
for i in range(n - 2, -1, -1):
49+
v = values[i]
50+
dp[i] = max(dp[i], v - dp[i + 1])
51+
52+
v = values[i] + values[i + 1]
53+
dp[i] = max(dp[i], v - dp[i + 2])
54+
55+
return dp[0] > 0

list/9c-advanced.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@
7474
58. https://www.lintcode.com/problem/maximal-square-ii/
7575
59. https://leetcode.com/problems/unique-paths/
7676
60. https://leetcode.com/problems/minimum-path-sum/
77-
78-
- https://www.lintcode.com/problem/edit-distance/
79-
- https://www.lintcode.com/problem/longest-continuous-increasing-subsequence/
80-
- https://www.lintcode.com/problem/longest-continuous-increasing-subsequence-ii/
81-
- https://www.lintcode.com/problem/coins-in-a-line/
82-
- https://www.lintcode.com/problem/coins-in-a-line-ii/
83-
- https://www.lintcode.com/problem/coins-in-a-line-iii/
77+
61. https://leetcode.com/problems/edit-distance/
78+
62. https://leetcode.com/problems/longest-continuous-increasing-subsequence/
79+
63. https://www.lintcode.com/problem/coins-in-a-line/
80+
64. https://www.lintcode.com/problem/coins-in-a-line-ii/
81+
65. https://www.lintcode.com/problem/coins-in-a-line-iii/
8482

8583
- https://www.lintcode.com/problem/stone-game/
8684
- https://www.lintcode.com/problem/burst-balloons/

0 commit comments

Comments
 (0)