Skip to content

Commit f98c688

Browse files
committed
More problem solving
1 parent 18777f7 commit f98c688

16 files changed

+543
-0
lines changed

src/main/java/array/AdvanceToEnd.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class AdvanceToEnd {
7+
public static void main(String[] args) {
8+
System.out.println(compute(Arrays.asList(3, 3, 1, 0, 2, 0, 1)));
9+
System.out.println(compute(Arrays.asList(3, 2, 0, 0, 2, 0, 1)));
10+
System.out.println(compute(Arrays.asList(2,3,5,5,7,11,11,11,13)));
11+
}
12+
13+
public static boolean compute(List<Integer> input) {
14+
return compute(input, input.size() - 1);
15+
}
16+
17+
public static boolean compute(List<Integer> input, int endIndex) {
18+
if (endIndex == 0) {
19+
return true;
20+
}
21+
for (int i = endIndex - 1; i >= 0; i--) {
22+
if (input.get(i) >= (endIndex - i)) {
23+
if (compute(input, i)) {
24+
return true;
25+
}
26+
}
27+
}
28+
return false;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
public class BackspaceStringCompare {
6+
public static void main(String[] args) {
7+
String s = "ab#c", t = "ad#c";
8+
System.out.println(compute(s, t));
9+
10+
System.out.println(compute("ab#d", "ad#b"));
11+
}
12+
13+
public static boolean compute(String s, String t) {
14+
return (getFinal(s).equals(getFinal(t)));
15+
}
16+
17+
public static String getFinal(String s) {
18+
int i = 0, x = -1;
19+
char[] newS = new char[s.length()];
20+
Arrays.fill(newS, ' ');
21+
while (i < s.length()) {
22+
if (s.charAt(i) == '#') {
23+
if (x >= 0) {
24+
newS[x--] = ' ';
25+
}
26+
} else {
27+
newS[++x] = s.charAt(i);
28+
}
29+
i++;
30+
}
31+
return new String(newS);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class BuySellStockTwice {
8+
public static void main(String[] args) {
9+
System.out.println(compute(Arrays.asList(12.0,11.0,13.0,9.0,12.0,8.0,14.0,13.0,15.0)));
10+
}
11+
12+
public static Double compute(List<Double> prices) {
13+
double maxTotalProfit = 0.0;
14+
List<Double> firstBuySellProfits = new ArrayList<>();
15+
16+
double minPriceSoFar = Double.MAX_VALUE;
17+
//compute first buy-sell profit by each day
18+
for (int i = 0; i < prices.size();++i) {
19+
minPriceSoFar = Math.min(minPriceSoFar, prices.get(i));
20+
maxTotalProfit = Math.max(maxTotalProfit, prices.get(i) - minPriceSoFar);
21+
firstBuySellProfits.add(maxTotalProfit);
22+
}
23+
// Backward phase. For each day, find the maximum profit if we make
24+
// the second buy on that day.
25+
double maxPriceSoFar = Double.MIN_VALUE;
26+
for (int i = prices.size() - 1; i > 0; --i) {
27+
maxPriceSoFar = Math.max(maxPriceSoFar , prices.get(i));
28+
maxTotalProfit = Math.max(maxTotalProfit , maxPriceSoFar - prices.get(i)
29+
+ firstBuySellProfits.get(i - 1));
30+
}
31+
return maxTotalProfit;
32+
}
33+
}

src/main/java/array/GroupEvenOdd.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package array;
2+
3+
public class GroupEvenOdd {
4+
public static void main(String[] args) {
5+
int[] input = {1,2,3,4,5,6,7};
6+
compute(input);
7+
System.out.print("[");
8+
for (int i = 0;i<input.length;i++) {
9+
System.out.print(input[i]);
10+
}
11+
System.out.println("]");
12+
}
13+
14+
public static void compute(int[] input) {
15+
int i = 0, s = 0, e = input.length - 1;
16+
while (i < e) {
17+
int temp = input[i];
18+
if (input[i] % 2 == 0) {
19+
input[i] = input[s];
20+
input[s] = temp;
21+
s++;
22+
} else {
23+
input[i] = input[e];
24+
input[e] = temp;
25+
e--;
26+
}
27+
i++;
28+
}
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
public class IncrementInteger {
9+
public static void main(String[] args) {
10+
System.out.println(compute(Stream.of(1,2,3).collect(Collectors.toList())));
11+
System.out.println(compute(Stream.of(9,9,9).collect(Collectors.toList())));
12+
}
13+
14+
public static List<Integer> compute(List<Integer> input) {
15+
int i = input.size() - 1;
16+
while (i>=0) {
17+
int val = input.get(i) + 1;
18+
if (val == 10) {
19+
input.set(i, 0);
20+
} else {
21+
input.set(i, val);
22+
break;
23+
}
24+
i--;
25+
}
26+
if (i == -1) {
27+
input.set(0, 0);
28+
input.add(0, 1);
29+
}
30+
return input;
31+
}
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
public class IntersectionOfTwoArrays {
7+
public static void main(String[] args) {
8+
int[] nums1 = { 1, 2, 3, 4 };
9+
int[] nums2 = { 3, 5, 6, 7 };
10+
System.out.println(compute(nums1, nums2));
11+
}
12+
13+
//Time = O(n), space=O(n)
14+
public static Integer[] compute(int[] nums1, int[] nums2) {
15+
HashSet<Integer> set1 = new HashSet<>();
16+
for (int i: nums1) {
17+
set1.add(i);
18+
}
19+
HashSet<Integer> intersectionSet = new HashSet<>();
20+
for (int i: nums2) {
21+
if (set1.contains(i)) {
22+
intersectionSet.add(i);
23+
}
24+
}
25+
return intersectionSet.toArray(Integer[]::new);
26+
}
27+
28+
public static Integer[] compute2(int[] nums1, int[] nums2) {
29+
Arrays.sort(nums2);
30+
HashSet<Integer> intersectionSet = new HashSet<>();
31+
for (int i=0;i<nums1.length;i++) {
32+
if (Arrays.binarySearch(nums2, nums1[i]) > -1) {
33+
intersectionSet.add(nums1[i]);
34+
}
35+
}
36+
return intersectionSet.toArray(Integer[]::new);
37+
}
38+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class IntervalListIntersections {
7+
public static void main(String[] args) {
8+
Interval[] a = {new Interval(0,2), new Interval(5,10), new Interval(13,23), new Interval(24,25)};
9+
Interval[] b = {new Interval(1,5), new Interval(8,12), new Interval(15, 24), new Interval(25,26)};
10+
11+
Interval[] output = compute(a, b);
12+
System.out.println("[");
13+
for (int i = 0; i < output.length;i++) {
14+
System.out.println(String.format("(%d, %d), ", output[i].s, output[i].e));
15+
}
16+
System.out.println("]");
17+
}
18+
19+
public static Interval[] compute(Interval[] a, Interval[] b) {
20+
List<Interval> result = new ArrayList<>();
21+
int aIdx = 0, bIdx = 0;
22+
Interval addition = null;
23+
while (aIdx < a.length && bIdx < b.length) {
24+
int as = a[aIdx].s;
25+
int ae = a[aIdx].e;
26+
int bs = b[bIdx].s;
27+
int be = b[bIdx].e;
28+
if (ae < bs) {
29+
//if a is before b, then increment aIdx
30+
aIdx++;
31+
} else if (as > be) {
32+
//if a is after b, then increment bIdx
33+
bIdx++;
34+
} else if (as <= bs && ae >= bs && ae <= be) {
35+
//If rear of a overlaps with b
36+
addition = new Interval(bs, ae);
37+
aIdx++;
38+
} else if (as >= bs && ae <= be) {
39+
//If a is within b
40+
addition = new Interval(as, ae);
41+
aIdx++;
42+
} else if (bs <= as && be >= as && be <= ae) {
43+
//if rear of b overlaps with a
44+
addition = new Interval(as, be);
45+
bIdx++;
46+
} else if (bs >= as && be <= ae) {
47+
//if b is within a
48+
addition = new Interval(bs, be);
49+
bIdx++;
50+
}
51+
if (addition != null) {
52+
System.out.println("Adding: " + addition.s + ", " + addition.e);
53+
result.add(addition);
54+
addition = null;
55+
}
56+
}
57+
return result.toArray(Interval[]::new);
58+
}
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class PartitionOnPivot {
7+
public static void main(String[] args) {
8+
List<Integer> array = Arrays.asList(0,1,2,0,2,1,1);
9+
System.out.println(compute(array, 3));
10+
}
11+
12+
public static List<Integer> compute(List<Integer> input, int pivot) {
13+
int j = input.size() - 1, n = 0;
14+
int pv = input.get(pivot);
15+
while (n < j) {
16+
if (input.get(n) > pv) {
17+
int temp = input.get(n);
18+
input.set(n, input.get(j));
19+
input.set(j, temp);
20+
j--;
21+
} else {
22+
n++;
23+
}
24+
}
25+
return input;
26+
}
27+
}

src/main/java/array/Permutation.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public class Permutation {
11+
public static void main(String[] args) {
12+
System.out.println(compute(Stream.of(1,2,3).collect(Collectors.toList())));
13+
}
14+
15+
public static List<List<Integer>> compute(List<Integer> a) {
16+
return compute(a, 0, a.size() - 1);
17+
}
18+
19+
public static List<List<Integer>> compute(List<Integer> a, int s, int e) {
20+
if (s >= e) {
21+
List<Integer> newList = new ArrayList<>();
22+
newList.addAll(a);
23+
return Arrays.asList(newList);
24+
}
25+
List<List<Integer>> res = new ArrayList<>();
26+
for (int i = s; i <= e; i++) {
27+
swap(a, s, i);
28+
res.addAll(compute(a, s + 1, e));
29+
swap(a, i, s);
30+
}
31+
return res;
32+
}
33+
34+
public static void swap(List<Integer> a, int x, int y) {
35+
int temp = a.get(x);
36+
a.set(x, a.get(y));
37+
a.set(y, temp);
38+
}
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package array;
2+
3+
public class SearchInsertPosition {
4+
public static void main(String[] args) {
5+
6+
int[] input = { 1, 3, 5, 6 };
7+
System.out.println("Result for " + input + " is " + compute(input, 5));
8+
int[] input2={1,3,5,6};
9+
System.out.println("Result for " + input2 + " is " + compute(input2, 2));
10+
int[] input3 = { 1, 3, 5, 6 };
11+
System.out.println("Result for " + input3 + " is " + compute(input3, 7));
12+
int[] input4 = { 1, 3, 5, 6 };
13+
System.out.println("Result for " + input4 + " is " + compute(input4, 0));
14+
}
15+
16+
public static int compute(int[] input, int k) {
17+
return bs(input, k, 0, input.length - 1);
18+
}
19+
20+
public static int bs(int[] input, int k, int l, int r) {
21+
if (l>r) {
22+
return l;
23+
}
24+
if (input[l] == k) {
25+
return l;
26+
} else if (input[r] == k) {
27+
return r;
28+
} else {
29+
if (l == r) {
30+
if (k < input[l]) {
31+
return l;
32+
} else if (k > input[l]) {
33+
return l + 1;
34+
} else {
35+
return l;
36+
}
37+
} else {
38+
int m = l + (r - 1) / 2;
39+
if (input[m] == k) {
40+
return m;
41+
} else if (k < input[m]) {
42+
return bs(input, k, l, m - 1);
43+
} else {
44+
return bs(input, k, m + 1, r);
45+
}
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)