File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
solution/1800-1899/1823.Find the Winner of the Circular Game Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -161,4 +161,30 @@ function findTheWinner(n, k) {
161
161
162
162
<!-- solution: end -->
163
163
164
+ <!-- solution: start -->
165
+
166
+ ### Solution 2. Simulation
167
+
168
+ <!-- tabs: start -->
169
+
170
+ #### TypeScript
171
+
172
+ ``` ts
173
+ function findTheWinner(n : number , k : number ): number {
174
+ const arr = Array .from ({ length: n }, (_ , i ) => i + 1 );
175
+ let i = 0 ;
176
+
177
+ while (arr .length > 1 ) {
178
+ i = (i + k - 1 ) % arr .length ;
179
+ arr .splice (i , 1 );
180
+ }
181
+
182
+ return arr [0 ];
183
+ }
184
+ ```
185
+
186
+ <!-- tabs: end -->
187
+
188
+ <!-- solution: end -->
189
+
164
190
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -160,4 +160,30 @@ function findTheWinner(n, k) {
160
160
161
161
<!-- solution: end -->
162
162
163
+ <!-- solution: start -->
164
+
165
+ ### Solution 2. Simulation
166
+
167
+ <!-- tabs: start -->
168
+
169
+ #### TypeScript
170
+
171
+ ``` ts
172
+ function findTheWinner(n : number , k : number ): number {
173
+ const arr = Array .from ({ length: n }, (_ , i ) => i + 1 );
174
+ let i = 0 ;
175
+
176
+ while (arr .length > 1 ) {
177
+ i = (i + k - 1 ) % arr .length ;
178
+ arr .splice (i , 1 );
179
+ }
180
+
181
+ return arr [0 ];
182
+ }
183
+ ```
184
+
185
+ <!-- tabs: end -->
186
+
187
+ <!-- solution: end -->
188
+
163
189
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function findTheWinner ( n : number , k : number ) : number {
2
+ const arr = Array . from ( { length : n } , ( _ , i ) => i + 1 ) ;
3
+ let i = 0 ;
4
+
5
+ while ( arr . length > 1 ) {
6
+ i = ( i + k - 1 ) % arr . length ;
7
+ arr . splice ( i , 1 ) ;
8
+ }
9
+
10
+ return arr [ 0 ] ;
11
+ }
You can’t perform that action at this time.
0 commit comments