动态规划-划分数组的最大和 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 ...
随机推荐
- Vue数据绑定(一)
Contents Vue作为当下炙手可热的前端三大框架之一,一直都想深入研究一下其内部的实现原理,去学习MVVM模式的精髓.如果说MVVM是当下最流行的图形用户界面开发模式,那么数据绑定则是这一模式的 ...
- PS4游戏将登陆PC:一曲属于主机的悲歌
曾经,红白机.PS游戏机等成为一代人难以磨灭的记忆.而随后的索尼PS3.微软Xbox 360.任天堂Wii U等,也称霸了次时代主机时代,成为家庭娱乐的中心.但面对着依托于PC和智能移动终端 ...
- 记录R的一些黑魔法
通路富集结果可视化 12345678 pathway<-read.table("PTC+_transcript_pep_supp_KEGG.txt",header=T,sep ...
- 使用python模拟登录网易邮箱网站
环境要求 python 3.6 chromedriver.exe 文件 人工登录 人工登录某个网站,首先需要用浏览器打开登录页面,然后在输入框中输入对应的账号和密码,最后点击登录,以下使用代码模拟以上 ...
- 第二章 表与指针Pro SQL Server Internal (Dmitri Korotkev)
聚集索引 聚集索引就是表中数据的物理顺序,它是按照聚集索引分类的.表只能定义一个聚集索引. 如果你要在一个有数据的堆表中创建一个聚集索引,如2-5所示,第一步要做的就是SQL服务器创建另一个根据聚集索 ...
- git指令-删除
git指令-删除 添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test.txt" [maste ...
- 爬虫(三)解析js,抓取优酷免费视频的真实播放地址
工具:google浏览器 + fiddler抓包工具 说明:这里不贴代码,[只讲思路!!!] 原始url = https://v.youku.com/v_show/id_XMzIwNjgyMDgwOA ...
- GPUImage学习总结
GPUImage是iOS上一个基于OpenGL进行图像处理的开源框架,内置大量滤镜,架构灵活,可以在其基础上很轻松地实现各种图像处理功能. GPUImgae特性 1,丰富的输入组件 摄像头.图片.视频 ...
- 阿里云ECS开放批量创建实例接口,实现弹性资源的创建
摘要: 为了更方便的实现弹性的资源创建,方便用户一次运行多台ECS按量实例来完成应用的开发和部署,阿里云开放了ECS的批量创建实例接口RunInstances,可以单次最多创建100台实例,避免重复调 ...
- html/css系列-图片上下居中
本文详情:http://www.zymseo.com/276.html图片上下居中的问题常用的几种方法:图片已知大小和未知大小,自行理解 .main{ width: 400px;height: 400 ...