We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 381181f commit fe67790Copy full SHA for fe67790
DynamicProgramming/MaximumSumIncreasingSubsequence/MaxSumIncreasingSubseq.java
@@ -15,14 +15,19 @@ public int maxSumOfIncSubseq(int A[]){
15
sum[0] = A[0];
16
17
for(int i = 1 ; i < n ; i++){
18
+ //for each index i we consider all elements before it
19
for(int j = 0 ; j < i ; j++){
20
+
21
+ //check for increasing subsequence and greater sum
22
if(A[j] < A[i] && sum[i] < sum[j]){
23
sum[i] = sum[j];
24
}
25
26
+ //since the subsequence ends in A[i] add the value of that element as well
27
sum[i] += A[i];
28
29
30
+ //find max sum
31
int maxSum = 0;
32
for(int i=0 ; i < n ; i++){
33
if(maxSum < sum[i]) maxSum = sum[i];
0 commit comments