动态规划-划分数组的最大和 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 ...
随机推荐
- SpringBoot 处理 POST Json 传参枚举
在 Spring 框架中对枚举类型的序列化/反序列化是有限制的. 假设如下面这样在某些情况下就不能正常工作: 123456789 public enum PayChannelEnum implemen ...
- OPPO招聘-互联网测试
邮 箱:ljy@oppo.com 工作地点:深圳
- Html学习笔记(二) 简单标签
标签的重点 标签的用途 标签在浏览器中的默认样式 <body>标签: 在网页上显示的内容 <p>标签: 添加段落 <hx>标签: 添加标题 标签一共有6个,h1.h ...
- uniq命令使用方法
uniq命令的作用:显示唯一的行,对于那些连续重复的行只显示一次! 接下来通过实践实例说明: [root@stu100 ~]# cat test boy took bat home boy took ...
- 【Eclipse】eclipse设置,为了更简单快捷的开发
保存时自动导包 Windows->Perferences->Java->Editor->Save Actions
- 【攻略】如何在云开发中使用 Redis?
默认情况下,云开发的函数部署在公共网络中,只可以访问公网.如果开发者需要访问腾讯云的 Redis.TencentDB.CVM.Kafka 等资源,需要建立私有网络来确保数据安全及连接安全. 本文会演示 ...
- unittest实战(一):用例框架
import unittest class forTest0(unittest.TestCase): @classmethod def setUpClass(cls) -> None: prin ...
- CSS3详解:border color
继续我们的 ,大家觉得怎么样呢?
- 盘点Linux运维常用工具(一)-web篇之httpd
#前言:想把自己学的各种服务进行分类归档起来,于是就写了盘点Linux运维常用工具,Linux方面使用到的web应用服务有httpd(apache).nginx.tomcat.lighttpd,先了解 ...
- 遍历tree
1.解决方法 filterData (arr) { var newArr = [] arr.map((item) => { var childrenArr if (item.children ! ...