Skip to content

Commit

Permalink
Merge pull request #2244 from BroLeaf/mybranch
Browse files Browse the repository at this point in the history
Add two DP program in C++
  • Loading branch information
ZoranPandovski authored Oct 7, 2019
2 parents bd59792 + 00ef3a3 commit 94b6a97
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions dynamic_programing/C++/Longest_Increasing_Subsequence.cpp
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;
}
18 changes: 18 additions & 0 deletions dynamic_programing/C++/Maximum_Consecutive_Sum.cpp
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;
}

0 comments on commit 94b6a97

Please sign in to comment.