Skip to content

Commit 07f4c5b

Browse files
committed
revisit some easy / mid questions
1 parent 35911d4 commit 07f4c5b

11 files changed

+251
-85
lines changed

src/problems/leetcode/FourSum.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package problems.leetcode;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
8+
// https://leetcode.com/problems/4sum/description/
9+
public class FourSum {
10+
11+
public static void main(String[] args) {
12+
System.out.println(fourSum(new int[] { 1, 0, -1, 0, -2, 2 }, 0));
13+
System.out.println(fourSum(new int[] { 1000000000, 1000000000, 1000000000, 1000000000 }, -294967296));
14+
}
15+
16+
// runtime: O(N^3)
17+
public static List<List<Integer>> fourSum(int[] nums, int target) {
18+
// 0, 1, 2, 3, 4, 5
19+
// -2, -1, 0, 0, 1, 2
20+
// target = 0
21+
// i j
22+
// remainingTarget = 3
23+
// i j k l
24+
// i j k l
25+
// i j k l
26+
// i j k l
27+
28+
if (nums.length < 4) {
29+
return Collections.emptyList();
30+
}
31+
32+
List<List<Integer>> result = new ArrayList<>();
33+
Arrays.sort(nums);
34+
35+
for (int i = 0; i < nums.length; i++) {
36+
if (i != 0 && nums[i] == nums[i - 1]) {
37+
continue;
38+
}
39+
40+
for (int j = i + 1; j < nums.length; j++) {
41+
if (j > i + 1 && nums[j] == nums[j - 1]) {
42+
continue;
43+
}
44+
45+
long remaining = (long) target - (nums[i] + nums[j]);
46+
int k = j + 1, l = nums.length - 1;
47+
while (k < l) {
48+
int sum = nums[k] + nums[l];
49+
if (sum > remaining) {
50+
l--;
51+
} else if (sum < remaining) {
52+
k++;
53+
} else {
54+
result.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
55+
56+
k++;
57+
while (k < l && nums[k] == nums[k - 1]) {
58+
k++;
59+
}
60+
61+
l--;
62+
while (k < l && nums[l] == nums[l + 1]) {
63+
l--;
64+
}
65+
}
66+
}
67+
}
68+
}
69+
70+
return result;
71+
}
72+
73+
}

src/problems/leetcode/IntegerToRoman.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,48 @@ public static void main(String[] args) {
1010
System.out.println(intToRoman(num));
1111
}
1212

