网站内容架构拓扑怎么做,网站内页标题修改,佛山网站建设怎么选,单页做网站教程问题描述
给定一个正整数、负整数和 0 组成的 N M 矩阵#xff0c;编写代码找出元素总和最大的子矩阵。
返回一个数组 [r1, c1, r2, c2]#xff0c;其中 r1, c1 分别代表子矩阵左上角的行号和列号#xff0c;r2, c2 分别代表右下角的行号和列号。若有多个满足条件的子矩阵…问题描述
给定一个正整数、负整数和 0 组成的 N × M 矩阵编写代码找出元素总和最大的子矩阵。
返回一个数组 [r1, c1, r2, c2]其中 r1, c1 分别代表子矩阵左上角的行号和列号r2, c2 分别代表右下角的行号和列号。若有多个满足条件的子矩阵返回任意一个均可。
解题思路
1. 基础概念Kadane算法
首先我们需要了解Kadane算法这是一种用于在一维数组中找到最大子数组和的算法。给定一个数组该算法可以找到一个连续子数组其元素之和最大并且能返回这个最大和。
2. 将问题从二维转化为一维
要在二维矩阵中寻找最大子矩阵和我们可以通过固定列的起始和结束点来将问题简化为一维问题
固定列界限选择两个列的索引left和right使得这两个索引之间的所有列都被包含在内。累加行元素对于每个行计算从left列到right列的元素和得到一个新的“行和数组”。例如如果left和right都是1即第二列那么行和数组中的每个元素就是原矩阵该行第二列的元素。
3. 应用Kadane算法到行和数组
对每一个固定的列对left和right我们都得到了一个行和数组。接下来
使用Kadane算法将Kadane算法应用于行和数组找出这个数组中的最大子数组和以及对应的起始行和结束行。记录最大和及其位置如果这次的最大子数组和大于之前记录的最大值更新最大值和相应的行和列索引。这些索引就确定了最大子矩阵的边界。
4. 处理所有可能的列对
从第一列开始逐一将每列作为起始列然后对每个可能的结束列重复上述过程。这意味着我们首先固定起始列然后让结束列从起始列开始向右延伸至矩阵的最后一列对每种情况都计算行和数组然后应用Kadane算法。
5. 输出最终结果
在所有列对组合被考虑之后全局记录的最大值及其对应的子矩阵边界就是我们的答案。这些信息可以用来标识出具有最大和的子矩阵。
代码实现
class Solution {
public:vectorint getMaxMatrix(vectorvectorint matrix) {int maxSum INT_MIN;vectorint result(4); // 存放最终结果[r1, c1, r2, c2]int rows matrix.size(), cols matrix[0].size();// 遍历所有列的组合for (int left 0; left cols; left) {vectorint rowSum(rows, 0); // 初始化行和数组for (int right left; right cols; right) {// 计算从left到right列的行和for (int i 0; i rows; i) {rowSum[i] matrix[i][right];}// 应用Kadane算法找到最大的子数组和及其索引int currentMax INT_MIN, tempSum 0;int rowStart 0, tempRowStart 0, rowEnd 0;for (int i 0; i rows; i) {if (tempSum 0) {tempSum rowSum[i];tempRowStart i;} else {tempSum rowSum[i];}if (tempSum currentMax) {currentMax tempSum;rowStart tempRowStart;rowEnd i;}}// 更新全局最大和及对应的子矩阵坐标if (currentMax maxSum) {maxSum currentMax;result {rowStart, left, rowEnd, right};}}}return result;}
};
文章转载自: http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.tlnkz.cn.gov.cn.tlnkz.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn