Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13bd762

Browse files
committed
feat: add solutions to lc problems: No.1269,1275
* No.1269.Number of Ways to Stay in the Same Place After Some Steps * No.1275.Find Winner on a Tic Tac Toe Game
1 parent 46c8b91 commit 13bd762

File tree

10 files changed

+345
-107
lines changed

10 files changed

+345
-107
lines changed

‎solution/1200-1299/1269.Number of Ways to Stay in the Same Place After Some Steps/README.md‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
**方法一:记忆化搜索**
6262

63-
我们观察题目的数据范围,可以发现,`steps` 最大不超过 500ドル,ドル这意味着我们最远只能往右走 500ドル$ 步。
63+
我们观察题目的数据范围,可以发现 $steps$ 最大不超过 500ドル,ドル这意味着我们最远只能往右走 500ドル$ 步。
6464

6565
我们可以设计一个函数 $dfs(i, j),ドル表示当前在位置 $i,ドル并且剩余步数为 $j$ 的方案数。那么答案就是 $dfs(0, steps)$。
6666

@@ -72,7 +72,7 @@
7272

7373
过程中,我们可以使用记忆化搜索避免重复计算。
7474

75-
时间复杂度 $O(steps \times steps),空间复杂度 O(steps \times steps)$。其中 $steps$ 是题目给定的步数。
75+
时间复杂度 $O(steps \times steps)$,空间复杂度 $O(steps \times steps)$。其中 $steps$ 是题目给定的步数。
7676

7777
<!-- tabs:start -->
7878

@@ -198,6 +198,32 @@ func numWays(steps int, arrLen int) int {
198198
}
199199
```
200200

201+
### **TypeScript**
202+
203+
```ts
204+
function numWays(steps: number, arrLen: number): number {
205+
const f = Array.from({ length: steps }, () => Array(steps + 1).fill(-1));
206+
const mod = 10 ** 9 + 7;
207+
const dfs = (i: number, j: number) => {
208+
if (i > j || i >= arrLen || i < 0 || j < 0) {
209+
return 0;
210+
}
211+
if (i == 0 && j == 0) {
212+
return 1;
213+
}
214+
if (f[i][j] != -1) {
215+
return f[i][j];
216+
}
217+
let ans = 0;
218+
for (let k = -1; k <= 1; ++k) {
219+
ans = (ans + dfs(i + k, j - 1)) % mod;
220+
}
221+
return (f[i][j] = ans);
222+
};
223+
return dfs(0, steps);
224+
}
225+
```
226+
201227
### **...**
202228

203229
```

