File tree Expand file tree Collapse file tree 3 files changed +9
-99
lines changed
solution/1800-1899/1823.Find the Winner of the Circular Game Expand file tree Collapse file tree 3 files changed +9
-99
lines changed Original file line number Diff line number Diff line change @@ -140,40 +140,10 @@ func findTheWinner(n int, k int) int {
140
140
#### TypeScript
141
141
142
142
``` 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
-
153
143
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 ;
177
147
}
178
148
```
179
149
Original file line number Diff line number Diff line change @@ -139,40 +139,10 @@ func findTheWinner(n int, k int) int {
139
139
#### TypeScript
140
140
141
141
``` 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
-
152
142
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 ;
176
146
}
177
147
```
178
148
Original file line number Diff line number Diff line change 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
-
11
1
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 ;
35
5
}
You can’t perform that action at this time.
0 commit comments