|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.List; |
5 |
| - |
6 |
| -/**There is a list of sorted integers from 1 to n. |
| 3 | +/** |
| 4 | + * 390. Elimination Game |
| 5 | + * |
| 6 | + * There is a list of sorted integers from 1 to n. |
7 | 7 | * Starting from left to right,
|
8 | 8 | * remove the first number and every other number afterward until you reach the end of the list.
|
9 | 9 | * Repeat the previous step again, but this time from right to left,
|
|
25 | 25 |
|
26 | 26 | */
|
27 | 27 | public class _390 {
|
28 |
| - |
29 |
| - //then I turned to Discuss and found this post: https://discuss.leetcode.com/topic/55870/share-my-solutions-for-contest-2 |
30 |
| - //instead of literally removing half of the elements in each scan, this solution is just moving the pointer to point to next start position |
31 |
| - //So brilliant! |
32 |
| - public int lastRemaining(int n) { |
33 |
| - int remaining = n; |
34 |
| - int start = 1; |
35 |
| - int step = 2; |
36 |
| - boolean forward = true; |
37 |
| - while (remaining > 1) { |
38 |
| - remaining /= 2; |
39 |
| - if (forward) { |
40 |
| - start = start + step * remaining - step / 2; |
41 |
| - } else { |
42 |
| - start = start - step * remaining + step / 2; |
43 |
| - } |
44 |
| - step *= 2; |
45 |
| - forward = !forward; |
46 |
| - } |
47 |
| - return start; |
48 |
| - } |
49 | 28 |
|
50 |
| - //I tried brute force, all producing the correct output, but got TLE by OJ. |
51 |
| - public static int lastRemaining_brute_force_TLE(int n) { |
52 |
| - List<Integer> list = new ArrayList(); |
53 |
| - for (int i = 0; i < n; i++) { |
54 |
| - list.add(i + 1); |
55 |
| - } |
56 |
| - boolean forward = true; |
57 |
| - while (list.size() > 1) { |
58 |
| - int size = list.size() / 2; |
59 |
| - if (list.size() == 1) { |
60 |
| - return list.get(0); |
61 |
| - } |
62 |
| - if (forward) { |
63 |
| - if (list.size() == 1) { |
64 |
| - return list.get(0); |
| 29 | + public static class Solution1 { |
| 30 | + /** |
| 31 | + * credit: https://discuss.leetcode.com/topic/55870/share-my-solutions-for-contest-2 instead of |
| 32 | + * literally removing half of the elements in each scan, this solution is just moving the |
| 33 | + * pointer to point to next start position So brilliant! |
| 34 | + */ |
| 35 | + public int lastRemaining(int n) { |
| 36 | + int remaining = n; |
| 37 | + int start = 1; |
| 38 | + int step = 2; |
| 39 | + boolean forward = true; |
| 40 | + while (remaining > 1) { |
| 41 | + remaining /= 2; |
| 42 | + if (forward) { |
| 43 | + start = start + step * remaining - step / 2; |
| 44 | + } else { |
| 45 | + start = start - step * remaining + step / 2; |
65 | 46 | }
|
66 |
| - for (int i = 0; i <= size && i < list.size(); i++) { |
67 |
| - list.remove(i); |
68 |
| - } |
69 |
| - forward = false; |
70 |
| - } else { |
71 |
| - if (list.size() == 1) { |
72 |
| - return list.get(0); |
73 |
| - } |
74 |
| - for (int i = list.size() - 1, count = 0; i >= 0 && count <= size; count++) { |
75 |
| - list.remove(i); |
76 |
| - i -= 2; |
77 |
| - } |
78 |
| - forward = true; |
| 47 | + step *= 2; |
| 48 | + forward = !forward; |
79 | 49 | }
|
| 50 | + return start; |
80 | 51 | }
|
81 |
| - return list.get(0); |
82 | 52 | }
|
83 |
| - |
84 |
| - public static void main(String... strings) { |
85 |
| - System.out.println(lastRemaining_brute_force_TLE(5204)); |
86 |
| - System.out.println(lastRemaining_brute_force_TLE(5058)); |
87 |
| -// System.out.println(lastRemaining(10)); |
88 |
| -// System.out.println(lastRemaining(9)); |
89 |
| -// System.out.println(lastRemaining(3)); |
90 |
| - } |
91 |
| - |
92 | 53 | }
|
0 commit comments