File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
algorithms/cpp/eliminationGame Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 88
99| # | Title | Solution | Difficulty |
1010| ---| ----- | -------- | ---------- |
11+ | 390| [ Elimination Game] ( https://leetcode.com/contest/2/problems/elimination-game/ ) | [ C++] ( ./algorithms/cpp/eliminationGame/EliminationGame.cpp ) | Medium|
1112| 389| [ Find the Difference] ( https://leetcode.com/problems/find-the-difference/ ) | [ C++] ( ./algorithms/cpp/findTheDifference/FindTheDifference.cpp ) | Easy|
1213| 388| [ Longest Absolute File Path] ( https://leetcode.com/problems/longest-absolute-file-path/ ) | [ C++] ( ./algorithms/cpp/longestAbsoluteFilePath/LongestAbsoluteFilePath.cpp ) | Medium|
1314| 387| [ First Unique Character in a String] ( https://leetcode.com/problems/first-unique-character-in-a-string/ ) | [ C++] ( ./algorithms/cpp/firstUniqueCharacterInAString/FirstUniqueCharacterInAString.cpp ) | Easy|
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/contest/2/problems/elimination-game/
2+ // Author : Hao Chen
3+ // Date : 2016-09-07-
4+
5+ /* *********************************************************************************
6+ *
7+ * There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other
8+ * number afterward until you reach the end of the list.
9+ *
10+ * Repeat the previous step again, but this time from right to left, remove the right most number and every other number
11+ * from the remaining numbers.
12+ *
13+ * We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
14+ *
15+ * Find the last number that remains starting with a list of length n.
16+ *
17+ * Example:
18+ *
19+ * Input:
20+ * n = 9,
21+ * 1 2 3 4 5 6 7 8 9
22+ * 2 4 6 8
23+ * 2 6
24+ * 6
25+ *
26+ * Output:
27+ * 6
28+ **********************************************************************************/
29+
30+ class Solution {
31+ public:
32+ int lastRemaining (int n) {
33+ int start = 1 , step = 1 ;
34+ while (n > 1 ) {
35+ start += step + (n-2 )/2 * 2 *step;
36+ n /= 2 ;
37+ step *= -2 ;
38+ }
39+ return start;
40+ }
41+ };
You can’t perform that action at this time.
0 commit comments