13+
private static String digitToString(int n, String s, String m, String l) {
14+
switch (n) {
15+
case 0:
16+
return "";
17+
case 1:
18+
return s;
19+
case 2:
20+
return s + s;
21+
case 3:
22+
return s + s + s;
23+
case 4:
24+
return s + m;
25+
case 5:
26+
return m;
27+
case 6:
28+
return m + s;
29+
case 7:
30+
return m + s + s;
31+
case 8:
32+
return m + s + s + s;
33+
case 9:
34+
return s + l;
35+
default:
36+
throw new IllegalArgumentException("Cannot convert " + n);
37+
}
38+
}
39+
1340
public static String intToRoman(int num) {
41+
if (num < 1 || num > 3999) {
42+
throw new IllegalArgumentException("Cannot convert " + num + " to Roman!");
43+
}
44+
45+
StringBuilder sb = new StringBuilder();
46+
sb.append(digitToString(num / 1000, "M", "", ""));
47+
sb.append(digitToString((num % 1000) / 100, "C", "D", "M"));
48+
sb.append(digitToString(((num % 100) / 10), "X", "L", "C"));
49+
sb.append(digitToString(num % 10, "I", "V", "X"));
50+
51+
return sb.toString();
52+
}
53+
54+
public static String intToRoman2(int num) {
1455
if (num < 1 || num > 3999) {
1556
throw new IllegalArgumentException("invalid num: " + num + " to convert!");
1657
}

src/problems/leetcode/LongestCommonPrefix.java

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

3+
import java.util.Optional;
4+
35
/**
46
* https://leetcode.com/problems/longest-common-prefix/
57
*/
68
public class LongestCommonPrefix {
79

810
public static void main(String[] args) {
9-
String[] strs = {"c", "c"};
11+
String[] strs = { "c", "c" };
1012
System.out.println(longestCommonPrefix(strs));
1113
}
1214

15+
// runtime: O(N)
16+
// space: O(1)
1317
public static String longestCommonPrefix(String[] strs) {
1418
if (strs == null || strs.length == 0) {
1519
return "";
1620
}
1721

1822
StringBuilder sb = new StringBuilder();
19-
20-
int i = 0;
21-
while (true) {
22-
if (i == strs[0].length()) {
23-
break;
24-
}
25-
26-
char c = strs[0].charAt(i);
27-
for (int j = 1; j < strs.length; j++) {
28-
String s = strs[j];
29-
if (i == s.length() || c != s.charAt(i)) {
30-
i = -1;
23+
for (int i = 0;; i++) {
24+
Optional<Character> c = Optional.empty();
25+
for (String s : strs) {
26+
if (i >= s.length()) {
27+
c = Optional.empty();
28+
break;
29+
} else if (c.isEmpty()) {
30+
c = Optional.of(s.charAt(i));
31+
} else if (c.get() != s.charAt(i)) {
32+
c = Optional.empty();
3133
break;
3234
}
3335
}
34-
35-
if (i != -1) {
36-
sb.append(c);
37-
i++;
38-
} else {
36+
if (c.isEmpty()) {
3937
break;
38+
} else {
39+
sb.append(c.get());
4040
}
4141
}
4242

src/problems/leetcode/MergeKSortedLists.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,42 @@ private static class ListNode {
1717
}
1818
}
1919

20-
private static class ListNodeHolder {
21-
final ListNode node;
22-
final int index;
23-
24-
ListNodeHolder(ListNode node, int index) {
25-
this.node = node;
26-
this.index = index;
20+
// runtime: O(NlgN) where N is number of lists
21+
// space: O(N)
22+
public static ListNode mergeKLists(ListNode[] lists) {
23+
if (lists == null || lists.length == 0) {
24+
return null;
2725
}
28-
}
2926

30-
public static ListNode mergeKLists(ListNode[] lists) {
31-
PriorityQueue<ListNodeHolder> heap = new PriorityQueue<>(Comparator.comparingInt(holder -> holder.node.val));
27+
PriorityQueue<ListNode> heap = new PriorityQueue<>(new Comparator<ListNode>() {
28+
public int compare(ListNode a, ListNode b) {
29+
return Integer.compare(a.val, b.val);
30+
}
31+
});
3232

33-
for (int i = 0; i < lists.length; i++) {
34-
ListNode node = next(lists, i);
33+
for (ListNode node : lists) {
3534
if (node != null) {
36-
heap.add(new ListNodeHolder(node, i));
35+
heap.add(node);
3736
}
3837
}
39-
38+
4039
ListNode head = null, tail = null;
41-
while (heap.size() > 0) {
42-
ListNodeHolder holder = heap.poll();
40+
while (!heap.isEmpty()) {
41+
ListNode node = heap.poll();
42+
ListNode next = node.next;
43+
node.next = null;
4344
if (head == null) {
44-
head = tail = holder.node;
45+
head = tail = node;
4546
} else {
46-
tail.next = holder.node;
47-
tail = holder.node;
47+
tail.next = node;
48+
tail = node;
4849
}
49-
50-
ListNode next = next(lists, holder.index);
5150
if (next != null) {
52-
heap.add(new ListNodeHolder(next, holder.index));
51+
heap.add(next);
5352
}
5453
}
55-
54+
5655
return head;
5756
}
5857

59-
private static ListNode next(ListNode[] lists, int index) {
60-
ListNode next = lists[index];
61-
if (next != null) {
62-
lists[index] = next.next;
63-
next.next = null;
64-
}
65-
66-
return next;
67-
}
6858
}

src/problems/leetcode/MergeTwoSortedLists.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ private static class ListNode {
1414
}
1515
}
1616

17+
// runtime: O(N)
18+
// space: O(1)
1719
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1820
if (l1 == null) {
1921
return l2;

src/problems/leetcode/RemoveNthNodeFromEndOfList.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public String toString() {
2222

2323
private int i;
2424

25+
// runtime: O(N)
26+
// space: O(N) due to recursive calls till tail
2527
public ListNode removeNthFromEnd(ListNode head, int n) {
2628
return remove(head, n);
2729
}

src/problems/leetcode/RomanToInteger.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ public static void main(String[] args) {
1111
}
1212

1313
public static int romanToInt(String s) {
14+
int num = 0, buf = 0;
15+
for (int i = 0, j = s.length(); i < j; i++) {
16+
int n = toArabicNumber(s.charAt(i));
17+
if (buf == 0) {
18+
buf = n;
19+
} else if (buf / n >= 10) {
20+
num += buf;
21+
buf = n;
22+
} else if (n <= buf) {
23+
buf += n;
24+
} else {
25+
num += (n - buf);
26+
buf = 0;
27+
}
28+
}
29+
30+
return num + buf;
31+
}
32+
33+
public static int romanToInt2(String s) {
1434
if (s == null || s.length() == 0) {
1535
return 0;
1636
}

src/problems/leetcode/SwapNodesInPairs.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,17 @@ public String toString() {
2222
}
2323
}
2424

25+
// runtime: O(N)
26+
// space: O(N) due to recursive call stack
2527
public static ListNode swapPairs(ListNode head) {
2628
if (head == null || head.next == null) {
2729
return head;
2830
}
2931

30-
ListNode prev = head, newHead = head.next, n1 = head.next.next;
31-
32-
newHead.next = head;
33-
head.next = n1;
34-
35-
while (n1 != null && n1.next != null) {
36-
ListNode n2 = n1.next;
37-
ListNode n3 = n1.next.next;
38-
prev.next = n2;
39-
n2.next = n1;
40-
n1.next = n3;
41-
prev = n1;
42-
n1 = n3;
43-
}
44-
45-
return newHead;
32+
ListNode next = head.next;
33+
head.next = swapPairs(next.next);
34+
next.next = head;
35+
return next;
4636
}
4737

4838
public static void main(String[] args) {

src/problems/leetcode/ThreeSum.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ public static void main(String[] args) {
1414
System.out.println(threeSum(nums));
1515
}
1616

17+
// runtime: O(N^2)
1718
public static List<List<Integer>> threeSum(int[] nums) {
1819
List<List<Integer>> indices = new ArrayList<>();
19-
Arrays.sort(nums);
20+
Arrays.sort(nums); // O(NlgN)
2021

21-
for (int i = 0, len = nums.length; i < len - 2; i++) {
22+
// O(N^2)
23+
for (int i = 0, len = nums.length; i < len - 2; i++) { // O(N)
2224
if (i != 0 && nums[i] == nums[i - 1]) {
2325
continue;
2426
}
2527

2628
int j = i + 1, k = len - 1;
27-
while (j < k) {
29+
while (j < k) { // O(N)
2830
if (nums[i] + nums[j] + nums[k] > 0) {
2931
k--;
3032
} else if (nums[i] + nums[j] + nums[k] < 0) {
@@ -48,4 +50,34 @@ public static List<List<Integer>> threeSum(int[] nums) {
4850
return indices;
4951
}
5052

53+
// runtime: O(N^3)
54+
public List<List<Integer>> threeSumBruteForce(int[] nums) {
55+
List<List<Integer>> indices = new ArrayList<>();
56+
Arrays.sort(nums);
57+
58+
for (int i = 0; i < nums.length; i++) {
59+
if (i > 0 && nums[i] == nums[i - 1]) {
60+
continue;
61+
}
62+
for (int j = i + 1; j < nums.length; j++) {
63+
if (j > i + 1 && nums[j] == nums[j - 1]) {
64+
continue;
65+
}
66+
for (int k = j + 1; k < nums.length; k++) {
67+
if (k > j + 1 && nums[k] == nums[k - 1]) {
68+
continue;
69+
} else if (nums[i] + nums[j] + nums[k] == 0) {
70+
List<Integer> l = new ArrayList();
71+
l.add(nums[i]);
72+
l.add(nums[j]);
73+
l.add(nums[k]);
74+
indices.add(l);
75+
}
76+
}
77+
}
78+
}
79+
80+
return indices;
81+
}
82+
5183
}

0 commit comments

Comments
 (0)