‎solution/1200-1299/1269.Number of Ways to Stay in the Same Place After Some Steps/README_EN.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ func numWays(steps int, arrLen int) int {
168168
}
169169
```
170170

171+
### **TypeScript**
172+
173+
```ts
174+
function numWays(steps: number, arrLen: number): number {
175+
const f = Array.from({ length: steps }, () => Array(steps + 1).fill(-1));
176+
const mod = 10 ** 9 + 7;
177+
const dfs = (i: number, j: number) => {
178+
if (i > j || i >= arrLen || i < 0 || j < 0) {
179+
return 0;
180+
}
181+
if (i == 0 && j == 0) {
182+
return 1;
183+
}
184+
if (f[i][j] != -1) {
185+
return f[i][j];
186+
}
187+
let ans = 0;
188+
for (let k = -1; k <= 1; ++k) {
189+
ans = (ans + dfs(i + k, j - 1)) % mod;
190+
}
191+
return (f[i][j] = ans);
192+
};
193+
return dfs(0, steps);
194+
}
195+
```
196+
171197
### **...**
172198

173199
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numWays(steps: number, arrLen: number): number {
2+
const f = Array.from({ length: steps }, () => Array(steps + 1).fill(-1));
3+
const mod = 10 ** 9 + 7;
4+
const dfs = (i: number, j: number) => {
5+
if (i > j || i >= arrLen || i < 0 || j < 0) {
6+
return 0;
7+
}
8+
if (i == 0 && j == 0) {
9+
return 1;
10+
}
11+
if (f[i][j] != -1) {
12+
return f[i][j];
13+
}
14+
let ans = 0;
15+
for (let k = -1; k <= 1; ++k) {
16+
ans = (ans + dfs(i + k, j - 1)) % mod;
17+
}
18+
return (f[i][j] = ans);
19+
};
20+
return dfs(0, steps);
21+
}

‎solution/1200-1299/1275.Find Winner on a Tic Tac Toe Game/README.md‎

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,15 @@
8383

8484
<!-- 这里可写通用的实现逻辑 -->
8585

86-
判断 A、B 谁能获胜,只需判断最后一个落棋的人能否获胜即可。我们用数组 `counter` 记录 `0~2` 行、`0~2` 列、`正对角线``副对角线`是否已满 3 个棋子。如果等于 3,此人获胜,游戏结束。
86+
**方法一:判断最后一个落棋的人能否获胜**
8787

88-
若最后落棋者为未能获胜,棋盘被下满返回 `Draw`,未下满则返回 `Pending`
88+
由于 `moves` 都有效,也即是说,不存在某个人获胜后,其他人仍然落棋的情况。因此,只需判断最后一个落棋的人能否获胜即可
8989

90-
> 数组 `counter` 长度为 8,其中,`counter[0..2]` 对应 `0~2` 行,`counter[3..5]` 对应 `0~2` 列,`counter[6]` 对应正对角线,`counter[7]` 对应副对角线的落棋次数。
90+
我们用一个长度为 8ドル$ 的数组 `cnt` 记录行、列以及对角线的落棋次数。其中 $cnt[0, 1, 2]$ 分别表示第 0,ドル 1, 2$ 行的落棋次数,而 $cnt[3, 4, 5]$ 分别表示第 0,ドル 1, 2$ 列的落棋次数,另外 $cnt[6]$ 和 $cnt[7]$ 分别表示两条对角线的落棋次数。落棋过程中,如果某个人在某一行、列或对角线上落棋次数达到 3ドル$ 次,则该人获胜。
91+
92+
如果最后一个落棋的人没有获胜,那么我们判断棋盘是否已满,如果已满,则平局;否则,游戏尚未结束。
93+
94+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为 `moves` 的长度。
9195

9296
<!-- tabs:start -->
9397

@@ -99,22 +103,17 @@
99103
class Solution:
100104
def tictactoe(self, moves: List[List[int]]) -> str:
101105
n = len(moves)
102-
counter = [0] * 8
103-
for i in range(n - 1, -1, -2):
104-
row, col = moves[i][0], moves[i][1]
105-
counter[row] += 1
106-
counter[col + 3] += 1
107-
if row == col:
108-
counter[6] += 1
109-
if row + col == 2:
110-
counter[7] += 1
111-
if (
112-
counter[row] == 3
113-
or counter[col + 3] == 3
114-
or counter[6] == 3
115-
or counter[7] == 3
116-
):
117-
return "A" if (i % 2) == 0 else "B"
106+
cnt = [0] * 8
107+
for k in range(n - 1, -1, -2):
108+
i, j = moves[k]
109+
cnt[i] += 1
110+
cnt[j + 3] += 1
111+
if i == j:
112+
cnt[6] += 1
113+
if i + j == 2:
114+
cnt[7] += 1
115+
if any(v == 3 for v in cnt):
116+
return "B" if k & 1 else "A"
118117
return "Draw" if n == 9 else "Pending"
119118
```
120119

