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 ...
随机推荐
- jedis连接池参数minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis探索
我们通常在使用JedisPoolConfig进行连接池配置的时候,minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis这两个参数经常会不懂 ...
- centos7初上手1-安装mysql数据库
随着云服务器的普及,购入云服务器的门槛越来越低,对一个程序员来说,很多人会购买一款云服务器.以前买过两年windows服务器(没有什么实际用途,就是为了玩),最近有机会接触一下linux服务器,选择了 ...
- ansj分词
本文转载至:https://blog.csdn.net/bitcarmanlee/article/details/53607776 最近的项目需要使用到分词技术.本着不重复造轮子的原则,使用了ansj ...
- LNMP(三)
第二十二次课 LNMP(三) 目录 一.Nginx负载均衡 二.ssl原理 三.生成ssl密钥对 四.Nginx配置ssl 五.php-fpm的pool 六.php-fpm慢执行日志 七.open_b ...
- clusterware启动顺序——CSS
CSS层面 1.ocssd.bin守护进程 该层面最主要的工作就是启动ocssd.bin守护进程.使本地节点能够成功地加入集群.ocssd.bin守护进程的启动过程可以通过以下的图形进行描述. ::: ...
- json格式字符串用Uncaught SyntaxError: Unexpected token ' Uncaught SyntaxError: Unexpected number
Unexpected number(index)的错误用的json字符串如 var jsonStr = "{1:'北京note备注信息',2:'上海note备注信息',3:'广东note备注 ...
- python "import this"
The Zen of Python, by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Simp ...
- 花了2小时写bug
程序员的工作,写bug,修bug,改bug 写了2小时逻辑关系,没写明白 比昨天多了一个返回上一层的功能 也很简单,清除下数组内容即可 emm..明天继续深究吧 dic = { "植物&qu ...
- javascript 中的原型继承
javascript圆形变成的基本规则: 所有数据都是对象: 要得到一个对象,不是通过实例化类,而是找到一个对象作为原型并克隆它: 对象会记住它的原型: 如果对象无法响应某个请求,它会把这个请求委托给 ...
- 关于SpringCloud配置网关转发时出现一下啊错误:“com.netflix.zuul.exception.ZuulException: Forwarding error at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.handleException”
com.netflix.zuul.exception.ZuulException: Forwarding error at org.springframework.cloud.netflix.zuul ...