Skip to content

Commit 0488ecf

Browse files
committed
practice
1 parent fb12165 commit 0488ecf

10 files changed

+364
-74
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package problems.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.Deque;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
// https://leetcode.com/problems/combination-sum-ii/
14+
public class CombinationSumII {
15+
16+
public static void main(String[] args) {
17+
for (List<Integer> l : combinationSum2(new int[] { 10, 1, 2, 2, 2, 2, 7, 6, 1, 1, 5 }, 8)) {
18+
System.out.println(l);
19+
}
20+
21+
// for (List<Integer> l : combinationSum2(new int[] { 10, 1, 2, 2, 7, 6, 1, 1, 5
22+
// }, 8)) {
23+
// System.out.println(l);
24+
// }
25+
}
26+
27+
// 1, 1, 1, 2, 2, 5, 6, 7, 10 target = 8
28+
29+
public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
30+
// container to hold the final combinations
31+
List<List<Integer>> results = new ArrayList<>();
32+
Deque<Integer> comb = new ArrayDeque<>();
33+
Map<Integer, Integer> counter = new HashMap<>();
34+
for (int candidate : candidates) {
35+
counter.compute(candidate, (k, v) -> v != null ? v + 1 : 1);
36+
}
37+
38+
// convert the counter table to a list of (num, count) tuples
39+
List<int[]> counterList = new ArrayList<>();
40+
counter.forEach((key, value) -> {
41+
counterList.add(new int[] { key, value });
42+
});
43+
44+
backtrack(comb, target, 0, counterList, results);
45+
return results;
46+
}
47+
48+
private static void backtrack(Deque<Integer> comb,
49+
int target, int i,
50+
List<int[]> counter,
51+
List<List<Integer>> results) {
52+
if (target == 0) {
53+
// make a deep copy of the current combination.
54+
results.add(new ArrayList<Integer>(comb));
55+
return;
56+
} else if (target < 0) {
57+
return;
58+
}
59+
60+
for (int j = i; j < counter.size(); j++) {
61+
int[] entry = counter.get(j);
62+
int candidate = entry[0], freq = entry[1];
63+
if (freq <= 0) {
64+
continue;
65+
}
66+
67+
// add a new element to the current combination
68+
comb.addLast(candidate);
69+
entry[1]--;
70+
71+
// continue the exploration with the updated combination
72+
backtrack(comb, target - candidate, j, counter, results);
73+
74+
// backtrack the changes, so that we can try another candidate
75+
entry[1]++;
76+
comb.removeLast();
77+
}
78+
}
79+
}

src/problems/leetcode/InsertInterval.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,38 @@ public static void main(String[] args) {
2020

2121
}
2222

