-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicProgramming.java
108 lines (74 loc) · 2.47 KB
/
DynamicProgramming.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.Arrays;
public class DynamicProgramming {
//dynamic programming - memoized
public static int coinChange(int[] coins, int amount) {
if (amount < 1) return 0;
return coinChange(coins, amount, new int[amount]);
}
private static int coinChange(int[] coins, int rem, int[] count) {
if (rem < 0) return -1;
if (rem == 0) return 0;
if (count[rem - 1] != 0) return count[rem - 1];
int min = Integer.MAX_VALUE;
for (int coin : coins) {
int res = coinChange(coins, rem - coin, count);
if (res >= 0 && res < min)
min = 1 + res;
}
if(min == Integer.MAX_VALUE)
min=-1;
count[rem - 1] = min;
return count[rem - 1];
}
//fibonacci with DP -- memoized
public static int fibonacci(int n) {
int[] history = new int[n+1];
return fibonacci(n , history);
}
private static int fibonacci(int n, int[] history) {
int res=0;
if(history[n]!=0)
return history[n];
if(n==1 || n==2)
res = 1;
else
res = fibonacci(n-1,history) + fibonacci(n-2,history);
history[n] = res;
return res;
}
//fibonacci with DP -- Bottom-UP (***NOT RECURSION***)
public static int fibonacciBU(int n) {
int[] history = new int[n+1];
if(n==1 || n==2)
return 1;
history[1] = 1;
history[2] = 1;
for(int i =3 ; i<=n ; i++)
history[i] = history[i-1] + history[i-2];
return history[n];
}
//
public static int solution2(int[] A) {
if(A.length==1)
return Math.abs(A[0]);
int[] absArray = new int[A.length];
for(int i=0 ; i<A.length ; i++)
absArray[i] = Math.abs(A[i]);
Arrays.sort(absArray);
for(int i=0 ; i<A.length ; i++)
System.out.println(absArray[i]);
int sum=0;
int r=0, l = absArray.length-1;
while(r<l)
{
System.out.println("\n r= " + absArray[r] + " l = " + absArray[l] + "\t\t sum is: " + sum);
if(sum<absArray[l])
sum+=absArray[r++];
else
sum-=absArray[l--];
if(r==l)
sum-=absArray[l--];
}
return sum;
}
}