18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum.

这道求和最大的子矩阵,跟LeetCode上的Maximum Size Subarray Sum Equals kMaximum Subarray很类似。这道题不建议使用brute force的方法,因为实在是不高效,我们需要借鉴上面LeetCode中的建立累计和矩阵的思路,我们先来看这道题的第一种解法,由于建立好累计和矩阵,那么我们通过给定了矩阵的左上和右下两个顶点的坐标可以在O(1)的时间内快速的求出矩阵和,所以我们要做的就是遍历矩阵中所有的子矩阵,然后比较其矩阵和,返回最大的即可,时间复杂度为O(n4)。

解法一:

vector<vector<int>> precompute(vector<vector<int>> &matrix) {
vector<vector<int>> sumMatrix = matrix;
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[i].size(); ++j) {
if (i == && j == ) {
sumMatrix[i][j] = matrix[i][j];
} else if (j == ) {
sumMatrix[i][j] = sumMatrix[i - ][j] + matrix[i][j];
} else if (i == ) {
sumMatrix[i][j] = sumMatrix[i][j - ] + matrix[i][j];
} else {
sumMatrix[i][j] = sumMatrix[i - ][j] + sumMatrix[i][j - ] - sumMatrix[i - ][j - ] + matrix[i][j];
}
}
}
return sumMatrix;
} int compute_sum(vector<vector<int>> &sumMatrix, int i1, int i2, int j1, int j2) {
if (i1 == && j1 == ) {
return sumMatrix[i2][j2];
} else if (i1 == ) {
return sumMatrix[i2][j2] - sumMatrix[i2][j1 - ];
} else if (j1 == ) {
return sumMatrix[i2][j2] - sumMatrix[i1 - ][j2];
} else {
return sumMatrix[i2][j2] - sumMatrix[i2][j1 - ] - sumMatrix[i1 - ][j2] + sumMatrix[i1 - ][j1 - ];
}
} int get_max_matrix(vector<vector<int>> &matrix) {
int res = INT_MIN;
vector<vector<int>> sumMatrix = precompute(matrix);
for (int r1 = ; r1 < matrix.size(); ++r1) {
for (int r2 = r1; r2 < matrix.size(); ++r2) {
for (int c1 = ; c1 < matrix[].size(); ++c1) {
for (int c2 = c1; c2 < matrix[].size(); ++c2) {
int sum = compute_sum(sumMatrix, r1, r2, c1, c2);
res = max(res, sum);
}
}
}
}
return res;
}

其实这道题的解法还能进一步优化到O(n3),根据LeetCode中的那道Maximum Subarray的解法,我们可以对一维数组求最大子数组的时间复杂度优化到O(n),那么我们可以借鉴其的思路,由于二维数组中遍历所有的列数相等的子矩阵的时间为O(n2),每一行的遍历是O(n),所以整个下来的时间复杂度即为O(n3),参见代码如下:

解法二:

int max_subarray(vector<int> &array) {
int res = , sum = ;
for (int i = ; i < array.size(); ++i) {
sum += array[i];
res = max(res, sum);
sum = max(sum, );
}
return res;
} int max_submatrix(vector<vector<int>> &matrix) {
if (matrix.empty() || matrix[].empty()) return ;
int res = ;
for (int r1 = ; r1 < matrix.size(); ++r1) {
vector<int> sum(matrix[].size());
for (int r2 = r1; r2 < matrix.size(); ++r2) {
for (int c = ; c < matrix[].size(); ++c) {
sum[c] += matrix[r2][c];
}
int t = max_subarray(sum);
res = max(res, t);
}
}
return res;
}

CareerCup All in One 题目汇总

[CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵的更多相关文章

  1. Google - Largest Sum Submatrix

    Given an NxN matrix of positive and negative integers, write code to find the submatrix with the lar ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  4. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  6. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

  8. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

随机推荐

  1. POJ 1163:The Triangle

    Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a progr ...

  2. SQLServer两张表筛选相同数据和不同数据

    概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...

  3. 分布式服务框架 Zookeeper -- 管理分布式环境中的数据

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html Zookeeper 分布式服务框架是 Apa ...

  4. popupwindow点击空白处如何自动消失?

    Popupwindow如果需要点击空白处自动消失,需要设置两个函数 1.customPopWindow.setFocusable(true);该函数也可以在构造函数中设置,如:mPopupWindow ...

  5. Java 中新增的 foreach 的用法

      JDK1.5加入的增强for和循环. foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部 ...

  6. requireJS的使用_API(1)

    之前有介绍过requireJS(模块化开发),可以看看 ,但是不详细,所以今天参考官网来详细介绍一下: 1.加载js文件: RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<scr ...

  7. Something about "if"

    分支:If引导了一个分支,语法结构有{if:if, else if,else:if,else:等}if可以单独使用,不一定非要加else,但是为了语句的严谨或者程序的稳定,请尽量使用else(注意if ...

  8. angular.element函数

    听说这个 element 函数和 jQuery 里的 $() 差不多 element函数有一个参数,传入的是一个对象,后面可以接着点其他的方法,如果jQuery可用的话就可以在它的后面点jQuery的 ...

  9. flume 使用 spool source的时候字符集出错

    1. 错误所在 -- ::, (pool--thread-) [ERROR - org.apache.flume.source.SpoolDirectorySource$SpoolDirectoryR ...

  10. Hadoop YARN资源隔离技术

    YARN对内存资源和CPU资源采用了不同的资源隔离方案.对于内存资源,它是一种限制性资源,它的量的大小直接决定应用程序的死活,因为应用程序到达内存限制,会发生OOM,就会被杀死.CPU资源一般用Cgr ...