2019-10-14 22:13:18

问题描述

问题求解

解法一:动态规划

这种数组划分的题目基本都可以使用dp来解决,核心的思路就是先维护低的划分,再在中间找分割点加入新的划分。

    public int splitArray(int[] nums, int m) {
int n = nums.length;
long[][] dp = new long[m + 1][n];
long[] presum = new long[n];
presum[0] = nums[0];
for (int i = 1; i < n; i++) presum[i] = presum[i - 1] + nums[i];
for (int i = 0; i < n; i++) dp[1][i] = presum[i];
for (int i = 2; i <= m; i++) {
for (int j = i - 1; j < n; j++) {
dp[i][j] = presum[n - 1];
for (int k = i - 2; k < j; k++) {
dp[i][j] = Math.min(dp[i][j], Math.max(dp[i - 1][k], presum[j] - presum[k]));
}
}
}
return (int)dp[m][n - 1];
}

解法二二分搜索

最小化最大的子串和是典型的二分搜索的问题描述。

求最小值的模版是(l, r],并不断维护。

    public int splitArray(int[] nums, int m) {
long l = 0;
long r = 0;
for (int num : nums) r += num;
while (r - l > 1) {
long mid = l + (r - l) / 2;
int k = helper(nums, mid);
if (k <= m) r = mid;
else l = mid;
}
return (int)r;
} private int helper(int[] nums, long target) {
int n = nums.length;
int res = 0;
for (int i = 0; i < n;) {
long curr = 0;
while (i < n) {
curr += nums[i];
if (curr > target) {
curr -= nums[i];
break;
}
else i += 1;
}
if (curr == 0) return n + 1;
res += 1;
}
return res;
}

  

动态规划-划分数组的最大和 Split Array Largest Sum的更多相关文章

  1. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

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

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

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

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

  6. Split Array Largest Sum

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

  7. 【leetcode】410. Split Array Largest Sum

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

  8. Leetcode: Split Array Largest Sum

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

  9. Split Array Largest Sum LT410

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

随机推荐

  1. 广州CVTE招聘-测试开发工程师

    内推邮箱:keweisheng@cvte.com 地点:广州 公司简介 CVTE成立于2005年,总部位于广州科学城,旗下设有多家独立的子公司,在香港设有全球服务中心,在国内设有21个营销服务中心和近 ...

  2. nexus7入手

    平板一直关注了很久了,关键是不知道平板对我来说,拿它来做什么用.所以,一直也就是关注,也没有决心买了. 终于这次出手了,N7,到货了! 照片是原生的android系统,不习惯,不习惯,直接用刷机精灵, ...

  3. 对话|首席研究员童欣:从长远看,AR的应用范围远比VR广泛

    ​童欣博士现任微软亚洲研究院网络图形组首席研究员.1993年毕业于浙江大学计算机系,获工学学士学位:1996年获浙江大学计算机系硕士学位:1999年获清华大学计算机系博士学位,同年加入微软亚洲研究院. ...

  4. 《深入理解 Java 虚拟机》读书笔记:类文件结构

    正文 一.无关性的基石 1.两种无关性 平台无关性: Java 程序的运行不受计算机平台的限制,"一次编写,到处运行". 语言无关性: Java 虚拟机只与 Class 文件关联, ...

  5. 一文看懂Java序列化

    一文看懂Java序列化 简介 Java实现 Serializable 最基本情况 类的成员为引用 同一对象多次序列化 子父类引用序列化 可自定义的可序列化 Externalizable:强制自定义序列 ...

  6. win10安装LoadRunner时,安装.net framwork组件报0x800F081F错误 解决办法

    一.报错原因 0x800F081F错误大多数是在安装软件时,系统无法联网自动下载安装. 经过各种排查及搜索解决方案,总结原因无非以下三种: 1.windows update被禁用. 2.电脑没有.ne ...

  7. Java核心技术卷1 第10版 笔记

    3.3 数据类型 Java是一种强类型语言.必须为每一个变量声明一种类型.在java中,一共有8终基本类型. 3.3.1 整形 类型 存储需求 取值范围 int 4字节 -2 147 483 648 ...

  8. 正式学习MVC 03

    1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...

  9. 有史以来最全的CMD命令

    说在前面的话: 本篇是博主通过网上查找整理而成的,且都是亲测可以的一些cmd命令,可以说是很齐全了,当然,如果有不可以运行的代码,欢迎大家留言指出,我会不断完善的,谢谢. CMD作用: 掌握一些基本的 ...

  10. linux 安装Mosquitto

    这篇博客讲的很好:https://www.cnblogs.com/chen1-kerr/p/7258487.html 此处简单摘抄部分内容 1.下载mosquitto安装包 源码地址:http://m ...