Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix format, and a little problem caused by #8 #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 014. Maximum Subarray/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
72 changes: 36 additions & 36 deletions 014. Maximum Subarray/solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};