- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP1.java
More file actions
Latest commit
180 lines (166 loc) · 6.18 KB
/
Copy pathDP1.java
File metadata and controls
180 lines (166 loc) · 6.18 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
packagecom.michaelho.DynamicProgramming;
importjava.util.Arrays;
importjava.util.Comparator;
importjava.util.List;
/**
* The DP1 class explores a set of dynamic programming questions and solutions such as
* Fibonacci, longest common subsequence (LCS), longest uncommon subsequence (LUS),
* and longest increasing subsequence (LIS).
*
* @author Michael Ho
* @since 2014-09-14
* */
classDP1 {
Fibonaccifib = newFibonacci();
LISlis = newLIS();
LUSlus = newLUS();
LCSlcs = newLCS();
/**
* The Fibonacci class explores the methods used to calculate the Fibonacci number.
* Methods include recursive and dynamic programming.
* */
classFibonacci {
/**
* The recursive method used to calculate Fibonacci numbers. This method
* is inefficient in runtime, which is O(2^N).
*
* @param x The number to be calculated Fibonacci numbers.
* @return int The results of calculation.
* */
intcount(intx) {
if (x == 0) {
return0;
} elseif (x == 1) {
return1;
}
returncount(x-1) + count(x-2);
}
/**
* The dynamic programing method used to calculate Fibonacci numbers.
* The method uses storage to reduced duplicate caluculation
*
* @param x The number to be calculated Fibonacci numbers.
* @return int The results of calculation.
* */
intdpCount(intx) {
int[] fArray = newint[x+1];
fArray[0] = 0;
fArray[1] = 1;
for (inti = 2; i <= x; i++) {
fArray[i] = fArray[i-1] + fArray[i-2];
}
returnfArray[x];
}
}
/**
* The LCS class use the dynamic programming method to calculate the longest common subsequence
* of given array. Subsequence is different from substring. Subsequence is a subset
* of elements in order that can be derived from another sequence while substring has
* to be a set of consecutive elements.
* */
classLCS {
/**
* The dynamic method used to find the longest length of subsequence. The runtime is O(N^2).
*
* @param s1 The first string to be calculated for longest increasing subsequence.
* @param s2 The second string to be calculated for longest increasing subsequence.
* @return int The results of calculation.
*/
intlengthOfLCS(Strings1, Strings2) {
returndpFindLongestLength(s1, s2, s1.length() - 1, s2.length() - 1);
}
/**
* The recursive function used to find LCS.
*
* @param s1 The first string to be calculated for longest increasing subsequence.
* @param s2 The second string to be calculated for longest increasing subsequence.
* @param idx1 The index of the first string.
* @param idx2 The index of the second string.
* @return int The results of calculation.
*/
privateintdpFindLongestLength(Strings1, Strings2, intidx1, intidx2) {
if (idx1 < 0 || idx2 < 0) {
return0;
}
if (s1.charAt(idx1) == s2.charAt(idx2))
return1 + dpFindLongestLength(s1, s2, idx1 - 1, idx2 - 1);
else
returnMath.max(dpFindLongestLength(s1, s2, idx1, idx2 - 1),
dpFindLongestLength(s1, s2, idx1 - 1, idx2));
}
}
/**
* The LUS class explores the method to calculate the longest uncommon subsequence
* of given array of strings.
* */
classLUS {
/**
* The method used to find the longest length of subsequence. The runtime is roughly O(N^2).
*
* @param strs The first string to be calculated for longest increasing subsequence.
* @return int The length of LUS.
* */
intfindLUSlength(String[] strs) {
if (strs.length <= 1) returnstrs.length;
List<String> strsList = Arrays.asList(strs);
strsList.sort(Comparator.comparingInt(String::length).reversed()); // Compare by length
for (inti = 0; i < strsList.size(); i++) {
booleanisLUS = true;
for (intj = 0; j < strsList.size(); j++) {
if (i != j && isSubsequence(strs[i], strs[j])) {
isLUS = false;
break;
}
}
if (isLUS) {
returnstrs[i].length();
}
}
return -1;
}
booleanisSubsequence(Strings1, Strings2) {
inti = 0;
intj = 0;
while (i < s1.length() && j < s2.length()) {
if (s1.charAt(i) == s2.charAt(j)) {
i++;
}
j ++;
}
returni == s1.length();
}
}
/**
* The LIS class explores the method to find the length of the longest increasing
* subsequence. Subsequence is different from substring. Subsequence is a subset
* of elements in order that can be derived from another sequence while substring
* has to be a set of consecutive elements.
* */
classLIS {
/**
* The dynamic method used to find the longest length of subsequence. The runtime is O(N^2).
*
* @param array The array to be calculated for longest increasing subsequence.
* @return int The results of calculation.
* */
intdpFindLongestLength(int[] array) {
intmax = 1;
int[] countArr = newint[array.length];
// O(N)
for (inti = 0; i < array.length; i++) {
countArr[i] = 1;
// O(N)
for (intj = 0; j < i; j ++) {
if (array[j] < array[i] && countArr[i] < countArr[j] + 1) {
countArr[i] = 1 + countArr[j];
}
}
}
// O(N)
for (intcount : countArr) {
max = Math.max(count, max);
}
returnmax;
}
}
}