Skip to content

Commit c125fc0

Browse files
committed
DP update
1 parent d689184 commit c125fc0

File tree

5 files changed

+14
-26
lines changed

5 files changed

+14
-26
lines changed

.idea/workspace.xml

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/company/Lecture12/EditDistance.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static int editDist(String f,String s,int flen,int slen){
1717
if(slen == 0){
1818
return flen;
1919
}
20-
2120
int count = 0;
2221
if(f.charAt(flen - 1) == s.charAt(slen - 1)){
2322
count = editDist(f,s,flen-1,slen-1);
@@ -54,7 +53,6 @@ public static int editDistDP(String f,String s,int flen,int slen,Integer[][] mem
5453
}
5554

5655
public static int editDistDPitr(String f,String s,Integer[][] mem){
57-
5856
int flen,slen = 0;
5957
for (flen = 0; flen <= f.length(); flen++) {
6058
for (slen = 0; slen <= s.length(); slen++) {
@@ -73,7 +71,6 @@ public static int editDistDPitr(String f,String s,Integer[][] mem){
7371
}
7472
}
7573
}
76-
7774
return mem[flen][slen];
7875
}
7976
}

src/com/company/Lecture12/EggDrop.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ public static int eggDrop(int floors,int eggs){
1515
if(floors == 0){
1616
return 0;
1717
}
18-
1918
if(eggs == 1){
2019
return floors;
2120
}
22-
2321
int minimum = floors;
2422
for (int f = 1; f <= floors; f++) {
2523
int down = eggDrop(f - 1,eggs -1);
@@ -37,22 +35,19 @@ public static int eggDropDP(int floors,int eggs, Integer[][] mem){
3735
if(floors == 0){
3836
return 0;
3937
}
40-
4138
if(eggs == 1){
4239
return floors;
4340
}
44-
4541
int minimum = floors;
4642
for (int f = 1; f <= floors; f++) {
47-
int down = eggDrop(f - 1,eggs -1);
48-
int up = eggDrop(floors - f,eggs);
43+
int down = eggDropDP(f - 1,eggs -1, mem);
44+
int up = eggDropDP(floors - f,eggs, mem);
4945

5046
int max = 1 + Math.max(down,up);
5147
if(max < minimum){
5248
minimum = max;
5349
}
5450
}
55-
5651
mem[floors][eggs] = minimum;
5752
return mem[floors][eggs];
5853
}

src/com/company/Lecture12/LongSubseq.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ public static int lcs(String f,String s,int flen,int slen){
1515
if(flen == 0 || slen == 0){
1616
return 0;
1717
}
18-
19-
int count = 0;
18+
int count;
2019
if(f.charAt(flen-1) == s.charAt(slen-1)){
2120
count = 1 + lcs(f,s,flen-1,slen-1);
2221
}else{
2322
int right = lcs(f,s,flen-1,slen);
2423
int left = lcs(f,s,flen,slen-1);
2524
count = Math.max(right,left);
2625
}
27-
2826
return count;
2927
}
3028

src/com/company/Lecture12/ZeroOneKnapsack.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ public static int zeroOne(int[] wts, int[] prs, int cap, int size){
1414
if(size == 0){
1515
return 0;
1616
}
17-
1817
int acc = 0;
1918
if(wts[size - 1] <= cap){
20-
acc = prs[size-1] + zeroOne(wts, prs, cap-wts[size-1], size-1);
19+
acc = prs[size-1] +
20+
zeroOne(wts, prs, cap-wts[size-1], size-1);
2121
}
22-
2322
int reject = zeroOne(wts, prs, cap, size-1);
2423
return Math.max(acc, reject);
2524
}
@@ -28,22 +27,20 @@ public static int zeroOneDP(int[] wts, int[] prs, int cap, int size, Integer[][]
2827
if(size == 0){
2928
return 0;
3029
}
31-
3230
if(mem[cap][size] != null){
3331
return mem[cap][size];
3432
}
3533
int acc = 0;
3634
if(wts[size - 1] <= cap){
37-
acc = prs[size-1] + zeroOneDP(wts, prs, cap-wts[size-1], size-1, mem);
35+
acc = prs[size-1] +
36+
zeroOneDP(wts, prs, cap-wts[size-1], size-1, mem);
3837
}
39-
4038
int reject = zeroOneDP(wts, prs, cap, size-1, mem);
4139
mem[cap][size] = Math.max(acc, reject);
4240
return mem[cap][size];
4341
}
4442

4543
public static int zeroOneDPitr(int[] wts, int[] prs, int cap, int size, Integer[][] mem){
46-
4744
for (int c = 0; c <= cap; c++) {
4845
for (int s = 0; s <= size; s++) {
4946
if(s == 0){
@@ -53,7 +50,6 @@ public static int zeroOneDPitr(int[] wts, int[] prs, int cap, int size, Integer[
5350
if(wts[s - 1] <= c){
5451
acc = prs[s-1] + mem[c-wts[s-1]][s-1];
5552
}
56-
5753
int reject = mem[c][s-1];
5854
mem[c][s] = Math.max(acc,reject);
5955
}

0 commit comments

Comments
 (0)