From 7f7b2b0e68bea40a25a54e0e4531ed16e5152296 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Thu, 24 Sep 2020 00:11:28 +0530 Subject: [PATCH 1/4] added kadane algorithm function (#1079) * added kadane algorithm function * added changes * third commit * fixed some problems * fixed warnings * added documentation * Update kadane_algorithm/kadane.cpp Co-authored-by: David Leal * Update kadane_algorithm/kadane.cpp Co-authored-by: David Leal * Update kadane_algorithm/kadane.cpp Co-authored-by: David Leal * added details in the documentation * Update kadane_algorithm/kadane.cpp Co-authored-by: David Leal * Update kadane_algorithm/kadane.cpp Co-authored-by: David Leal * added kadane2.cpp * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * added some things * fixed * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * changes * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * added code * added changes suggested * Update .vscode/settings.json Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal * Update dynamic_programming/kadane2.cpp Co-authored-by: David Leal Co-authored-by: David Leal --- dynamic_programming/kadane2.cpp | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 dynamic_programming/kadane2.cpp diff --git a/dynamic_programming/kadane2.cpp b/dynamic_programming/kadane2.cpp new file mode 100644 index 000000000..c632e6f5a --- /dev/null +++ b/dynamic_programming/kadane2.cpp @@ -0,0 +1,73 @@ +/** + * @file + * @brief Implementation of [Kadane + * Algorithm] (https://en.wikipedia.org/wiki/Kadane%27s_algorithm) + * + * @details + * Kadane algorithm is used to find the maximum sum subarray in an array and + * maximum sum subarray problem is the task of finding a contiguous subarray + * with the largest sum + * + * ### Algorithm + * The simple idea of the algorithm is to search for all positive + * contiguous segments of the array and keep track of maximum sum contiguous + * segment among all positive segments(curr_sum is used for this) + * Each time we get a positive sum we compare it with max_sum and update max_sum + * if it is greater than curr_sum + * + * @author [Ayush Singh](https://github.com/ayush523) + */ +#include +#include +#include +/** + * @namespace dynamic_programming + * @brief Dynamic Programming algorithms + */ +namespace dynamic_programming { +/** + * @namespace kadane + * @brief Functions for [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm. + */ +namespace kadane { +/** + * @brief maxSubArray function is used to calculate the maximum sum subarray + * and returns the value of maximum sum which is stored in the variable max_sum + * @tparam N number of array size + * @param n array where numbers are saved + * @returns the value of maximum subarray sum + */ +template +int maxSubArray(const std::array &n) { + int curr_sum = + 0; // declaring a variable named as curr_sum and initialized it to 0 + int max_sum = INT_MIN; // Initialized max_sum to INT_MIN + for (int i : n) { // for loop to iterate over the elements of the array + curr_sum += n[i]; + max_sum = std::max(max_sum, curr_sum); // getting the maximum value + curr_sum = std::max(curr_sum, 0); // updating the value of curr_sum + } + return max_sum; // returning the value of max_sum +} +} // namespace kadane +} // namespace dynamic_programming + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + const int N = 5; + std::array n{}; // declaring array + // taking values of elements from user + for (int i = 0; i < n.size(); i++) { + std::cout << "Enter value of n[" << i << "]" + << "\n"; + std::cin >> n[i]; + } + int max_sum = dynamic_programming::kadane::maxSubArray( + n); // calling maxSubArray function + std::cout << "Maximum subarray sum is " << max_sum; // Printing the answer + + return 0; +} From 01965739494a16cb71ad4fb11694da0420ab5715 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 23 Sep 2020 18:42:14 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3e9afd3b3..60840a60d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -60,6 +60,7 @@ * [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp) * [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp) * [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp) + * [Kadane2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane2.cpp) * [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp) * [Longest Common Subsequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_subsequence.cpp) * [Longest Increasing Subsequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_increasing_subsequence.cpp) From 110ae88aaf53e1f9c0ab8f460487403959466922 Mon Sep 17 00:00:00 2001 From: Pardeep Bhatt Date: Thu, 24 Sep 2020 12:49:55 +0530 Subject: [PATCH 3/4] Delete largestbst_in_binary_tree.cpp --- .../largestbst_in_binary_tree.cpp | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 dynamic_programming/largestbst_in_binary_tree.cpp diff --git a/dynamic_programming/largestbst_in_binary_tree.cpp b/dynamic_programming/largestbst_in_binary_tree.cpp deleted file mode 100644 index a848b6b33..000000000 --- a/dynamic_programming/largestbst_in_binary_tree.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// Tree node structure used in the program - -struct Node { - int data; - Node *left; - Node *right; - - Node(int val) { - data = val; - left = right = NULL; - } -}; - -// Return the size of the largest sub-tree which is also a BST -struct info { - int subtree_size; - int max; - int min; - int largestBST_size; - bool isBST; -}; - -// utility function -info largestBSTBT(Node *root) { - if (!root) - return {0, INT_MIN, INT_MAX, 0, true}; - if (!root->left and !root->right) - return {1, root->data, root->data, 1, true}; - - info left = largestBSTBT(root->left); - info right = largestBSTBT(root->right); - - info sol; - sol.subtree_size = (1 + left.subtree_size + right.subtree_size); - - if (left.isBST and right.isBST and left.max < root->data and - right.min > root->data) { - sol.isBST = true; - sol.min = min(left.min, min(right.min, root->data)); - sol.max = max(right.max, max(left.max, root->data)); - sol.largestBST_size = sol.subtree_size; - - return sol; - } - - sol.largestBST_size = max(left.largestBST_size, right.largestBST_size); - sol.isBST = false; - - return sol; -} - -int largestBst(Node *root) { return largestBSTBT(root).largestBST_size; } \ No newline at end of file From 9ce09fa373ffe569bb145601f03239d0142595a7 Mon Sep 17 00:00:00 2001 From: Pardeep Bhatt Date: Thu, 24 Sep 2020 14:13:34 +0530 Subject: [PATCH 4/4] Update cut_rod.cpp --- dynamic_programming/cut_rod.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index ce98401dd..0d566cc8c 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -11,8 +11,8 @@ using std::cout; template -int cutrod(const std::array &p, int n) { - std::array r; +int cutrod(const std::array &p, const int n) { + int *r = new int[n + 1]; r[0] = 0; for (int j = 0; j < n; j++) { int q = INT_MIN; @@ -21,12 +21,15 @@ int cutrod(const std::array &p, int n) { } r[j + 1] = q; } - return r[n]; + int ans = r[n]; + delete[] r; + return ans; } int main() { - std::array price = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; - cout << cutrod(price, 30); + const int n = 30; + std::array price = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; + cout << cutrod(price, n); return 0; }