Skip to content

Commit a29028c

Browse files
committed
more problems and solutions
1 parent 16faf46 commit a29028c

16 files changed

+358
-29
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ HELP.md
3333
ensime-langserver.log
3434
.ensime_cache
3535

36-
target/
36+
target/
37+
bin/

src/main/java/array/Candy.java

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

33
public class Candy {
4+
/*
5+
There are N children standing in a line. Each child is assigned a rating value. You are
6+
giving candies to these children subjected to the following requirements:
7+
1. Each child must have at least one candy. 2. Children with a higher rating get
8+
more candies than their neighbors.
9+
What is the minimum candies you must give?
10+
*/
411
public static void main(String[] args) {
512

613
}
Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
package array;
22

3-
class ContainerCoord {
4-
float x;
5-
float y;
6-
public ContainerCoord(float x, float y) {
7-
this.x = x;
8-
this.y = y;
9-
}
10-
}
11-
123
public class ContainerWithMostWater {
4+
/*
5+
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate
6+
(i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai)
7+
and (i, 0). Find two lines, which together with x-axis forms a container, such that the
8+
container contains the most water.
9+
10+
Initially we can assume the result is 0. Then we scan from both sides.
11+
If leftHeight < rightHeight, move right and find a value that is greater than leftHeight.
12+
if leftHeight > rightHeight, move left and find a value that is greater than rightHeight.
13+
Additionally, keep tracking the max value.
14+
*/
1315
public static void main(String[] args) {
14-
ContainerCoord c1 = new ContainerCoord(0, 1);
15-
ContainerCoord c2 = new ContainerCoord(1, 2);
16-
ContainerCoord c3 = new ContainerCoord(2, 3);
17-
ContainerCoord c4 = new ContainerCoord(3, 2);
18-
ContainerCoord c5 = new ContainerCoord(4, 2.5f);
19-
ContainerCoord c6 = new ContainerCoord(5, 2);
20-
ContainerCoord c7 = new ContainerCoord(6, 4);
21-
ContainerCoord c8 = new ContainerCoord(7, 2);
16+
System.out.println(compute(new int[]{1,2,4,2,3,2,5,2}));
17+
System.out.println(compute(new int[]{1,1,3,2,4,1,4,2,2,3}));
18+
}
2219

23-
24-
System.out.println();
20+
public static int compute(int[] arr) {
21+
if (arr == null || arr.length < 2) {
22+
return 0;
23+
}
24+
int max = 0;
25+
int left = 0;
26+
int right = arr.length - 1;
27+
while (left < right) {
28+
max = Math.max(max, (right - left) * Math.min(arr[left], arr[right]));
29+
if (arr[left] <arr[right]) {
30+
left++;
31+
} else {
32+
right--;
33+
}
34+
}
35+
return max;
2536
}
26-
2737
}

src/main/java/array/BinarySearchOutOfOrder.java renamed to src/main/java/array/ElementMatchingFunction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import java.util.*;
44

5-
/* Find index in a binary search array where value at the index is same as index.
5+
/* Find elements in an array which satisfied given function definition:
6+
Function will return true for value x, if all numbers on left side of x are small and all number on the right side of x are greater.
67
78
From: https://www.geeksforgeeks.org/google-interview-experience-for-sde-1/
89
Example: [ 4, 3, 1, 5, 7, 6, 10]
@@ -11,7 +12,7 @@
1112
Space Complexity: O(n)
1213
*/
1314

14-
public class BinarySearchOutOfOrder {
15+
public class ElementMatchingFunction {
1516
public static void main(String[] args) {
1617

1718
int[] input = {4, 3, 1, 5, 7, 6, 10};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package array;
2+
3+
public class MinimumSizeSubArraySum {
4+
/* Given an array of integers and a integer s, find the length of the minimum subarray whose sum is greater or same as s.
5+
6+
For example, int array = 2,3,1,2,4,3, and S = 7, result would be 2 (for subarray 4,3)
7+
*/
8+
public static void main(String args[ ]) {
9+
System.out.println( compute(new int[]{2,3,1,2,4,3}, 7) );
10+
}
11+
12+
private static int compute(int[] arr, int s) {
13+
int min = Integer.MAX_VALUE;
14+
int l = 0, r = 0;
15+
int sum = arr[l];
16+
if (sum >= s) {
17+
min = 1;
18+
r = 1;
19+
sum = sum + arr[r];
20+
}
21+
do {
22+
System.out.println("l: " + l + ", r: " + r + ", sum: " + sum);
23+
if (sum >= s) {
24+
if (r - l + 1 < min) {
25+
min = r - l + 1;
26+
}
27+
sum = sum - arr[l];
28+
l++;
29+
} else {
30+
r++;
31+
if (r < arr.length) {
32+
sum = sum + arr[r];
33+
}
34+
}
35+
} while (l < r && r < arr.length);
36+
return (min == Integer.MAX_VALUE)? 0: min;
37+
}
38+
}

src/main/java/array/PairWithGivenSum.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String args[]) {
1919

2020
int sum = sc.nextInt();
2121

22-
Stack<String> res = compute(n, arr, sum);
22+
Stack<String> res = compute2(n, arr, sum);
2323
if (res.empty()) {
2424
System.out.println("-1");
2525
} else {
@@ -30,6 +30,7 @@ public static void main(String args[]) {
3030
}
3131
}
3232

33+
/* Using OBhashmap */
3334
public static Stack<String> compute(int n, int[] arr, int sum) {
3435
Stack<String> res = new Stack<>();
3536
int resIndex = 0;
@@ -43,5 +44,25 @@ public static Stack<String> compute(int n, int[] arr, int sum) {
4344
}
4445
return res;
4546
}
47+
48+
/* If input is sorted */
49+
public static Stack<String> compute2(int n, int[] arr, int sum) {
50+
Stack<String> res = new Stack<>();
51+
int i = 0;
52+
int j = arr.length - 1;
53+
while (i<j) {
54+
int x = arr[i] + arr[j];
55+
if (x < sum) {
56+
i++;
57+
} else if (x > sum) {
58+
j--;
59+
} else {
60+
res.push(arr[i] + ", " + arr[j]);
61+
i++;
62+
j--;
63+
}
64+
}
65+
return res;
66+
}
4667
}
4768

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
public class ThreeSumClosest {
6+
/*
7+
Given an array S of n integers, find three integers in S such that the sum is closest to a
8+
given number, target. Return the sum of the three integers. You may assume that each
9+
input would have exactly one solution.
10+
11+
For example, given array S = {-1 2 1 -4}, and target = 1.
12+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
13+
*/
14+
public static void main(String[] args) {
15+
System.out.println ( threeSum(new int[]{ -1, 0, 1, 2, -1, -4}, 4));
16+
}
17+
18+
private static int threeSum(int[] input, int target) {
19+
Arrays.sort(input);
20+
int min = Integer.MAX_VALUE;
21+
int result = 0;
22+
23+
for(int i=0;i<input.length-2;i++) {
24+
int start = i+1;
25+
int end = input.length-1;
26+
while (start < end) {
27+
int sum = input[i] + input[start] + input[end];
28+
int diff = Math.abs(sum - target);
29+
if (diff == 0) {
30+
return sum;
31+
}
32+
if (diff < min) {
33+
min = diff;
34+
result = sum;
35+
}
36+
if (sum < target) {
37+
start++;
38+
} else {
39+
end--;
40+
}
41+
}
42+
}
43+
return result;
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package array;
2+
3+
public class TrappingRainWater {
4+
/*
5+
Given n non-negative integers representing an elevation map where the width of each
6+
bar is 1, compute how much water it is able to trap after raining.
7+
For example, given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
8+
*/
9+
public static void main(String[] args) {
10+
System.out.println(compute(new int[]{1,2,4,2,3,2,5,2}));
11+
}
12+
13+
public static int compute(int[] arr) {
14+
if (arr == null || arr.length < 2) {
15+
return 0;
16+
}
17+
//TODO
18+
return 0;
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dynamicprogramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class BalloonBurst {
7+
public static void main(String[] args) {
8+
System.out.println(maxCoinsNaive(new int[]{3,5,8}));
9+
}
10+
11+
public static int maxCoinsNaive(int[] nums) {
12+
List<Integer> balloons = new ArrayList<>();
13+
for (int i = 0; i < nums.length; i++)
14+
balloons.add(nums[i]);
15+
return maxCoinNaiveRec(balloons);
16+
}
17+
18+
public static int maxCoinNaiveRec(List<Integer> balloons) {
19+
int max = 0;
20+
int listSize = balloons.size();
21+
int i = 0;
22+
while(i < listSize) {
23+
int left = (i == 0) ? 1 : balloons.get(i - 1);
24+
int right = (i == listSize - 1) ? 1 : balloons.get(i + 1);
25+
int n = left * balloons.get(i) * right;
26+
List<Integer> tmp = new ArrayList<>(balloons);
27+
tmp.remove(i);
28+
max = Math.max(max, n + maxCoinNaiveRec(tmp));
29+
i++;
30+
}
31+
System.out.println("list:" + balloons + ", max:" + max);
32+
return max;
33+
}
34+
35+
}

src/main/java/dynamicprogramming/Fibonacci.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static void main(String[] args) {
1818
}
1919

2020
void initialize() {
21-
for (int i = 0; i < MAX; i++) {
22-
lookup[i] = NIL;
21+
for (int i = 0; i < MAX; i++) {
22+
lookup[i] = NIL;
2323
}
2424
}
2525

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dynamicprogramming;
2+
3+
public class LongestPalindromicSubString {
4+
/*
5+
https://leetcode.com/problems/longest-palindromic-substring/
6+
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
7+
Example 1:
8+
9+
Input: "babad"
10+
Output: "bab"
11+
Note: "aba" is also a valid answer.
12+
13+
Example 2:
14+
15+
Input: "cbbd"
16+
Output: "bb"
17+
*/
18+
public static void main(String[] args) {
19+
LongestPalindromicSubString instance = new LongestPalindromicSubString();
20+
System.out.println(instance.longestPalindrome("babad"));
21+
System.out.println(instance.longestPalindrome("cbbd"));
22+
}
23+
24+
public String longestPalindrome(String s) {
25+
return null;
26+
}
27+
}

src/main/java/dynamicprogramming/SharingChocolateBar.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class SharingChocolateBar {
1616
* sweetness level 7, 7, 9, 9 respectively)
1717
*/
1818
public static void main(String[] args) {
19-
19+
int numBars = 7;
20+
int[] sweetness = {1, 2, 4, 7, 3, 6, 9};
21+
2022
}
2123
}

src/main/java/graph/CloneGraph.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package graph;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
8+
public class CloneGraph {
9+
public static void main(String[] args) {
10+
GraphNode n4 = new GraphNode("4");
11+
GraphNode n3 = new GraphNode("3", Arrays.asList(n4));
12+
GraphNode n2 = new GraphNode("2", Arrays.asList(n3, n4));
13+
GraphNode n1 = new GraphNode("1", Arrays.asList(n2, n4));
14+
15+
LinkedList<GraphNode> queue = new LinkedList<>();
16+
Map<GraphNode, GraphNode> map = new HashMap<>();
17+
queue.add(n1);
18+
GraphNode newHead = new GraphNode(n1.label);
19+
map.put(n1, newHead);
20+
while (!queue.isEmpty()) {
21+
GraphNode n = queue.pop();
22+
System.out.println("Next: " + n.label);
23+
for (GraphNode neighborNode: n.neighbours) {
24+
if (map.getOrDefault(neighborNode, null) == null) {
25+
GraphNode newNode = new GraphNode(neighborNode.label);
26+
map.put(neighborNode, newNode);
27+
map.get(n).neighbours.add(newNode);
28+
queue.add(neighborNode);
29+
} else {
30+
map.get(n).neighbours.add(map.get(neighborNode));
31+
}
32+
}
33+
}
34+
}
35+
}

src/main/java/graph/GraphNode.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package graph;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
public class GraphNode {
7+
public String label;
8+
public List<GraphNode> neighbours;
9+
public boolean visited;
10+
public GraphNode next;
11+
12+
public GraphNode(String value) {
13+
label = value;
14+
neighbours = new ArrayList<GraphNode>();
15+
}
16+
17+
public GraphNode(String value, List<GraphNode> n) {
18+
label = value;
19+
neighbours = n;
20+
}
21+
}

0 commit comments

Comments
 (0)