Google - Largest Sum Submatrix
Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum.
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
} class Solution{
public int maxSubMatrix(int[][] nums) {
if(nums == null || nums.length == 0 || nums[0].length == 0){
return 0;
}
int r = nums.length;
int c = nums[0].length;
int maxSum = nums[0][0];
for(int i = 0; i < r; i++){
int[] sum = new int[c];
for(int j = i; j < r; j++){
for(int k = 0; k < c; k++){
sum [k] += nums[j][c];
}
int m = maxSubArray(sum);
maxSum = Math.max(maxSum, m);
}
}
} public int maxSubArray(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int maxSoFar = nums[0];
int maxEndingHere = nums[0];
for(int i = 1; i < nums.length; i++){
maxEndingHere = Math.max(maxEndingHere + nums[i], nums[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
}
Google - Largest Sum Submatrix的更多相关文章
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
- [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 ...
- [Swift]LeetCode813. 最大平均值和的分组 | Largest Sum of Averages
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
随机推荐
- Visual Studio+VAssistX自动添加注释,函数头注释,文件头注释
转载:http://blog.csdn.net/xzytl60937234/article/details/70455777 在VAssistX中为C++提供了比较规范注释模板,用这个注释模板为编写的 ...
- IRP小结 0x01 IRP & IO_STACK_LOCATION(结合WRK理解)
写博客整理记录一下IRP相关的知识点,加深一下印象. 所有的I/O请求都是以IRP的形式提交的.当I/O管理器为了响应某个线程调用的的I/O API的时候,就会构造一个IRP,用于在I/O系统处理这个 ...
- python-之-深浅拷贝二(元组)
元组比较特殊 1.----元组本身为不可变类型 import copy v1 = (1, 2, 3, 4) v2 = copy.copy(v1) print(id(v1), id(v2)) v3 = ...
- L1-062 幸运彩票
彩票的号码有 6 位数字,若一张彩票的前 3 位上的数之和等于后 3 位上的数之和,则称这张彩票是幸运的.本题就请你判断给定的彩票是不是幸运的. 输入格式: 输入在第一行中给出一个正整数 N(≤ 10 ...
- MongoDB基本命令总结
其实一直想整理下我常使用的MongoDB数据库的一些操作命令,终于有时间了~ MongoDB是一种开源的,免费的非关系型数据库(NoSql),不存在表.记录等概念,与通常的关系型数据库有些差异: Mo ...
- 安装oracle数据库的操作步骤
1. vnc启动之后,进入数据库安装包所在目录,此处是/home/DB/backup/database 2. 输入命令 ./runInstaller 3. 弹出linux图形化界面,同时弹出Oracl ...
- phpstrom常用快捷键
mark一下 格式化(应设置QQ快捷键) 自动代码提示 Ctrl+Alt+L Ctrl+J 页面查找 页面查找并替换 Ctrl+F Ctrl+R 全局查找 全局查找并替换 ...
- mbstowcs 和ifstream 前为何要setlocale
最近看公司的一些代码,发现一些地方调用了std::locale::global(locale("")); (c++) 和 setlocale(LC_ALL, "" ...
- 泊爷带你学go -- 经典的继承与接口 简直吊炸天 !
package main import ( "fmt" ) type TeamBase struct { m_TeamId uint64 m_Rid uint32 m_RoomRu ...
- 低成本制作基于OpenWRT的渗透工具
不知道你听说过Hak5的产品没有,它们可是黑客以及渗透测试人员的最爱.其中,有很多的PoC黑客工具都曾在热门美剧<黑客军团>中出现过.Hak5的 PACKETSQUIRREL 上架已经有好 ...