Divide and Conquer
169 Majority Element
Here low = 0, high = nums.size(); initially (super end)
solve(vector<int>& nums, int low, int high)
Base case:
1 == high - low return nums[low]
Otherwise, get mid = low + (high - mid) / 2;
int leftMod = solve(nums, low, mid);
int rightMod = solve(nums, mid, high);
if (leftMod == rightMod)
return leftMod;
Otherwise count the leftMod and rightMod frequency in the whole array. return the bigger one.
Get the maximum of (1) maximum sub-array in the left half (2) maximum in the right half (3) and the maximum sub-array that accross the right half and right half.
globalMax = max(solve(nums, left, mid), solve(nums, mid, right));
return max(globalMax, leftMax + rightMax);
241 Different Ways to Add Parentheses
Get the different ways to add parentheses (1) in the left half (2) in the right half (3) and the different combinations in the left half and right half using different operations.
215 Kth Largest Element in an Array
(1) Get the order of the target element: order = nums.size() - k;
(2) while start < end do the following:
(3) Let pivot = start, open = start + 1, close = start, loop every element at idx from open to end and do the following
(4) if nums[idx] < nums[pivot] swap nums[idx] with nums[open], close = open, open++
(5) after the inner loop, swap nums[pivot] with nums[close]
(6) if close == order, return nums[close], else if order < close, let end = close try the left half. Otherwise, let start = order + 1, try the right half.
(7) If after the loop, no early exit, return -1, meaning not exist in the array.
240 Search a 2D Matrix II
(1) For loop each row of the matrix and try to test whether the target is in that row (check target >= row.front() && target <= row.back() )
(2) When the row contains target is found, do binary search in that row : serachVector(row, target),
if any row return true, then return found (true) for the problem
(3) If all the rows doesn't contain target, return false (i.e no early exit above)
Here low = 0, high = nums.size(); initially (super end)
solve(vector<int>& nums, int low, int high)
Base case:
1 == high - low return nums[low]
Otherwise, get mid = low + (high - mid) / 2;
int leftMod = solve(nums, low, mid);
int rightMod = solve(nums, mid, high);
if (leftMod == rightMod)
return leftMod;
Otherwise count the leftMod and rightMod frequency in the whole array. return the bigger one.
| 53 |
globalMax = max(solve(nums, left, mid), solve(nums, mid, right));
return max(globalMax, leftMax + rightMax);
241 Different Ways to Add Parentheses
Get the different ways to add parentheses (1) in the left half (2) in the right half (3) and the different combinations in the left half and right half using different operations.
215 Kth Largest Element in an Array
(1) Get the order of the target element: order = nums.size() - k;
(2) while start < end do the following:
(3) Let pivot = start, open = start + 1, close = start, loop every element at idx from open to end and do the following
(4) if nums[idx] < nums[pivot] swap nums[idx] with nums[open], close = open, open++
(5) after the inner loop, swap nums[pivot] with nums[close]
(6) if close == order, return nums[close], else if order < close, let end = close try the left half. Otherwise, let start = order + 1, try the right half.
(7) If after the loop, no early exit, return -1, meaning not exist in the array.
240 Search a 2D Matrix II
(1) For loop each row of the matrix and try to test whether the target is in that row (check target >= row.front() && target <= row.back() )
(2) When the row contains target is found, do binary search in that row : serachVector(row, target),
if any row return true, then return found (true) for the problem
(3) If all the rows doesn't contain target, return false (i.e no early exit above)
Comments
Post a Comment