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 ...
随机推荐
- 《Swell数学》用户故事
一.用户故事基础知识: 1. 从用户的角度来描述用户渴望得到的功能. 2. 用户故事是描述对用户有价值的功能,好的用户故事应该包括角色.功能和商业价值三个要素. 3. 一个用户故事只是以客户能够明白的 ...
- 在Eclipse中Tomcat配置图片保存路径
在上一篇二维码功能实现的时候发现,若将二维码保存在项目路径下,服务器起了之后存入的二维码图片是无法实时读取的,所以在Tomcat上配置图片保存位置,将图片保存到项目外的地方. 查找资料的时候看见一个方 ...
- 记一次JDK升级带来的连环反应
公司之前有个很久以前的小项目,页面用到了flash. 现在要去掉flash, 前端使用公司自己开发的框架来展示数据, 使用该框架后台要引用一个jar包封装数据传递给前台. 但该框架是jdk1.8编译的 ...
- OpenGL之shader着色器的应用,三色渐变的三角形
学习自: https://learnopengl-cn.github.io/01%20Getting%20started/05%20Shaders/#_7 首先放一张效果图: 本次教程,将着色器单独定 ...
- 【转】Android-Input 触摸设备
https://source.android.com/devices/input/touch-devices 触摸设备 Android 支持各种触摸屏和触摸板,包括基于触控笔的数字化板. 触摸屏是与显 ...
- linux下快速安装chrome
linux下安装chrome 1.按下 Ctrl + Alt + t 键盘组合键,启动终端 2.在终端中,输入以下命令: (将下载源加入到系统的源列表.命令的反馈结果如图.如果返回“地址解析错误”等信 ...
- Mac下安装证书fiddlerRoot.cer
Step 1: 设置Mac的代理如下 Step 2:打开127.0.0.1:8888,下载fiddlerRoot.cer; Step 3:下载好了,双击安装,但是默认这个证书是不可信的,你需要在钥匙串 ...
- React Navigation基本用法
/** * Created by apple on 2018/9/23. */ import React, { Component } from 'react'; import {AppRegistr ...
- PHP 出现中文乱码的问题
在代码中添加 <?php //设置页面显示的文字编码 头部就写header函数处理成utf-8 header("Content-Type:text/html;charset=utf-8 ...
- .NET并行计算和并发8:硬件支持
共享内存多核系统,分布式内存系统 区别 分布式内存系统主要通过Message passing interface在各个微处理器之间通信,但是MPI共享内存多核系统是没有必要的,会造成额外的开销. 分布 ...