From 40ca3186f0a67c3344aeccbee350689a372decb7 Mon Sep 17 00:00:00 2001 From: Mooophy Date: Fri, 10 Apr 2015 03:41:50 +1200 Subject: [PATCH] fix format, and a little problem caused by #8 modified: main.cc modified: solution.h --- 014. Maximum Subarray/main.cc | 4 +- 014. Maximum Subarray/solution.h | 72 ++++++++++++++++---------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/014. Maximum Subarray/main.cc b/014. Maximum Subarray/main.cc index b8e08d7..8420382 100644 --- a/014. Maximum Subarray/main.cc +++ b/014. Maximum Subarray/main.cc @@ -7,8 +7,8 @@ int main() Solution s; std::cout << s.maxSubArray(A, 9) << std::endl; - SolutionByDivideAndConquer dac; - std::cout << s.maxSubArray(A, 9) << std::endl; + SolutionByDivideAndConquer dac; + std::cout << dac.maxSubArray(A, 9) << std::endl; return 0; } diff --git a/014. Maximum Subarray/solution.h b/014. Maximum Subarray/solution.h index bd0f72b..4142ece 100644 --- a/014. Maximum Subarray/solution.h +++ b/014. Maximum Subarray/solution.h @@ -14,42 +14,42 @@ class Solution { }; class SolutionByDivideAndConquer{ - //O(n) - int find_max_crossing_subarray(int arr[], unsigned low, unsigned mid, unsigned hgh) - { - auto accumulation = 0; - - auto lft_sum = accumulation = arr[mid]; - if (mid > low) - for (auto i = mid - 1; i != low - 1; --i) - if ((accumulation += arr[i]) > lft_sum) - lft_sum = accumulation; - - auto rht_sum = accumulation = arr[mid + 1]; - if (hgh > mid) - for (auto i = mid + 2; i != hgh + 1; ++i) - if ((accumulation += arr[i]) > rht_sum) - rht_sum = accumulation; - - return lft_sum + rht_sum; - } - - //O(n lg n) - int divide_and_conquer(int arr[], unsigned low, unsigned hgh) - { - if (low == hgh) return arr[low]; - - auto mid = (low + hgh) / 2; - auto lft = divide_and_conquer(arr, low, mid); - auto rht = divide_and_conquer(arr, mid + 1, hgh); - auto crs = find_max_crossing_subarray(arr, low, mid, hgh); - - return std::max({ lft, rht, crs }); - } + //O(n) + int find_max_crossing_subarray(int arr[], unsigned low, unsigned mid, unsigned hgh) + { + auto accumulation = 0; + + auto lft_sum = accumulation = arr[mid]; + if (mid > low) + for (auto i = mid - 1; i != low - 1; --i) + if ((accumulation += arr[i]) > lft_sum) + lft_sum = accumulation; + + auto rht_sum = accumulation = arr[mid + 1]; + if (hgh > mid) + for (auto i = mid + 2; i != hgh + 1; ++i) + if ((accumulation += arr[i]) > rht_sum) + rht_sum = accumulation; + + return lft_sum + rht_sum; + } + + //O(n lg n) + int divide_and_conquer(int arr[], unsigned low, unsigned hgh) + { + if (low == hgh) return arr[low]; + + auto mid = (low + hgh) / 2; + auto lft = divide_and_conquer(arr, low, mid); + auto rht = divide_and_conquer(arr, mid + 1, hgh); + auto crs = find_max_crossing_subarray(arr, low, mid, hgh); + + return std::max({ lft, rht, crs }); + } public: - int maxSubArray(int A[], int n) - { - return divide_and_conquer(A, 0, n - 1); - } + int maxSubArray(int A[], int n) + { + return divide_and_conquer(A, 0, n - 1); + } };