Skip to content

Commit cbea537

Browse files
authored
Add files via upload
1 parent 6d54e8d commit cbea537

16 files changed

+655
-0
lines changed

CoinChange.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dp;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given a value N, if we want to make change for N cents, and we have infinite supply of each of
7+
* S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins
8+
* doesn’t matter.
9+
* For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}.
10+
* So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2},
11+
* {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.
12+
*/
13+
public class CoinChange {
14+
15+
public static void main(String[] args) {
16+
rdp(5, new int[]{1, 2, 3});
17+
rdp(10, new int[]{2, 5, 3, 6});
18+
rdp(50, new int[]{1, 2});
19+
dp(5, new int[]{1, 2, 3});
20+
dp(10, new int[]{2, 5, 3, 6});
21+
dp(50, new int[]{1, 2});
22+
}
23+
24+
static void dp(int N, int[] s) {
25+
int[][] dp = new int[N + 1][s.length + 1];
26+
for (int j = 0; j <= s.length; dp[0][j] = 1, j++);
27+
28+
for (int i = 1; i <= N; i++) {
29+
for (int j = 1; j <= s.length; j++) {
30+
dp[i][j] = dp[i][j - 1];
31+
if (i - s[j - 1] >= 0) {
32+
dp[i][j] += dp[i - s[j - 1]][j];
33+
}
34+
}
35+
}
36+
System.out.println(dp[N][s.length] + " no of ways in i");
37+
}
38+
39+
static void rdp(int N, int[] s) {
40+
Arrays.sort(s);
41+
System.out.println(rcc(N, s, 0) + " no of ways in r");
42+
}
43+
44+
static int rcc(int N, int[] s, int index) {
45+
if (N == 0) return 1;
46+
int num = 0;
47+
for (int j = index; j < s.length; j++) {
48+
if (s[j] <= N) {
49+
num += rcc(N - s[j], s, j);
50+
}
51+
}
52+
return num;
53+
}
54+
}

EditDistance.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dp;
2+
3+
4+
public class EditDistance {
5+
6+
private static final int MATCH = 0;
7+
private static final int INSERT = 1;
8+
private static final int DELETE = 2;
9+
private static final int TRANSPOSE = 3;
10+
11+
public static void main(String[] args) {
12+
char[] x = "thou shall not".toCharArray();
13+
char[] y = "you should not".toCharArray();
14+
//char[] x = "saketh".toCharArray();
15+
//char[] y = "sakeht".toCharArray();
16+
long now = System.currentTimeMillis();
17+
System.out.println(editDistance(x, y) + " " + (System.currentTimeMillis() - now));
18+
}
19+
20+
private static int editDistance(char[] x, char[] y) {
21+
int[][] dist = new int[x.length + 1][y.length + 1];
22+
int[] costs = new int[TRANSPOSE + 1];
23+
24+
// row init
25+
for (int i = 0; i <= x.length; i++) {
26+
dist[i][0] = i;
27+
}
28+
// column init
29+
for (int j = 0; j <= y.length; j++) {
30+
dist[0][j] = j;
31+
}
32+
33+
for (int i = 1; i <= x.length; i++) {
34+
for (int j = 1; j <= y.length; j++) {
35+
costs[MATCH] = dist[i - 1][j - 1] + ((x[i - 1] == y[j - 1]) ? 0 : 1);
36+
costs[INSERT] = dist[i][j - 1] + 1;
37+
costs[DELETE] = dist[i - 1][j] + 1;
38+
costs[TRANSPOSE] = (i > 1 && j > 1 && x[i - 1] == y[j - 2] && x[i - 2] == y[j - 1])
39+
? dist[i - 2][j - 2] + 1 : Integer.MAX_VALUE;
40+
dist[i][j] = costs[MATCH];
41+
for (int e = INSERT; e <= TRANSPOSE; e++) {
42+
dist[i][j] = Math.min(dist[i][j], costs[e]);
43+
}
44+
}
45+
}
46+
47+
return dist[x.length][y.length];
48+
}
49+
}

EggDropping.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dp;
2+
3+
/**
4+
* Given an N story building and a supply of k eggs, find the strategy which minimizes (in the worst
5+
* case) the number of experimental drops required to determine the break floor.
6+
*/
7+
public class EggDropping {
8+
9+
public static void main(String[] args) {
10+
System.out.println(solve(8, 4));
11+
System.out.println(solve(256, 15));
12+
System.out.println(solve(1024, 15));
13+
System.out.println(solve(100, 2));
14+
System.out.println(solve(100, 3));
15+
}
16+
17+
private static int solve(int n, int k) {
18+
int[][] cost = new int[n + 1][k + 1];
19+
20+
for (int i = 1; i <= n; i++) cost[i][1] = i;
21+
for (int j = 1; j <= k; j++) cost[1][j] = 1;
22+
23+
for (int i = 2; i <= n; i++) {
24+
for (int j = 2; j <= k; j++) {
25+
cost[i][j] = Integer.MAX_VALUE;
26+
for (int x = 1; x <= i; x++) {
27+
int c = 1 + Math.max((cost[x - 1][j - 1]), cost[i - x][j]);
28+
if (c < cost[i][j]) {
29+
cost[i][j] = c;
30+
}
31+
}
32+
}
33+
}
34+
return cost[n][k];
35+
}
36+
37+
38+
static int rsolve(int e, int f) {
39+
return rdp(e, f, f);
40+
}
41+
42+
// FIXME stackoverflow error because of recursive calls
43+
static int rdp(int e, int f, int k) {
44+
if (f <= 1) return 1;
45+
if (e == 1) return f;
46+
if (e <= 0) return Integer.MAX_VALUE;
47+
48+
return 1 + Math.min(rdp(e - 1, f - 1, k), rdp (e, k - f, k));
49+
50+
}
51+
}

FindInterval.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
public class FindInterval {
7+
8+
public static void main(String[] args) {
9+
Interval []intervals = new Interval[3];
10+
intervals[0] = new Interval(3,4);
11+
intervals[1] = new Interval(2,3);
12+
intervals[2] = new Interval(1,2);
13+
int []arr = findRightInterval(intervals);
14+
for (int i = 0; i < arr.length; i++) {
15+
System.out.print(arr[i] + " ");
16+
}
17+
}
18+
19+
private static int[] findRightInterval(Interval[] intervals) {
20+
int []res = new int[intervals.length];
21+
int len = intervals.length;
22+
if (len == 1) {
23+
int []a = new int[1];
24+
a[0] = -1;
25+
return a;
26+
}
27+
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
28+
for (int i = 0; i < len; i++) {
29+
map.put(intervals[i].start, i);
30+
}
31+
Arrays.sort(intervals, (a, b)->(a.start - b.start));
32+
for (int i = 0; i < len; i++) {
33+
int tar = intervals[i].end;
34+
int left = i+1;
35+
int right = len ;
36+
while (left < right) {
37+
int mid = (left + right)/2;
38+
if (intervals[mid].start < tar) { left = mid + 1; }
39+
else { right = mid; }
40+
}
41+
res[map.get(intervals[i].start)] = (right == len) ? -1 : map.get(intervals[right].start);
42+
}
43+
return res;
44+
}
45+
}

Interval.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.leetcode;
2+
3+
public class Interval {
4+
public int start;
5+
public int end;
6+
7+
public Interval() {
8+
this.start = 0;
9+
this.end = 0;
10+
}
11+
12+
public Interval(int start, int end) {
13+
this.start = start;
14+
this.end = end;
15+
}
16+
}

Knapsack.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dp;
2+
3+
/**
4+
* Given weights and values of n items, put these items in a knapsack of capacity W to get the
5+
* maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and
6+
* wt[0..n-1] which represent values and weights associated with n items respectively. Also given
7+
* an integer W which represents knapsack capacity, find out the maximum value subset of val[]
8+
* such that sum of the weights of this subset is smaller than or equal to W. You cannot break an
9+
* item, either pick the complete item, or don’t pick it (0-1 property)
10+
*/
11+
public class Knapsack {
12+
13+
public static void main(String[] args) {
14+
rknapsack(5, new int[]{60, 100, 120}, new int[]{1, 2, 3});
15+
rknapsack(10, new int[]{60, 100, 120, 130, 140}, new int[]{1, 2, 3, 4, 5});
16+
rknapsack(10, new int[]{0, 60, 100, 120, 130, 140}, new int[]{0, 1, 2, 3, 4, 5});
17+
dp(5, new int[]{60, 100, 120}, new int[]{1, 2, 3});
18+
dp(10, new int[]{60, 100, 120, 130, 140}, new int[]{1, 2, 3, 4, 5});
19+
dp(10, new int[]{0, 60, 100, 120, 130, 140}, new int[]{0, 1, 2, 3, 4, 5});
20+
}
21+
22+
static void dp(int W, int[] values, int[] weights) {
23+
int dp[][] = new int[values.length + 1][W + 1];
24+
25+
for (int i = 1; i <= values.length; i++) {
26+
for (int j = 1; j <= W; j++) {
27+
dp[i][j] = dp[i - 1][j];
28+
if (weights[i - 1] <= j) {
29+
dp[i][j] = Math.max(dp[i][j], values[i - 1] + dp[i - 1][j - weights[i - 1]]);
30+
}
31+
}
32+
}
33+
34+
System.out.println("DP knapsack max : " + dp[values.length][W]);
35+
}
36+
37+
static void rknapsack(int W, int[] values, int[] weights) {
38+
System.out.println("Recursive knapsack max : " + rdp(W, values, weights, values.length -1));
39+
}
40+
41+
static int rdp(int W, int[] values, int[] weights, int i) {
42+
if (i < 0) return 0;
43+
int value = rdp(W, values, weights, i - 1);
44+
if (weights[i] <= W) {
45+
value = Math.max(value, values[i] + rdp(W - weights[i], values, weights, i - 1));
46+
}
47+
return value;
48+
}
49+
}

LBS.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dp;
2+
3+
/**
4+
* Given an array arr[0 … n-1] containing n positive integers, a subsequence of arr[] is called
5+
* Bitonic if it is first increasing, then decreasing. Write a function that takes an array as
6+
* argument and returns the length of the longest bitonic subsequence.
7+
8+
* A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty.
9+
* Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty
10+
*/
11+
public class LBS {
12+
13+
public static void main(String[] args) {
14+
/*rlbs(new int[]{1, 11, 2, 10, 4, 5, 2, 1});
15+
rlbs(new int[]{12, 11, 40, 5, 3, 1});
16+
rlbs(new int[]{80, 60, 30, 40, 20, 10});*/
17+
}
18+
19+
/* Solution - can be solved as combination of LIS and LDS */
20+
21+
}

LCA.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dp;
2+
3+
/**
4+
* Longest Commong Array(Substring)
5+
*/
6+
public class LCA {
7+
8+
public static void main(String[] args) {
9+
rsolve("COMBINATION".toCharArray(), "PERMUTATION".toCharArray());
10+
rsolve("COMBINATION".toCharArray(), "COMBUSTION".toCharArray());
11+
rsolve("MICHAELANGELO".toCharArray(), "HEIROGLYPHOLOGY".toCharArray());
12+
rsolve("ABCDEFG".toCharArray(), "HKLMNABCIJ".toCharArray());
13+
rsolve("forgeeksskeegfor".toCharArray(), "rofgeeksskeegrof".toCharArray());
14+
}
15+
16+
static int max;
17+
static void rsolve(char[] m, char[] n) {
18+
max = 0;
19+
rdp(m, n, m.length - 1, n.length - 1);
20+
System.out.println("recursive lcs " + max);
21+
}
22+
23+
static int rdp(char[] m, char[] n, int i, int j) {
24+
if (i < 0 || j < 0) return 0;
25+
if (m[i] == n[j]) {
26+
int tmp = 1 + rdp(m, n, i - 1, j - 1);
27+
max = Math.max(tmp, max);
28+
return tmp;
29+
} else {
30+
int tmp = Math.max(rdp(m, n, i - 1, j), rdp(m, n, i, j - 1));
31+
max = Math.max(tmp, max);
32+
return 0;
33+
}
34+
}
35+
}

LCS.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dp;
2+
3+
/**
4+
* Longest Common Subsequence
5+
*/
6+
public class LCS {
7+
8+
public static void main(String[] args) {
9+
rsolve("COMBINATION".toCharArray(), "PERMUTATION".toCharArray());
10+
rsolve("COMBINATION".toCharArray(), "COMBUSTION".toCharArray());
11+
rsolve("MICHAELANGELO".toCharArray(), "HEIROGLYPHOLOGY".toCharArray());
12+
rsolve("ABCDEFG".toCharArray(), "HKLMNABCIJ".toCharArray());
13+
rsolve("forgeeksskeegfor".toCharArray(), "rofgeeksskeegrof".toCharArray());
14+
}
15+
16+
static void rsolve(char[] m, char[] n) {
17+
System.out.println("recursive lcs " + rdp(m, n, 0, 0));
18+
}
19+
20+
static int rdp(char[] m, char[] n, int i, int j) {
21+
if (i >= m.length || j >= n.length) return 0;
22+
int max = 0;
23+
if (m[i] == n[j]) {
24+
max = 1 + rdp(m, n, i + 1, j + 1);
25+
} else {
26+
max = Math.max(max, rdp(m, n, i, j + 1));
27+
max = Math.max(max, rdp(m, n, i + 1, j));
28+
}
29+
return max;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)