题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among these m subarrays)。
Note:
If n is the length of array, assume the following constraints are satisfied:
1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)
Examples:
Input:
nums = [7,2,5,10,8]
m = 2
Output:
18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
状态:dp[j][i]就是简单的缩小题目的规模,把j长的序列分成i个连续的子序列,子序列和的最大值中的最小值。
状态转移方程:每次我们只关注整个序列中最后一个元素加入时对dp值的影响,由于是要分成连续的序列,所以最后一个元素只能与它前面的若干元素组成子序列,需要一个for来枚举包含最后一个元素的子序列的情况,例如我现在要求dp[j][i],在放入最后一个元素nums[j]时,设klen为第i个连续子序列的长度,这个子序列的和为
dp[nums.size][1]-dp[nums.size-klen][1],而前nums-klen个元素组成的i-1个连续子序列和的最大值的最小值为dp[nums.size][i-1]已经在前面的计算过程中完成了计算,易知dp[j][i] = min(max(dp[nums.size][1]-dp[nums.size-klen][1],dp[nums.size][i-1])),这个题可以很明显的看出动态规划的最优子结构
不过为了方便起见,先将所有的dp[j][1]计算出来,计算很简单也好理解,就是for循环叠加,不过如果单纯用int数组的话可能越界,比如:[1,2147483647],会在dp数组中出现-2147483648这样的元素。由于输入样本都是整数,可以使用double数组,返回的结果用int强制转换即可
 
     public int splitArray(int[] nums,int m) {
int nlen = nums.length;
int[]num = new int[nlen+1];
double[][]dp = new double[nlen+1][m+1];
double temp = 0;
num[0] = nums.length;
for(int i = 1;i<=nlen;i++)
num[i] = nums[i-1];
for(int i = 0;i<=nlen;i++)
dp[i][0] = 0;
for(int i = 0;i<=m;i++)
dp[0][i] = 0;
for(int i = 1;i<=m;i++) {
for(int j = i;j<=nlen;j++) {
if(i==1)dp[j][i] = dp[j-1][i]+num[j];
else {
dp[j][i] = dp[nlen][1];
for(int k = i-1;k<j;k++) {
temp = dp[k][i-1]>(dp[j][1]-dp[k][1])?dp[k][i-1]:(dp[j][1]-dp[k][1]);
dp[j][i] = dp[j][i]<temp?dp[j][i]:temp;
}
}
}
} for(int i = 1;i<=m;i++) {
for(int j = 1;j<=nlen;j++)
System.out.print(dp[j][i]+" ");
System.out.println();
} return (int) dp[nlen][m];
}

动态规划——Split Array Largest Sum的更多相关文章

  1. [LeetCode] Split Array Largest Sum 分割数组的最大值

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

  2. Split Array Largest Sum

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

  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. Split Array Largest Sum LT410

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

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

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

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

  8. 【leetcode】410. Split Array Largest Sum

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

  9. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

随机推荐

  1. Django---ORM框架

    一.get请求和post请求 GET请求: 1. 浏览器请求一个页面2. 搜索引擎检索关键字的时候 POST请求:1. 浏览器向服务端提交数据,比如登录/注册等 HTTP中GET与POST的区别 ht ...

  2. django系列 2 :启动应用,目录结构解读

    来源:https://docs.djangoproject.com/en/2.1/intro/tutorial01/ 该教程是创建一个用于投票的网页. 1.使用命令创建site 进入要创建site的目 ...

  3. centos7下tomcat8.5安装部署与优化

    转自:https://www.cnblogs.com/busigulang/articles/8529719.html centos 7 Tomcat 8.5 的安装及生产环境的搭建调优 一 安装to ...

  4. HTML词法和语法

    1. 词 token 专业不是计算机的博主比较尴尬,一直以为token就是验证身份用的标识 token —— 表示 “最小有意义的单元” 以这个简单的p标签为例,我们分析哪些是token: <p ...

  5. 主机管理+堡垒机系统开发:strace命令及日志解析(五)

    一.strace命令简介 测试命令截图 第一个窗口执行命令如下 [root@elk ~]# w 16:51:56 up 3 days, 6:01, 3 users, load average: 0.0 ...

  6. 第十二节:MVC中的一些特殊优化

    一. 删除WebForm视图引擎 在MVC框架中检索视图的顺序为:当前控制器下对应的文件夹的aspx文件→share文件夹aspx文件→当前控制器下对应文件夹的cshtml文件→share文件夹的cs ...

  7. Android App性能测试之二:CPU、流量

    CPU---监控值的获取方法.脚本实现和数据分析 1.获取CPU状态数据 adb shell dumpsys cpuinfo | findstr packagename 自动化测试脚本见cpustat ...

  8. Linux下使用crontab对MYSQL进行备份以及定时清

      数据备份是一个项目必需的工作,保证数据库出问题时,将损失减小到最低.本文记录下linux服务器中使用脚本对MYSQL数据备份,并且定时清除7天前的备份. crontab定时备份 1.创建备份目录 ...

  9. 330 div+css Experience

    今天学习的div,感觉对编辑html更为方便快捷,但还是需要多练,多熟悉一下思路和逻辑方式 越来越感觉,代码不是重要的,重要的是方向和思路,am的float clearfloat 及属性,还有over ...

  10. WPS或xls 数据分列 清洗

    一 .一般分离 时间:2017年11月27日14:55:12 数据如下: 501陈**:田莨铺58 502陈**:田莨铺58 503陈**.六麻杨冲58元 504陈**.石脚哗.200元 505陈** ...