23-
public static int[][] insert(int[][] intervals, int[] newInterval) {
24-
List<int[]> insertedIntervals = new ArrayList<>(intervals.length);
25-
int i = 0;
26-
while (i < intervals.length && intervals[i][0] <= newInterval[0]) {
27-
insertedIntervals.add(intervals[i++]);
23+
// runtime: O(N)
24+
// space: O(N)
25+
public static int[][] insert(int[][] intervals1, int[] newInterval) {
26+
if (intervals1 == null || intervals1.length < 1) {
27+
return new int[][] {newInterval};
2828
}
2929

30-
insertedIntervals.add(newInterval);
31-
32-
while (i < intervals.length) {
33-
insertedIntervals.add(intervals[i++]);
34-
}
35-
36-
LinkedList<int[]> mergedIntervals = new LinkedList<>();
37-
38-
for (int[] interval : insertedIntervals) {
39-
if (mergedIntervals.isEmpty() || mergedIntervals.getLast()[1] < interval[0]) {
40-
mergedIntervals.add(interval);
41-
continue;
30+
int[][] intervals2 = new int[][]{newInterval};
31+
int i = 0, j = 0, n = intervals1.length, m = 1;
32+
int[] interval = intervals1[i][0] > intervals2[j][0] ? intervals2[j++] : intervals1[i++];
33+
List<int[]> result = new ArrayList<>();
34+
35+
while (i < n || j < m) {
36+
int[] next;
37+
if (i < n && j < m) {
38+
next = intervals1[i][0] > intervals2[j][0] ? intervals2[j++] : intervals1[i++];
39+
} else if (i < n) {
40+
next = intervals1[i++];
41+
} else {
42+
next = intervals2[j++];
43+
}
44+
if (next[0] <= interval[1]) {
45+
interval[1] = Math.max(interval[1], next[1]);
46+
} else {
47+
result.add(interval);
48+
interval = next;
4249
}
43-
44-
int[] prev = mergedIntervals.removeLast();
45-
mergedIntervals.add(new int[]{prev[0], Math.max(prev[1], interval[1])});
4650
}
4751

52+
result.add(interval);
4853

49-
return mergedIntervals.toArray(new int[0][0]);
54+
return result.toArray(new int[0][0]);
5055
}
5156

5257
/*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
6+
// https://leetcode.com/problems/length-of-last-word/submissions/
7+
public class LengthOfLastWord {
8+
9+
// runtime: O(N)
10+
// space: O(N)
11+
public int lengthOfLastWord(String s) {
12+
if (s == null || s.length() < 1) {
13+
return 0;
14+
}
15+
16+
String[] words = s.trim().split(" ");
17+
Collections.reverse(Arrays.asList(words));
18+
for (String w : words) {
19+
if (!w.isEmpty()) {
20+
return w.length();
21+
}
22+
}
23+
24+
return 0;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
}
30+
31+
}

src/problems/leetcode/MaximumSubarray.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ public static void main(String[] args) {
1010
System.out.println(maxSubArray(nums));
1111
}
1212

13+
// runtime: O(N)
14+
// space: O(1)
1315
public static int maxSubArray(int[] nums) {
16+
if (nums == null || nums.length < 1) {
17+
return 0;
18+
}
19+
1420
int maxSum = nums[0];
1521
for (int i = 1; i < nums.length; i++) {
16-
int s = nums[i] + nums[i - 1];
17-
if (nums[i] < s) {
18-
nums[i] = s;
19-
}
20-
21-
if (nums[i] > maxSum) {
22-
maxSum = nums[i];
23-
}
22+
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
23+
maxSum = Math.max(maxSum, nums[i]);
2424
}
2525

2626
return maxSum;

src/problems/leetcode/MergeIntervals.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
*/
1111
public class MergeIntervals {
1212

13+
// runtime: O(NlgN)
14+
// space: O(N)
1315
public static int[][] merge(int[][] intervals) {
1416
if (intervals == null || intervals.length == 0) {
1517
return new int[0][0];
1618
}
1719

20+
// sort by start time then end time
1821
Comparator<Object> c = Comparator.comparingInt(interval -> ((int[]) interval)[0])
1922
.thenComparingInt(interval -> ((int[]) interval)[1]);
2023
Arrays.sort(intervals, c);

src/problems/leetcode/MinimumPathSum.java

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

3+
import java.util.Arrays;
4+
35
/**
46
* https://leetcode.com/problems/minimum-path-sum/
57
*/
@@ -11,27 +13,27 @@ public static void main(String[] args) {
1113
}
1214

1315
public static int minPathSum(int[][] grid) {
14-
if (grid == null || grid.length == 0 || grid[0].length == 0) {
16+
if (grid == null || grid.length == 0) {
1517
return 0;
1618
}
17-
1819
int m = grid.length, n = grid[0].length;
19-
int[][] dp = new int[m + 1][n + 1];
20-
for (int i = n - 1; i >= 0; i--) {
21-
dp[m - 1][i] = grid[m - 1][i] + dp[m - 1][i + 1];
20+
for (int i = 1; i < m; i++) {
21+
grid[i][0] += grid[i - 1][0];
2222
}
23-
24-
for (int i = m - 1; i >= 0; i--) {
25-
dp[i][n - 1] = grid[i][n - 1] + dp[i + 1][n - 1];
23+
for (int j = 1; j < n; j++) {
24+
grid[0][j] += grid[0][j - 1];
2625
}
27-
28-
for (int i = m - 2; i >= 0; i--) {
29-
for (int j = n - 2; j >= 0; j--) {
30-
dp[i][j] = grid[i][j] + Math.min(dp[i + 1][j], dp[i][j + 1]);
26+
for (int i = 1; i < m; i++) {
27+
for (int j = 1; j < n; j++) {
28+
grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
3129
}
3230
}
3331

34-
return dp[0][0];
32+
for (int[] row : grid) {
33+
System.out.println(Arrays.toString(row));
34+
}
35+
36+
return grid[m - 1][n - 1];
3537
}
3638

3739
/*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package problems.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// https://leetcode.com/problems/permutation-sequence/solutions/
7+
8+
public class PermutationSequence {
9+
10+
// https://leetcode.com/problems/permutation-sequence/solutions/22507/explain-like-i-m-five-java-solution-in-o-n/
11+
12+
// runtime: O(N^2)
13+
public String getPermutation(int n, int k) {
14+
List<Integer> numbers = new ArrayList<>();
15+
int[] factorial = new int[n + 1];
16+
StringBuilder sb = new StringBuilder();
17+
18+
// create an array of factorial lookup
19+
int sum = 1;
20+
factorial[0] = 1;
21+
for (int i = 1; i <= n; i++) {
22+
sum *= i;
23+
factorial[i] = sum;
24+
}
25+
// factorial[] = {1, 1, 2, 6, 24, ... n!}
26+
27+
// create a list of numbers to get indices
28+
for (int i = 1; i <= n; i++) {
29+
numbers.add(i);
30+
}
31+
// numbers = {1, 2, 3, 4}
32+
33+
k--;
34+
for (int i = 1; i <= n; i++) {
35+
int index = k / factorial[n - i];
36+
sb.append(String.valueOf(numbers.get(index)));
37+
numbers.remove(index);
38+
k -= index * factorial[n - i];
39+
}
40+
41+
return String.valueOf(sb);
42+
}
43+
44+
}

src/problems/leetcode/RotateList.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ private static class ListNode {
1515

1616
@Override
1717
public String toString() {
18-
return "ListNode{" +
19-
"val=" + val +
20-
", next=" + next +
21-
'}';
18+
return String.valueOf(val) + (next != null ? " -> " + next : "");
2219
}
2320
}
2421

@@ -61,14 +58,54 @@ public static void main(String[] args) {
6158
ListNode head = new ListNode(1);
6259
head.next = new ListNode(2);
6360
head.next.next = new ListNode(3);
64-
head.next.next.next = new ListNode(4);
65-
head.next.next.next.next = new ListNode(5);
61+
// head.next.next.next = new ListNode(4);
62+
// head.next.next.next.next = new ListNode(5);
6663

67-
int k = 5;
6864

69-
ListNode newHead = new RotateList().rotateRight(head, k);
65+
int k = 4;
66+
67+
ListNode newHead = new RotateList().rotateRight2(head, k);
7068
System.out.println(newHead);
7169
}
7270

71+
public static ListNode rotateRight2(ListNode head, int k) {
72+
if (head == null || head.next == null || k < 1) {
73+
return head;
74+
}
75+
76+
// 1 -> 2 -> 3 4 -> 5
77+
// k=2
78+
// n=1
79+
// h
80+
// t
81+
// n=5 t
82+
// n=1
83+
// nt
84+
// nh
85+
86+
int n = 1;
87+
ListNode tail = head;
88+
while (tail.next != null) {
89+
n++;
90+
tail = tail.next;
91+
}
92+
93+
k %= n;
94+
if (k == 0) {
95+
return head;
96+
}
97+
n -= k;
98+
ListNode newTail = head;
99+
while (--n > 0) {
100+
newTail = newTail.next;
101+
}
102+
103+
ListNode newHead = newTail.next;
104+
newTail.next = null;
105+
tail.next = head;
106+
107+
return newHead;
108+
}
109+
73110

74111
}

0 commit comments

Comments
 (0)