diff --git a/dynamic_programing/C++/Longest_Increasing_Subsequence.cpp b/dynamic_programing/C++/Longest_Increasing_Subsequence.cpp new file mode 100644 index 0000000000..a597dde543 --- /dev/null +++ b/dynamic_programing/C++/Longest_Increasing_Subsequence.cpp @@ -0,0 +1,38 @@ +#include + +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; +} diff --git a/dynamic_programing/C++/Maximum_Consecutive_Sum.cpp b/dynamic_programing/C++/Maximum_Consecutive_Sum.cpp new file mode 100644 index 0000000000..910e74558d --- /dev/null +++ b/dynamic_programing/C++/Maximum_Consecutive_Sum.cpp @@ -0,0 +1,18 @@ +#include + +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; +}