Skip to content

Commit 381181f

Browse files
authored
Update and rename MaxSumIncreasingSubseq.txt to MaxSumIncreasingSubseq.java
1 parent 5bc23e3 commit 381181f

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
https://www.techiedelight.com/increasing-subsequence-with-maximum-sum/
3+
4+
Basically the same as the problem - Longest Increasing Subsequence - Instead of storing the max length, we store the max sum.
5+
6+
The problem is to find the maximum sum possible after considering all increasing subsequences.
7+
*/
8+
9+
public int maxSumOfIncSubseq(int A[]){
10+
int n = A.length;
11+
12+
//sum[i] indicates the sum of the increasing subsequence formed ending at i
13+
int sum[] = new int[n];
14+
15+
sum[0] = A[0];
16+
17+
for(int i = 1 ; i < n ; i++){
18+
for(int j = 0 ; j < i ; j++){
19+
if(A[j] < A[i] && sum[i] < sum[j]){
20+
sum[i] = sum[j];
21+
}
22+
}
23+
sum[i] += A[i];
24+
}
25+
26+
int maxSum = 0;
27+
for(int i=0 ; i < n ; i++){
28+
if(maxSum < sum[i]) maxSum = sum[i];
29+
}
30+
31+
return maxSum;
32+
33+
}
34+
35+
/*
36+
Time Complexity - O(n^2)
37+
Space Complexity - O(n)
38+
*/

DynamicProgramming/MaximumSumIncreasingSubsequence/MaxSumIncreasingSubseq.txt

-47
This file was deleted.

0 commit comments

Comments
 (0)