Skip to content

Commit 865e5b1

Browse files
refactor 390
1 parent 83911d2 commit 865e5b1

File tree

1 file changed

+24
-63
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+24
-63
lines changed
Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.fishercoder.solutions;
22

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.
77
* Starting from left to right,
88
* remove the first number and every other number afterward until you reach the end of the list.
99
* Repeat the previous step again, but this time from right to left,
@@ -25,68 +25,29 @@
2525
2626
*/
2727
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-
}
4928

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;
6546
}
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;
7949
}
50+
return start;
8051
}
81-
return list.get(0);
8252
}
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-
9253
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy