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. How to install the zsh shell in Linux && how to set it as a default login shell

    Z shell’s (zsh) popularity has increased in the last years. I have not moved too zsh yet, but I am g ...

  2. mysql编译时报的一个警告warning: type-punning to incomplete type might break strict-aliasing rules,可能是bug

    cmake的时候报了一个警告: /softdb/mysql-5.5.37/storage/innobase/handler/ha_innodb.cc:11870: warning: type-punn ...

  3. 第十一篇:SOUI系统资源管理

    SOUI资源管理模块 从前篇已经讲到在SOUI中所有资源文件通过一个uires.idx文件进行索引. 这里将介绍在程序中如何引用这些资源文件. 在SOUI系统中,资源文件通过一个统一的接口对象读取: ...

  4. java线程之——sleep()与wait()的区别

    sleep()是Thread的方法,wait()是Object的方法 如果线程进入了同步锁,sleep不会释放对象锁,wait会释放对象锁 sleep的作用就是让正在执行的线程主动让出CPU,给其它线 ...

  5. 看懂UML类图与时序图

    看懂UML类图和时序图 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关系: 能看懂类图中各个类之间的线条.箭头代表什么意思后,也就足够应对 日常的工作和交流: 同时,我们应该能将类图 ...

  6. error: bad symbolic reference. A signature in HiveContext.class refers to term hive

    在spark-shell中执行val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)报错: error: bad symbolic ...

  7. JAVA Day5

                                         数组   1.数组是一组变量的集合,存储“相同数据类型”的一组数据: 连续的空间. 2.声明一个变量就是在内存空间划出一块合适 ...

  8. Linux使用du和df查看磁盘和文件夹占用空间

    df df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. df -lh 参数 -h 表示使用「Human-readable」输出,也就是使用 GB.MB 等易读的格式. $ ...

  9. ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)

    将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName   re限制输出速率,按照 ...

  10. 廖雪峰js教程笔记4 sort排序的一些坑

    排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个对象呢?直接比较数学上的大小是没有意义的,因 ...