Skip to content

Commit 11eaef6

Browse files
committed
practice
1 parent 0e9deb9 commit 11eaef6

9 files changed

+405
-67
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problems.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
// https://leetcode.com/problems/combination-sum/
9+
public class CombinationSum {
10+
11+
// runtime: O(2 ^ N) where N is number of candidates
12+
// space: O(N)
13+
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
14+
if (candidates == null || candidates.length < 1) {
15+
return Collections.emptyList();
16+
}
17+
Arrays.sort(candidates);
18+
List<List<Integer>> result = new ArrayList<>();
19+
combinationSum(candidates, target, 0, 0, new ArrayList<>(), result);
20+
return result;
21+
}
22+
23+
private static void combinationSum(int[] candidates, int target, int i, int currentSum, List<Integer> current,
24+
List<List<Integer>> result) {
25+
if (i >= candidates.length) {
26+
return;
27+
}
28+
29+
int candidate = candidates[i];
30+
if (currentSum + candidate == target) {
31+
current.add(candidate);
32+
result.add(new ArrayList<>(current));
33+
current.remove(current.size() - 1);
34+
return;
35+
} else if (currentSum + candidate < target) {
36+
current.add(candidate);
37+
combinationSum(candidates, target, i, currentSum + candidate, current, result);
38+
current.remove(current.size() - 1);
39+
} else {
40+
return;
41+
}
42+
43+
combinationSum(candidates, target, i + 1, currentSum, current, result);
44+
}
45+
46+
public static void main(String[] args) {
47+
System.out.println(combinationSum(new int[] { 2, 3, 6, 7 }, 6));
48+
System.out.println(combinationSum(new int[] { 4, 2, 8 }, 8));
49+
}
50+
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problems.leetcode;
2+
3+
// https://leetcode.com/problems/count-and-say/
4+
public class CountAndSay {
5+
6+
// runtime: O(N)
7+
// space: O(N) due to recursive call stack
8+
public static String countAndSay(int n) {
9+
if (n < 2) {
10+
return "1";
11+
}
12+
13+
String s = countAndSay(n - 1);
14+
StringBuilder sb = new StringBuilder();
15+
int current = s.charAt(0) - '0', count = 1;
16+
17+
for (int i = 1; i < s.length(); i++) {
18+
int num = s.charAt(i) - '0';
19+
if (current == num) {
20+
count++;
21+
} else {
22+
sb.append(count).append(current);
23+
current = num;
24+
count = 1;
25+
}
26+
}
27+
28+
sb.append(count).append(current);
29+
30+
return sb.toString();
31+
}
32+
33+
public static void main(String[] args) {
34+
System.out.println(countAndSay(2));
35+
}
36+
37+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
// https://leetcode.com/problems/first-missing-positive/
6+
public class FirstMissingPositive {
7+
8+
public static int firstMissingPositive(int[] nums) {
9+
// https://leetcode.com/problems/first-missing-positive/solutions/17214/Java-simple-solution-with-documentation/
10+
11+
// Ignore all numbers <=0 and >n since they are outside the range of possible
12+
// answers (which we proved was [1..n]). We do this by replacing them with the
13+
// value n+1.
14+
// For all other integers <n+1, mark their bucket (cell) to indicate the integer
15+
// exists. (*see below)
16+
// Find the first cell not marked, that is the first missing integer. If you did
17+
// not find an unmarked cell, there was no missing integer, so return n+1.
18+
19+
int n = nums.length;
20+
21+
// 1. mark numbers (num < 0) and (num > n) with a special marker number (n+1)
22+
// (we can ignore those because if all number are > n then we'll simply return
23+
// 1)
24+
for (int i = 0; i < n; i++) {
25+
if (nums[i] < 1 || nums[i] > n) {
26+
nums[i] = n + 1;
27+
}
28+
}
29+
System.out.println(Arrays.toString(nums));
30+
// note: all number in the array are now positive, and on the range 1..n+1
31+
32+
// 2. mark each cell appearing in the array, by converting the index for that
33+
// number to negative
34+
for (int i = 0; i < n; i++) {
35+
int num = Math.abs(nums[i]);
36+
if (num > n) {
37+
continue;
38+
}
39+
int numIndex = num - 1;
40+
if (nums[numIndex] > 0) { // prevents double negative operations
41+
nums[numIndex] = -1 * nums[numIndex];
42+
}
43+
}
44+
System.out.println(Arrays.toString(nums));
45+
46+
// 3. find the first cell which isn't negative (doesn't appear in the array)
47+
for (int i = 0; i < n; i++) {
48+
if (nums[i] >= 0) {
49+
return i + 1;
50+
}
51+
}
52+
53+
// 4. no positive numbers were found, which means the array contains all numbers
54+
// 1..n
55+
return n + 1;
56+
}
57+
58+
public static void main(String[] args) {
59+
System.out.println(firstMissingPositive(new int[] { 1, 2, 0 }));
60+
System.out.println(firstMissingPositive(new int[] { 3, 4, -1, 1 }));
61+
System.out.println(firstMissingPositive(new int[] { 7, 8, 9, 11, 12 }));
62+
System.out.println(firstMissingPositive(new int[] { 1, 8, 9, 11, 12 }));
63+
}
64+
65+
}

src/problems/leetcode/JumpGame.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,44 @@
66
public class JumpGame {
77

88
public static void main(String[] args) {
9-
// int[] nums = {2, 3, 1, 1, 4};
10-
int[] nums = {3, 2, 1, 0, 4};
11-
System.out.println(canJump(nums));
9+
// int[] nums = {2, 3, 1, 1, 4};
10+
int[] nums = { 3, 2, 1, 0, 4 };
11+
System.out.println(canJumpON(nums));
1212
}
1313

14-
public static boolean canJump(int[] nums) {
15-
if (nums == null) {
14+
// runtime: O(N)
15+
// space: O(1)
16+
public static boolean canJumpON(int[] nums) {
17+
for (int i = 0, boundary = 0; i <= boundary; i++) {
18+
boundary = Math.max(boundary, i + nums[i]);
19+
if (boundary >= nums.length - 1) {
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
}
26+
27+
// runtime: O(N^2)
28+
// space: O(N)
29+
public static boolean canJumpDP(int[] nums) {
30+
if (nums == null || nums.length < 1) {
1631
return false;
1732
}
1833

19-
int n = nums.length;
20-
boolean[] jumps = new boolean[n];
21-
jumps[n - 1] = true;
22-
for (int i = n - 2; i >= 0; i--) {
23-
for (int j = 1, maxJump = nums[i]; j <= maxJump; j++) {
24-
jumps[i] |= jumps[i + j];
34+
boolean[] success = new boolean[nums.length];
35+
int i = nums.length - 1;
36+
success[i--] = true;
37+
for (; i >= 0; i--) {
38+
for (int j = 1; j <= nums[i] && i + j < nums.length; j++) {
39+
if (success[i + j]) {
40+
success[i] = true;
41+
break;
42+
}
2543
}
2644
}
2745

28-
return jumps[0];
46+
return success[0];
2947
}
3048

3149
}

src/problems/leetcode/JumpGameII.java

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@
55
*/
66
public class JumpGameII {
77

8+
// runtime: O(N^2)
9+
// space: O(N)
10+
public static int jumpON2(int[] nums) {
11+
int n = nums.length;
12+
int[] counts = new int[n];
13+
for (int i = n - 2; i >= 0; i--) {
14+
int jumps = Integer.MAX_VALUE;
15+
for (int j = 1; j <= nums[i] && i + j < n; j++) {
16+
int c = counts[i + j];
17+
if (c != Integer.MAX_VALUE) {
18+
jumps = Math.min(jumps, 1 + c);
19+
}
20+
21+
}
22+
counts[i] = jumps;
23+
}
24+
25+
return counts[0];
26+
}
27+
28+
// runtime: O(N)
29+
// space: O(1)
30+
// https://leetcode.com/problems/jump-game-ii/editorial/
31+
public static int jumpGreedy(int[] nums) {
32+
// The starting range of the first jump is [0, 0]
33+
int answer = 0, n = nums.length;
34+
int curEnd = 0, curFar = 0;
35+
36+
for (int i = 0; i < n - 1; ++i) {
37+
// Update the farthest reachable index of this jump.
38+
curFar = Math.max(curFar, i + nums[i]);
39+
40+
// If we finish the starting range of this jump,
41+
// Move on to the starting range of the next jump.
42+
if (i == curEnd) {
43+
answer++;
44+
curEnd = curFar;
45+
}
46+
}
47+
48+
return answer;
49+
}
50+
851
public static int jumpON(int[] nums) {
952
if (nums == null) {
1053
return 0;
@@ -32,31 +75,33 @@ public static int jumpON(int[] nums) {
3275
return jumpCount;
3376
}
3477

78+
// https://leetcode.com/problems/jump-game-ii/editorial/comments/1776566
3579
public static int jump(int[] nums) {
3680
if (nums == null) {
3781
return 0;
3882
}
3983

40-
int steps = 0;
41-
for (int l = 0, r = 0; r < nums.length - 1; steps++) {
42-
int next = r;
43-
for (int i = l; i <= r; i++) {
44-
next = Math.max(next, nums[i] + i);
84+
int steps = 0, low = 0, high = 0, n = nums.length;
85+
while (high < n - 1) {
86+
int maxJump = 0;
87+
for (int i = low; i <= high; i++) {
88+
maxJump = Math.max(maxJump, i + nums[i]);
4589
}
46-
l = r + 1;
47-
r = next;
90+
low = high + 1;
91+
high = maxJump;
92+
steps++;
4893
}
4994

5095
return steps;
5196
}
5297

5398
public static void main(String[] args) {
54-
// int[] jumps = {2, 3, 1, 1, 4};
55-
int[] jumps = {4, 1, 1, 3, 1, 1, 1};
56-
// int[] jumps = {1, 2, 1, 1, 1};
57-
// int[] jumps = {10,9,8,7,6,5,4,3,2,1,1,0};
58-
System.out.println(jump(jumps));
59-
System.out.println(jumpON(jumps));
99+
// int[] jumps = {2, 3, 1, 1, 4};
100+
int[] jumps = { 4, 1, 1, 3, 1, 1, 1 };
101+
// int[] jumps = {1, 2, 1, 1, 1};
102+
// int[] jumps = {10,9,8,7,6,5,4,3,2,1,1,0};
103+
System.out.println(jumpGreedy(jumps));
104+
// System.out.println(jumpON(jumps));
60105
}
61106

62107
}

src/problems/leetcode/MultiplyStrings.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package problems.leetcode;
22

3+
import java.util.Arrays;
4+
35
/**
46
* https://leetcode.com/problems/multiply-strings/
57
*/
68
public class MultiplyStrings {
79

810
public static void main(String[] args) {
9-
String num1 = "12512";
10-
String num2 = "0";
11-
12-
String res = multiply(num1, num2);
13-
System.out.println(res);
11+
System.out.println(multiply("123456789", "987654321"));
1412
}
1513

14+
// runtime: O(A * B) where A and B are number of digits of the numbers
15+
// space: O(M) where M is number of digits of the greater number
1616
public static String multiply(String num1, String num2) {
1717
num1 = new StringBuilder(num1).reverse().toString();
1818
num2 = new StringBuilder(num2).reverse().toString();

0 commit comments

Comments
 (0)