File tree 4 files changed +10
-37
lines changed
4 files changed +10
-37
lines changed Original file line number Diff line number Diff line change 1
- package _20160910_4th_contest ;
1
+ package easy ;
2
2
3
- import java .util .HashSet ;
4
- import java .util .Iterator ;
5
- import java .util .LinkedList ;
6
- import java .util .Queue ;
7
- import java .util .Set ;
3
+ import java .util .*;
8
4
9
5
public class IntegerReplacement {
10
- public static int integerReplacement_failed (int n ) {
11
- if (n == 1 ) return 0 ;
12
- int steps = 0 ;
13
- while (n != 1 ){
14
- if (n %2 == 1 && n > 1 ) {
15
- n -= 1 ;
16
- steps ++;
17
- }
18
-
19
- n /= 2 ;
20
- steps ++;
21
- }
22
- return steps ;
23
- }
24
-
25
- public static int integerReplacement_failed_2 (int n ) {
26
- if (n == 1 ) return 0 ;
27
- int temp = 2 , steps = 1 ;
28
- while (temp <= n ){
29
- temp *= 2 ;
30
- steps ++;
31
-
32
- if (temp %2 == 1 ){
33
- temp += 1 ;
34
- steps ++;
35
- }
36
- }
37
- return steps ;
38
- }
39
6
40
7
public static int integerReplacement (int n ) {
41
8
long min = Long .MAX_VALUE ;
Original file line number Diff line number Diff line change 1
- package _20160910_4th_contest ;
1
+ package easy ;
2
2
3
3
//F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
4
4
public class RotateFunction {
Original file line number Diff line number Diff line change @@ -9,7 +9,10 @@ public class RandomPickIndex {
9
9
10
10
}
11
11
12
+ //TODO: use reservoir sampling to solve it again
13
+
12
14
class Solution {
15
+ //brute force
13
16
int [] input ;
14
17
java .util .Random rand = new java .util .Random ();
15
18
public Solution (int [] nums ) {
@@ -30,7 +33,7 @@ public int pick(int target) {
30
33
}
31
34
32
35
33
- class Solution_MLE {
36
+ class Solution_MemoryLimitExceeded {
34
37
35
38
private Map <Integer , List <Integer >> map = new HashMap ();
36
39
java .util .Random rand = new java .util .Random ();
Original file line number Diff line number Diff line change 2
2
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
3
3
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
4
4
| 415| [ Add Strings] ( https://leetcode.com/problems/add-strings/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/AddStrings.java ) | O(n)| O(1) | Easy|
5
+ |398|[ Random Pick Index] ( https://leetcode.com/problems/random-pick-index/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/RandomPickIndex.java ) | | | Medium| Reservoir Sampling
6
+ |397|[ Integer Replacement] ( https://leetcode.com/problems/integer-replacement/ ) |[ Solution] ( ../../blob/master/EASY/src/easy/IntegerReplacement.java ) | ? | ? | Easy| BFS
7
+ | 396| [ Rotate Function] ( https://leetcode.com/problems/rotate-function/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/RotateFunction.java ) | O(n^2) could be optimized to O(n) | O(1) | Easy|
5
8
| 390| [ Elimination Game] ( https://leetcode.com/problems/elimination-game/ ) | [ Solution] ( ../../blob/master/MEDIUM/src/medium/EliminationGame.java ) | O(logn)| O(1) | Medium|
6
9
| 389| [ Find the Difference] ( https://leetcode.com/problems/find-the-difference/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/FindTheDifference.java ) | O(n)| O(1) | Easy|
7
10
|388|[ Longest Absolute File Path] ( https://leetcode.com/problems/longest-absolute-file-path/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/LongestAbsoluteFilePath.java ) | O(n)|O(d) | Medium| Stack
You can’t perform that action at this time.
0 commit comments