[抄题]:

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.

Note:
If n is the length of array, assume the following constraints are satisfied:

  • 1 ≤ n ≤ 1000
  • 1 ≤ m ≤ min(50, n)

Examples:

Input:
nums = [7,2,5,10,8]
m = 2 Output:
18 Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道干嘛用二分法:二分法可以通过mid的移动 找一个位置

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

想不到:最大的数肯定要分隔开,就看能往左划几个数和它一组

[7,2,5,10,8,105550]
3
返回105550

分组不超过m就往左扩展 否则往右

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

l r都必须定义成long型,在返回的时候切换回来

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

多试试几个case 自然就明白了

数组中找一个位置,就可以用二分查找

[复杂度]:Time complexity: O(lgn) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:迭代

[关键模板化代码]:

while (l <= r) {
long mid = (l + r) / 2;
if (noLongerThanM(mid, nums, m)) r = mid - 1;
else l = mid + 1;
} return (int)l;
}

[其他解法]:

dp麻烦

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public int splitArray(int[] nums, int m) {
//ini: sum, l, r
int max = nums[0]; long sum = 0;
for (int num : nums) {
sum += num;
max = Math.max(max, num);
} //cc
if (nums.length == 1) return (int)sum;
long l = max, r = sum; // b - s
while (l <= r) {
long mid = (l + r) / 2;
if (noLongerThanM(mid, nums, m)) r = mid - 1;
else l = mid + 1;
} return (int)l;
} public boolean noLongerThanM(long value, int[] nums, int m) {
int count = 1, sum = 0; for (int num : nums) {
sum += num;
if (sum > value) {
sum = num;
count++;
if (count > m) return false;
}
} return true;
}
}

410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小的更多相关文章

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

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

  2. 410 Split Array Largest Sum 分割数组的最大值

    给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件:    1 ≤ n ≤ 1000 ...

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

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

  4. 【leetcode】410. Split Array Largest Sum

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  5. 410. Split Array Largest Sum

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

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

  7. Split Array Largest Sum

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

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

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

  9. Leetcode: Split Array Largest Sum

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

随机推荐

  1. jdk1.8新特性之函数式接口

    函数式接口就是只有一个抽象方法的接口.如果这个接口里没有或者包含了两个以上的抽象方法,对不起,你不叫函数式接口,只能叫你接口.那这个函数式有啥用呢?如果配合Lambda表达式的话,可以大大的简化代码. ...

  2. Mac环境下PHPstorm配置xdebug开发调试web程序

    一.安装PHP的xdebug扩展 安装xdebug(技巧,为了找到适配的版本,让xdebug网站根据phpinfo()函数输出分析找到对应的方法及安装步骤:如果安装了多个PHP版本的话,尽量用phpi ...

  3. idea 注册码 地址:

    http://idea.lanyus.com IntelliJ IDEA 注册码 *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的Int ...

  4. 阻塞队列之五:LinkedBlockingQueue

    一.LinkedBlockingQueue简介 LinkedBlockingQueue是一个使用链表完成队列操作的阻塞队列.链表是单向链表,而不是双向链表.采用对于的next构成链表的方式来存储对象. ...

  5. Servlet文件上传和下载的复习

    上传 使用Servlet完成上传和下载相较于使用Struts框架有点麻烦,毕竟更偏底层了 项目中主要使用的jar包: commons-io-2.2.jar  commons-fileupload-1. ...

  6. 【UVA】1596 Bug Hunt(模拟)

    题目 题目     分析 算是个模拟吧     代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...

  7. nginx转发请求

    location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_add ...

  8. Mysql--可用的 MySQL 产品和专业服务

    一.MySQL Community Edition(社区版):MySQL Community Edition is the freely downloadable version of the wor ...

  9. JavaScript数据类型的检测

    主要有一下四种方法: 1.typeof 2.instanceof 3.constructor 4.Object.prototype.toString.call() 1.typeof 不能具体细分是什么 ...

  10. 1.Linux下生成密钥

    1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...