Skip to content

Commit 718bd8d

Browse files
committed
feat: update ts solution to lc problem: No.1823
1 parent 424dc09 commit 718bd8d

File tree

3 files changed

+9
-99
lines changed

3 files changed

+9
-99
lines changed

solution/1800-1899/1823.Find the Winner of the Circular Game/README.md

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -140,40 +140,10 @@ func findTheWinner(n int, k int) int {
140140
#### TypeScript
141141

142142
```ts
143-
class LinkNode {
144-
public val: number;
145-
public next: LinkNode;
146-
147-
constructor(val: number = 0, next?: LinkNode) {
148-
this.val = val;
149-
this.next = next;
150-
}
151-
}
152-
153143
function findTheWinner(n: number, k: number): number {
154-
if (k === 1) {
155-
return n;
156-
}
157-
const dummy = new LinkNode(0);
158-
let cur = dummy;
159-
for (let i = 1; i <= n; i++) {
160-
cur.next = new LinkNode(i);
161-
cur = cur.next;
162-
}
163-
cur.next = dummy.next;
164-
165-
cur = dummy;
166-
let count = 0;
167-
while (cur.next != cur) {
168-
count++;
169-
if (count === k) {
170-
cur.next = cur.next.next;
171-
count = 0;
172-
} else {
173-
cur = cur.next;
174-
}
175-
}
176-
return cur.val;
144+
if (n === 1) return 1;
145+
const res = (findTheWinner(n - 1, k) + k) % n;
146+
return res ? res : n;
177147
}
178148
```
179149

solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -139,40 +139,10 @@ func findTheWinner(n int, k int) int {
139139
#### TypeScript
140140

141141
```ts
142-
class LinkNode {
143-
public val: number;
144-
public next: LinkNode;
145-
146-
constructor(val: number = 0, next?: LinkNode) {
147-
this.val = val;
148-
this.next = next;
149-
}
150-
}
151-
152142
function findTheWinner(n: number, k: number): number {
153-
if (k === 1) {
154-
return n;
155-
}
156-
const dummy = new LinkNode(0);
157-
let cur = dummy;
158-
for (let i = 1; i <= n; i++) {
159-
cur.next = new LinkNode(i);
160-
cur = cur.next;
161-
}
162-
cur.next = dummy.next;
163-
164-
cur = dummy;
165-
let count = 0;
166-
while (cur.next != cur) {
167-
count++;
168-
if (count === k) {
169-
cur.next = cur.next.next;
170-
count = 0;
171-
} else {
172-
cur = cur.next;
173-
}
174-
}
175-
return cur.val;
143+
if (n === 1) return 1;
144+
const res = (findTheWinner(n - 1, k) + k) % n;
145+
return res ? res : n;
176146
}
177147
```
178148

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
1-
class LinkNode {
2-
public val: number;
3-
public next: LinkNode;
4-
5-
constructor(val: number = 0, next?: LinkNode) {
6-
this.val = val;
7-
this.next = next;
8-
}
9-
}
10-
111
function findTheWinner(n: number, k: number): number {
12-
if (k === 1) {
13-
return n;
14-
}
15-
const dummy = new LinkNode(0);
16-
let cur = dummy;
17-
for (let i = 1; i <= n; i++) {
18-
cur.next = new LinkNode(i);
19-
cur = cur.next;
20-
}
21-
cur.next = dummy.next;
22-
23-
cur = dummy;
24-
let count = 0;
25-
while (cur.next != cur) {
26-
count++;
27-
if (count === k) {
28-
cur.next = cur.next.next;
29-
count = 0;
30-
} else {
31-
cur = cur.next;
32-
}
33-
}
34-
return cur.val;
2+
if (n === 1) return 1;
3+
const res = (findTheWinner(n - 1, k) + k) % n;
4+
return res ? res : n;
355
}

0 commit comments

Comments
 (0)