Skip to content

Commit 3ce8dd2

Browse files
committed
Add solution #390
1 parent e36d92e commit 3ce8dd2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
309309
388|[Longest Absolute File Path](./0388-longest-absolute-file-path.js)|Medium|
310310
389|[Find the Difference](./0389-find-the-difference.js)|Easy|
311+
390|[Elimination Game](./0390-elimination-game.js)|Medium|
311312
392|[Is Subsequence](./0392-is-subsequence.js)|Easy|
312313
394|[Decode String](./0394-decode-string.js)|Medium|
313314
395|[Longest Substring with At Least K Repeating Characters](./0395-longest-substring-with-at-least-k-repeating-characters.js)|Medium|

solutions/0390-elimination-game.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
};

0 commit comments

Comments
 (0)