动态规划-划分数组的最大和 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 ...
随机推荐
- 开发过程中关于JSON的那些事
在使用过程中,对JSON了解的还不够,特地整理一下,用于个人学习和知识参考. 1.IBM的json入门指南 json官网 2.javaweb中发送接收解析问题 3.Java解析json,以及js ...
- 网络编程之C10K
网络编程之C10K 虽然在过去的十几年里C10K问题已经可以很好的解决,但学习网络编程时研究C10K问题仍然价值巨大,因为技术的发展都是有规律和线索可循的,了解C10K问题及其解决思路,通过举一反三, ...
- C++走向远洋——49(项目一2、复数类中的运算符重载、类的友元函数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 第一章 感受mac之美-换一种方式用电脑,开启新历程
感谢关注我的读者一直以来的追随与信任.去年到今年以来大环境都不是很好.裁员,机构优化,工厂倒闭,公司破产,贸易战等消息传来,不少还是身边发生的.今年开年以来更是有病毒横行,天降蝗灾等灾害.愿大家都好好 ...
- firewalls 开放端口
# 1. 开放 tcp 80 端口 firewall-cmd --zone=public --add-port=10080/tcp --permanent # 2. 开放 10080 ~ 65535 ...
- Samtec与Neoconix达成合作并和II-VI推出新产品
序言:Samtec近日动作不断, 近日Samtec与Neoconix达成合作并和II-VI推出新产品,以下是详细内容. Samtec与Neoconix签订Neoconix PCBeam 技术授权协议, ...
- java反序列化-ysoserial-调试分析总结篇(4)
1.前言 这篇文章继续分析commoncollections4利用链,这篇文章是对cc2的改造,和cc3一样,cc3是对cc1的改造,cc4则是对cc2的改造,里面chained的invoke变成了i ...
- 2019年后,Java岗面试快速突击指南
大家好.这篇文章给大家分享一下如何获得一个可以去参加面试的最小可行知识(Minimal Viable Knowledge)!我自己在就基本上靠文章中的策略在找实习的时候拿到了头条阿里的offer.所以 ...
- JavaScript常见的六种继承方式
前言 面向对象编程很重要的一个方面,就是对象的继承.A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法.这对于代码的复用是非常有用的. 大部分面向对象的编程语言,都是通过"类 ...
- 前端每日实战:111# 视频演示如何用纯 CSS 创作一只艺术的鸭子
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/aaoveW 可交互视频 此视频是可 ...