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. Ubuntu 18.04安装VNC远程登录

    reference: https://blog.csdn.net/bluewhalerobot/article/details/73649353 https://community.bwbot.org ...

  2. 字符串及其操作,字符的Unicode编码

    plainText=input('message:') for c in plainText: print(chr(ord(c)-3),end='') plainText=input('message ...

  3. The Preliminary Contest for ICPC China Nanchang National Invitational I题

    Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...

  4. web项目中web.xml简介

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

  5. day 29 元类

    ---恢复内容开始--- 一.元类的介绍 元类:在python里,一切皆对象.所有自定义的类本身也是元类的对象,即所有自定义的类本质上也是由元类实例化出来的. class关键字创建自定义类的底层的工作 ...

  6. Spring Boot 整合JDBCTemplate

    1. 首先配置pom.xml 1.1 dbcm2 是数据源类型,表示配置dataSource的方式 1.2 spring-boot-starter-jdbc是表示让spring boot 支持jdbc ...

  7. spring cloud使用Feign做消费端时的eureka.client.registerWithEureka/eureka.client.fetchRegistry是否配置的问题

    记录一下今天工作中的一个小失误. 今天用Feign搭建服务消费者的时候,考虑消费者不需要再提供服务给其他服务,所以不需要注册到注册中心(eureka)中.结果把registerWithEureka和f ...

  8. ubuntu安装ssh-service出现无法依赖的解决

    (1)首先先确认下ubuntu系统是否已经安装ssh(通常ubuntu中默认是安装的) 通过命令进行查看:$dpkg -l | grep ssh这里我们可以看到,系统显示已经安装了openssh-cl ...

  9. 在Servlet中获取spring容器WebApplicationContext

    WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ge ...

  10. springboot单元测试自动回滚:@Transactional

    2019-04-21 12:23:14.509 INFO 9384 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - St ...