@@ -126,15 +125,19 @@ class Solution:
126125
class Solution {
127126
public String tictactoe(int[][] moves) {
128127
int n = moves.length;
129-
int[] counter = new int[8];
130-
for (int i = n - 1; i >= 0; i -= 2) {
131-
int row = moves[i][0], col = moves[i][1];
132-
++counter[row];
133-
++counter[col + 3];
134-
if (row == col) ++counter[6];
135-
if (row + col == 2) ++counter[7];
136-
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
137-
return (i % 2) == 0 ? "A" : "B";
128+
int[] cnt = new int[8];
129+
for (int k = n - 1; k >= 0; k -= 2) {
130+
int i = moves[k][0], j = moves[k][1];
131+
cnt[i]++;
132+
cnt[j + 3]++;
133+
if (i == j) {
134+
cnt[6]++;
135+
}
136+
if (i + j == 2) {
137+
cnt[7]++;
138+
}
139+
if (cnt[i] == 3 || cnt[j + 3] == 3 || cnt[6] == 3 || cnt[7] == 3) {
140+
return k % 2 == 0 ? "A" : "B";
138141
}
139142
}
140143
return n == 9 ? "Draw" : "Pending";
@@ -149,22 +152,80 @@ class Solution {
149152
public:
150153
string tictactoe(vector<vector<int>>& moves) {
151154
int n = moves.size();
152-
vector<int> counter(8, 0);
153-
for (int i = n - 1; i >= 0; i -= 2) {
154-
int row = moves[i][0], col = moves[i][1];
155-
++counter[row];
156-
++counter[col + 3];
157-
if (row == col) ++counter[6];
158-
if (row + col == 2) ++counter[7];
159-
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
160-
return (i % 2 == 0) ? "A" : "B";
155+
int cnt[8]{};
156+
for (int k = n - 1; k >= 0; k -= 2) {
157+
int i = moves[k][0], j = moves[k][1];
158+
cnt[i]++;
159+
cnt[j + 3]++;
160+
if (i == j) {
161+
cnt[6]++;
162+
}
163+
if (i + j == 2) {
164+
cnt[7]++;
165+
}
166+
if (cnt[i] == 3 || cnt[j + 3] == 3 || cnt[6] == 3 || cnt[7] == 3) {
167+
return k % 2 == 0 ? "A" : "B";
161168
}
162169
}
163170
return n == 9 ? "Draw" : "Pending";
164171
}
165172
};
166173
```
167174
175+
### **Go**
176+
177+
```go
178+
func tictactoe(moves [][]int) string {
179+
n := len(moves)
180+
cnt := [8]int{}
181+
for k := n - 1; k >= 0; k -= 2 {
182+
i, j := moves[k][0], moves[k][1]
183+
cnt[i]++
184+
cnt[j+3]++
185+
if i == j {
186+
cnt[6]++
187+
}
188+
if i+j == 2 {
189+
cnt[7]++
190+
}
191+
if cnt[i] == 3 || cnt[j+3] == 3 || cnt[6] == 3 || cnt[7] == 3 {
192+
if k%2 == 0 {
193+
return "A"
194+
}
195+
return "B"
196+
}
197+
}
198+
if n == 9 {
199+
return "Draw"
200+
}
201+
return "Pending"
202+
}
203+
```
204+
205+
### **TypeScript**
206+
207+
```ts
208+
function tictactoe(moves: number[][]): string {
209+
const n = moves.length;
210+
const cnt = new Array(8).fill(0);
211+
for (let k = n - 1; k >= 0; k -= 2) {
212+
const [i, j] = moves[k];
213+
cnt[i]++;
214+
cnt[j + 3]++;
215+
if (i == j) {
216+
cnt[6]++;
217+
}
218+
if (i + j == 2) {
219+
cnt[7]++;
220+
}
221+
if (cnt[i] == 3 || cnt[j + 3] == 3 || cnt[6] == 3 || cnt[7] == 3) {
222+
return k % 2 == 0 ? 'A' : 'B';
223+
}
224+
}
225+
return n == 9 ? 'Draw' : 'Pending';
226+
}
227+
```
228+
168229
### **...**
169230

170231
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /