动态规划-划分数组的最大和 Split Array Largest Sum
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的更多相关文章
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- [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 ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 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 LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- 查询优化基础知识 - chendh blog
概述 处理一个给定的查询,尤其是复杂查询,通常会有许多种策略,查询优化就是从这许多策略中找出最有效的查询执行计划的处理过程. 查询执行计划的步骤 产生逻辑上与给定表达式等价的表达式: 估计每个执行计划 ...
- Apple store,奢华背后的辛酸
store,奢华背后的辛酸" title="Apple store,奢华背后的辛酸"> 2000年的时候,乔布斯提出了要建造苹果直营店,也就是Apple st ...
- Oracle中的列转行实现字段拼接用例
文章目录 Oracle中的列转行实现字段拼接 场景 在SQL使用过程中经常有这种需求:将某列字段拼接成in('XX','XX','XX','XX','XX','XX' ...)做为查询条件. 实现 s ...
- Mybatis调用存储过程报错
Mybatis调用存储过程 贴码 123456 Error querying database. Cause: java.sql.SQLException: User does not have ac ...
- 《深入理解 Java 虚拟机》读书笔记:类文件结构
正文 一.无关性的基石 1.两种无关性 平台无关性: Java 程序的运行不受计算机平台的限制,"一次编写,到处运行". 语言无关性: Java 虚拟机只与 Class 文件关联, ...
- dubbo与trivial超时机制的深入思考
说在前面 trivial是根据之前设计的RPC框架而来的(还在增进当中),其中较为不同的一个点为,在客户端去掉了业务线程池,因为既然都要等待,不必要再加一层. 进入正题 有在网上看到这样的信息,“之前 ...
- 操作系统-IO管理和磁盘调度
I/O设备 IO设备的类型 分为三类:人机交互类外部设备:打印机.显示器.鼠标.键盘等等.这类设备数据交换速度相对较慢,通常是以字节为单位进行数据交换的 存储设备:用于存储程序和数据的设备,如磁盘.磁 ...
- 关于图数据库HugeGraph的百万,千万,亿量级测试
1.Hugegraph测试硬件 1.1.本机硬件 本机测试hugeGraph版本:0.10.4 后置存储数据库:rocksdb,1TB的普通硬盘 1.2.测试服务器硬件 测试服务器hugegraph版 ...
- 桌面运维之Windows快捷键,每一个工程师都是“快捷键”的工程师!
1.win快捷键 首先教大家win7新增的3D效果: Win + Tab 快速切换已打开的程序(和Alt+tab一样的效果) Win + Home 将所有使用中窗口以外的窗口最小化 Win + Spa ...
- python初学者必看学习路线图!!!
python应该是近几年比较火的语言之一,很多人刚学python不知道该如何学习,尤其是没有编程基础想要从事程序员工作的小白,想必应该都会有此疑惑,包括我刚学python的时候也是通过从网上查找相关资 ...