-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2244 from BroLeaf/mybranch
Add two DP program in C++
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int arr[9] = { 1, 4, 2, 3, 8, 3, 4, 1, 9}; // LIS should be {1, 2, 3, 8, 9} | ||
int f[9] = {}, LIS[9] = {}, max = 1, L = 0; // f used to record previous location of LIS | ||
for (int i = 0; i < 9; i++) | ||
{ | ||
LIS[i] = 1; | ||
f[i] = i; | ||
} | ||
for (int i = 1; i < 9; i++) | ||
{ | ||
for (int j = 0; j <= i; j++) | ||
{ | ||
if (arr[j] < arr[i] && LIS[j]+1 > LIS[i]) | ||
{ | ||
LIS[i] = LIS[j] + 1; | ||
if (LIS[i] > max) | ||
{ | ||
max = LIS[i]; | ||
L = i; | ||
} | ||
f[i] = j; | ||
} | ||
} | ||
} | ||
int tmp = L; | ||
while(LIS[L]--) | ||
{ | ||
cout << arr[tmp] << " "; | ||
tmp = f[tmp]; | ||
} | ||
cout << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int array[7] = {7, 6, -5, -8, 6, -2, 10}; // Maximum consecutive sum would be 6 + (-2) + 10 = 14 | ||
int sum = 0; | ||
int max_sum = array[0]; | ||
for (int i = 0; i < (sizeof(array)/sizeof(int)); ++i) | ||
{ | ||
sum += array[i]; | ||
sum = max(0, sum); | ||
max_sum = max(sum, max_sum); | ||
} | ||
cout << max_sum; | ||
return 0; | ||
} |