GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — programming/problems/array/MaximumSumRectangleInMatrix.cpp at master · rp-code9/programming Expand file tree/ Copy pathMaximumSumRectangleInMatrix.cppMore file actions 111 lines (94 loc) · 2.74 KB / Copy pathMaximumSumRectangleInMatrix.cppFile metadata and controls111 lines (94 loc) · 2.74 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 // GeeksforGeeks https://www.geeksforgeeks.org/maximum-sum-rectangle-in-a-2d-matrix-dp-27/ #include <iostream> #include <vector> #include <climits> #include <algorithm> struct Result { public: int maxSum {INT_MIN}; int top {-1}; int bottom {-1}; int left {-1}; int right {-1}; void setValues(int sum, int t, int b, int l, int r) { maxSum = sum; top = t; bottom = b; left = l; right = r; } }; int kadane(std::vector<int>& array, int& start, int& finish) { int currentSum {0}; int maxSum {INT_MIN}; int lStart {0}; finish = -1; for (int i = 0; i < array.size(); ++i) { currentSum += array[i]; if (currentSum < 0) { currentSum = 0; lStart = i + 1; } else if (currentSum > maxSum) { maxSum = currentSum; finish = i; start = lStart; } } if (finish != - 1) { return maxSum; } maxSum = array[0]; start = finish = 0; for (int i = 1; i < array.size(); ++i) { if (array[i] > maxSum) { maxSum = array[i]; start = finish = i; } } return maxSum; } void maximumSumRectangle(std::vector<std::vector<int>>& matrix, Result& result) { const int ROWS = matrix.size(); const int COLS = matrix[0].size(); int start {-1}; int finish {-1}; int sum {INT_MIN}; int maxSum {INT_MIN}; std::vector<int> temp(ROWS); for (int left = 0; left < COLS; ++left) { std::fill(temp.begin(), temp.end(), 0); for (int right = left; right < COLS; ++right) { // Find row sum of this vertical column at right index for (int i = 0; i < ROWS; ++i) { temp[i] = temp[i] + matrix[i][right]; } // Run Kadane algorithm, get start and end indices sum = kadane(temp, start, finish); // Update bounds and max sum if (sum > maxSum) { maxSum = sum; result.setValues(maxSum, start, finish, left, right); } } } } void test(std::vector<std::vector<int>>& matrix) { struct Result result; maximumSumRectangle(matrix, result); std::cout << "Maximum sum is : " << result.maxSum << "\n"; std::cout << "(top, left) : " << "(" << result.top << "," << result.left << ")\n"; std::cout << "(bottom, right) : " << "(" << result.bottom << "," << result.right << ")\n"; } int main() { std::vector<std::vector<int>> matrix = {{1, 2, -1, -4, 20}, { -8, -3, 4, 2, 1}, {3, 8, 10, 1, 3}, {-4, -1, 1, 7, -6}}; test(matrix); return 0; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |