File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 308
308
387|[ First Unique Character in a String] ( ./0387-first-unique-character-in-a-string.js ) |Easy|
309
309
388|[ Longest Absolute File Path] ( ./0388-longest-absolute-file-path.js ) |Medium|
310
310
389|[ Find the Difference] ( ./0389-find-the-difference.js ) |Easy|
311
+ 390|[ Elimination Game] ( ./0390-elimination-game.js ) |Medium|
311
312
392|[ Is Subsequence] ( ./0392-is-subsequence.js ) |Easy|
312
313
394|[ Decode String] ( ./0394-decode-string.js ) |Medium|
313
314
395|[ Longest Substring with At Least K Repeating Characters] ( ./0395-longest-substring-with-at-least-k-repeating-characters.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 390. Elimination Game
3
+ * https://leetcode.com/problems/elimination-game/
4
+ * Difficulty: Medium
5
+ *
6
+ * You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order.
7
+ * Apply the following algorithm on arr:
8
+ * - Starting from left to right, remove the first number and every other number afterward until
9
+ * you reach the end of the list.
10
+ * - Repeat the previous step again, but this time from right to left, remove the rightmost number
11
+ * and every other number from the remaining numbers.
12
+ * - Keep repeating the steps again, alternating left to right and right to left, until a single
13
+ * number remains.
14
+ *
15
+ * Given the integer n, return the last number that remains in arr.
16
+ */
17
+
18
+ /**
19
+ * @param {number } n
20
+ * @return {number }
21
+ */
22
+ var lastRemaining = function ( n ) {
23
+ let result = 1 ;
24
+
25
+ for ( let isRemaining = true , count = n , step = 1 ; count > 1 ; ) {
26
+ if ( isRemaining || count % 2 === 1 ) {
27
+ result += step ;
28
+ }
29
+ count = Math . floor ( count / 2 ) ;
30
+ isRemaining = ! isRemaining ;
31
+ step *= 2 ;
32
+ }
33
+
34
+ return result ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments