Skip to content

Commit d2904be

Browse files
committed
More problems
1 parent f98c688 commit d2904be

18 files changed

+420
-49
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ repositories {
1919
dependencies {
2020
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
2121
compile group: 'edu.princeton.cs', name: 'algs4', version: '1.0.4'
22+
implementation 'org.apache.commons:commons-lang3:3.11'
23+
implementation 'com.google.guava:guava:29.0-jre'
2224
testCompile group: 'junit', name: 'junit', version: '4.12'
2325
}
2426

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
<artifactId>algs4</artifactId>
2929
<version>1.0.4</version>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.apache.commons</groupId>
33+
<artifactId>commons-lang3</artifactId>
34+
<version>3.11</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.guava</groupId>
38+
<artifactId>guava</artifactId>
39+
<version>29.0-jre</version>
40+
<type>bundle</type>
41+
</dependency>
3142
<dependency>
3243
<groupId>junit</groupId>
3344
<artifactId>junit</artifactId>
@@ -135,4 +146,4 @@
135146
</plugin>
136147
</plugins>
137148
</build>
138-
</project>
149+
</project>

src/main/java/array/AdvanceToEnd.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static boolean compute(List<Integer> input) {
1414
return compute(input, input.size() - 1);
1515
}
1616

17+
//Recursive - inefficient, can use dynamic programming
1718
public static boolean compute(List<Integer> input, int endIndex) {
1819
if (endIndex == 0) {
1920
return true;
@@ -27,4 +28,13 @@ public static boolean compute(List<Integer> input, int endIndex) {
2728
}
2829
return false;
2930
}
31+
32+
//Efficient
33+
public static boolean computeItr(List<Integer> input) {
34+
int furthestSoFar = 0, lastindex = input.size();
35+
for (int i = 0; i < input.size() && i <= furthestSoFar; i++) {
36+
furthestSoFar = Math.max(furthestSoFar, i + input.get(i));
37+
}
38+
return furthestSoFar >= lastindex;
39+
}
3040
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class JumpOverNumbers {
7+
public static void main(String[] args) {
8+
List<Integer> input = Arrays.asList(3, 4, 1, 2, 5, 6, 9, 0, 1, 2, 3, 1);
9+
System.out.println(compute(input));
10+
}
11+
12+
public static int compute(List<Integer> input) {
13+
int idx = 0, jumps = 0;
14+
while (idx < input.size()) {
15+
int val = input.get(idx);
16+
if (val == 0) {
17+
jumps = -1;
18+
break;
19+
} else {
20+
idx += val;
21+
jumps++;
22+
}
23+
}
24+
return jumps;
25+
}
26+
}

src/main/java/array/KLargestElements.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package array;
22

3+
import java.util.ArrayList;
34
import java.util.Comparator;
45
import java.util.List;
56
import java.util.PriorityQueue;
@@ -11,9 +12,11 @@ public static void main(String[] args) {
1112
int k = 3;
1213
int[] input = { 1, 23, 12, 9, 30, 2, 50};
1314
System.out.println(compute(input, k));
15+
System.out.println(compute2(input, k));
1416
}
1517

1618
private static List<Integer> compute(int[] nums, int k) {
19+
//Max Queue.
1720
PriorityQueue<Integer> pQueue = new PriorityQueue<>(new Comparator<Integer>() {
1821

1922
@Override
@@ -31,4 +34,15 @@ public int compare(Integer o1, Integer o2) {
3134
}
3235
return IntStream.range(1, k+1).map(i -> pQueue.poll()).boxed().collect(Collectors.toList());
3336
}
37+
38+
private static List<Integer> compute2(int[] nums, int k) {
39+
PriorityQueue<Integer> pq = new PriorityQueue<>(k);
40+
for (int i = 0; i< nums.length; i++) {
41+
pq.add(nums[i]);
42+
if (pq.size() > k) {
43+
pq.poll();
44+
}
45+
}
46+
return new ArrayList<Integer>(pq);
47+
}
3448
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class MaximumSubarraySum {
7+
/*
8+
* The maximum subarray problem is a task to find the series of contiguous
9+
* elements with the maximum sum in any given array.
10+
*
11+
* For instance, in the below array, the highlighted subarray has the maximum
12+
* sum(6):
13+
-3 1 -8 4 -1 2 1 -5 5
14+
| |
15+
*/
16+
public static void main(String[] args) {
17+
System.out.println("Max sum: " + compute(Arrays.asList(-3, 1, -8, 4, -1, 2, 1, -5, 5)));
18+
}
19+
20+
//Kadane Algorithm
21+
public static int compute(List<Integer> input) {
22+
int maxSoFar = input.get(0);
23+
int maxEndHere = maxSoFar;
24+
int start = 0, end = 0;
25+
for (int i = 1;i < input.size(); i++) {
26+
if (input.get(i) > maxEndHere + input.get(i)) {
27+
maxEndHere = input.get(i);
28+
start = i;
29+
} else {
30+
maxEndHere = maxEndHere + input.get(i);
31+
}
32+
if (maxEndHere > maxSoFar) {
33+
maxSoFar = maxEndHere;
34+
end = i;
35+
}
36+
}
37+
System.out.printf("Max sum sub array found with indices %d, %d\n", start, end);
38+
return maxSoFar;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
public class NumberOfReversePairs {
6+
public static void main(String[] args) {
7+
int[] input = {10, 3, 4, 2, 5, 7, 9, 11};
8+
System.out.println("Result: " + divide(input, 0, input.length-1));
9+
}
10+
11+
public static int divide(int[] input, int low, int high) {
12+
if (low>=high) {
13+
return 0;
14+
}
15+
int mid = low + (high - low)/2;
16+
int leftInversions = divide(input, low, mid);
17+
int rightInversions = divide(input, mid, high);
18+
int mergeInversions = mergeAndCount(input, low, mid, high);
19+
return leftInversions + rightInversions + mergeInversions;
20+
}
21+
22+
public static int mergeAndCount(int[] input, int low, int mid, int high) {
23+
int[] left = Arrays.copyOfRange(input, low, mid + 1);
24+
int[] right = Arrays.copyOfRange(input, mid + 1, high + 1);
25+
int i = 0, j = 0, k = low, inversions = 0;
26+
while (i < left.length && j < right.length) {
27+
if (left[i] <= right[j]) {
28+
input[k++] = left[i++];
29+
} else {
30+
input[k++] = right[j++];
31+
inversions += (mid + 1) - (low + i);
32+
}
33+
}
34+
while (i<left.length) {
35+
input[k++] = left[i++];
36+
}
37+
while (j<right.length) {
38+
input[k++] = left[j++];
39+
}
40+
return inversions;
41+
}
42+
}

src/main/java/dynamicprogramming/MoneyChange.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,16 @@ private static int recursiveDP(int[] coins, int[] cache, int amt) {
6666
private static int iterativeDP(int[] coins, int[] cache, int amt) {
6767
cache[0] = 0;
6868
for (int coinAmt = 1; coinAmt <= amt; coinAmt++) {
69-
if (cache[coinAmt] < 0) {
70-
int minCoins = Integer.MAX_VALUE;
71-
for (int coin: coins) {
72-
if (coinAmt - coin >= 0) {
73-
int currCoins = cache[coinAmt - coin] + 1;
74-
if (currCoins < minCoins) {
75-
minCoins = currCoins;
76-
}
69+
int minCoins = Integer.MAX_VALUE;
70+
for (int coin : coins) {
71+
if (coinAmt - coin >= 0) {
72+
int currCoins = cache[coinAmt - coin] + 1;
73+
if (currCoins < minCoins) {
74+
minCoins = currCoins;
7775
}
7876
}
79-
cache[coinAmt] = minCoins;
8077
}
78+
cache[coinAmt] = minCoins;
8179
}
8280
return cache[amt];
8381
}

src/main/java/dynamicprogramming/SquareSubMatrix.java

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

2222
System.out.println("Recursive DP: " + recursiveDP(arr));
2323

24-
System.out.println("Iterative DP: " + iterativeDP());
24+
System.out.println("Iterative DP: " + iterativeDP(arr));
2525
}
2626

2727
private static int recursive(boolean arr[][]) {
@@ -68,7 +68,24 @@ private static int recursiveDPI(boolean[][] arr, int x, int y, int[][] cache) {
6868
return cache[x][y];
6969
}
7070

71-
private static int iterativeDP() {
72-
return 0;
71+
private static int iterativeDP(boolean arr[][]) {
72+
int max = 0;
73+
int[][] cache = new int[arr.length][arr[0].length];
74+
for (int i=0;i<cache.length;i++) {
75+
for (int j=0;j<cache[0].length;i++) {
76+
// If we’re in the first row/column then
77+
// the value is just 1 if that cell is
78+
// true and 0 otherwise. In other rows and
79+
// columns need to look up and to the left
80+
if (i == 0 || j == 0) {
81+
cache[i][j] = arr[i][j] ? 1 : 0;
82+
} else if (arr[i][j]) {
83+
cache[i][j] = Math.min(Math.min(cache[i][j - 1], cache[i - 1][j]), cache[i - 1][j - 1]) + 1;
84+
}
85+
if (cache[i][j] > max)
86+
max = cache[i][j];
87+
}
88+
}
89+
return max;
7390
}
7491
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dynamicprogramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ZeroSumSubSequence {
7+
public static void main(String[] args) {
8+
System.out.println(compute(new int[] { 1, 2, -5, 1, 2, -1 }));
9+
}
10+
11+
public static List<Integer> compute(int[] arr) {
12+
return compute(arr, 0, 0);
13+
}
14+
15+
public static List<Integer> compute(int[] arr, int index, int sum) {
16+
List<Integer> ret = new ArrayList<>();
17+
if (index == arr.length)
18+
return ret;
19+
if (sum - arr[index] == 0) {
20+
ret.add(arr[index]);
21+
return ret;
22+
}
23+
List<Integer> res1 = compute(arr, index + 1, sum - arr[index]);
24+
if (res1.size() > 0) {
25+
ret.add(arr[index]);
26+
ret.addAll(res1);
27+
}
28+
List<Integer> res2 = compute(arr, index + 1, sum);
29+
if (res2.size() > 0) {
30+
return res2;
31+
}
32+
return ret;
33+
}
34+
35+
/*public static List<Integer> iterative(int[] arr) {
36+
int[][] sum = new int[arr.length][arr.length];
37+
int s = 0, e = 0;
38+
for (int i = 0; i < arr.length; i++) {
39+
sum[i][i] = arr[i];
40+
}
41+
for (int i = 0; i < arr.length; i++) {
42+
for (int j = i + 1; j < arr.length; j++) {
43+
}
44+
}
45+
}*/
46+
}

src/main/java/dynamicprogramming/ZeroSumSubarray.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/linkedlist/DeleteMiddleNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Node deleteMiddleNode(Node n) {
3838
temp = temp.next.next;
3939
} else {
4040
if (temp.next != null) { //temp is at an odd numbered node and next/final node is even, so no mid node in this list.
41-
nodeBeforeMid = null;
41+
nodeBeforeMid = null;
4242
}
4343
temp = null;
4444
}

0 commit comments

Comments
 (0)