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的更多相关文章

  1. [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 ...

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

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

  3. Split Array Largest Sum

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

  4. [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 ...

  5. Leetcode: Split Array Largest Sum

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

  6. 410. Split Array Largest Sum

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

  7. [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 ...

  8. [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 ...

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

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

随机推荐

  1. jar包添加到maven本地仓库

    操作系统windows 本地要配置过maven环境 cmd  运行命令 mvn install:install-file -Dfile=D:\commons-net-3.6.jar.jar -Dgro ...

  2. lunx中部分命令总结

    一.文件和目录操作命令ls  全拼list,功能是列出目录的内容及其内容属性信息. cd  全拼change directory,功能是从当前工作目录切换到指定的工作目录. cp  全拼copy,其功 ...

  3. Oracle学习DayFive(PL/SQL)

    一.PL/SQL简介  PL/SQL 是 Procedure Language & Structured Query Language 的缩写.PL/SQL 是对 SQL 语言存储过程语言的扩 ...

  4. zookeeper 启动 zookeeper_server.pid: Permission denied

    在启动zookeeper的时候 报错 没有权限 以为是 zookeeper 没有权限 然后 chmod -R 777 zookeeper/ 之后还是不行. 后来 才发现 原来是我 /tmp/zooke ...

  5. c#计算器

    代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴. using System;using System.Collections.Generic;using System. ...

  6. JavaScript 中的常用12种循环遍历(数组或对象)的方法

    1.for 循环 let arr = [1,2,3]; for (let i=0; i<arr.length; i++){ console.log(i,arr[i]) } // 0 1 // 1 ...

  7. vue启动报错

    在安装依赖后,启动时报错 修复方法:将项目node_modules文件夹删除掉,重新cnpm install即可

  8. js 冒泡排序与快速排序

    刚好今晚看了js的冒泡排序跟快速排序,趁着还没忘记先记下来. 1. 冒泡排序:遍历数组,每个元素都与后一个元素比较,如果大于下一个元素,则两个元素位置调换.否则的话当前元素再与下下个元素比较,一直到 ...

  9. tomcat免安装版做成windows系统服务

    安装服务在命令行中进入/Tomcat路径/bin/,执行“service.bat install”:卸载服务在命令行中进入/Tomcat路径/bin/,执行“service.bat remove”:

  10. python爬虫,使用urllib2库报错

    urllib2发生报错URLError: <urlopen error [Errno 10061]:首先检查网址是否正确其次如果报这种错误,是因为ie里设置了代理,取消即可, 步骤: 打开IE浏 ...