动态规划——Split Array Largest Sum
If n is the length of array, assume the following constraints are satisfied:
1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)
Input:
nums = [7,2,5,10,8]
m = 2
18
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值的影响,由于是要分成连续的序列,所以最后一个元素只能与它前面的若干元素组成子序列,需要一个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])),这个题可以很明显的看出动态规划的最优子结构
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的更多相关文章
- [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
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 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 ...
- Split Array Largest Sum LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- [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 ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
随机推荐
- IDEA 破解
推荐三篇文章 : 1: https://blog.csdn.net/nishiwodebocai21/article/details/71359619?fps=1&locationNu ...
- 安装python caffe过程中遇到的一些问题以及对应的解决方案
关于系统环境: Ubuntu 16.04 LTS cuda 8.0 cudnn 6.5 Anaconda3 编译pycaffe之前需要配置文件Makefile.config ## Refer to h ...
- Python的设计模式
设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...
- python 错误捕获机制分析
python语言是编程中使用率在Top 3之内的语言.python语言以灵活与简单著称,那么越是灵活的语言越需要判断出错的功力. 简单示例 以下是一个简单的错误程序,被除数不可为0,那么看看该代码的执 ...
- 在Spring框架中bean配置文件中constructor-arg标签中没有name元素?
bean配置文件出现错误的依赖: <beans <beans xmlns="http://www.springframework.org/schema/beans" ...
- Windows系统下查看某一进程下所有线程的dos命令
1.查看进程 pslist或 tasklist 注:若出现“pslist不是外部或内部命令,也不是可运行的程序....”,需要去TechNet官网下载psTools(链接https://technet ...
- 四十九、进程间通信——System V IPC 之消息队列
49.1 System V IPC 介绍 49.1.1 System V IPC 概述 UNIX 系统存在信号.管道和命名管道等基本进程间通讯机制 System V 引入了三种高级进程间通信机制 消息 ...
- centos7 下 nfs 搭建总结
nfs一般用于生产环境磁盘空间不足导致数据无法写入,从而通过异机远程挂载磁盘方式解决问题. 一. rpm -qa | grep nfs-utils rpm -qa | grep rpcbind 二. ...
- Python3:判断三角形的类型
# 判断三角形类型def triangle(a,b,c): if a>0 and b>0 and c>0: if a+b>c and b+c>a and a+c>b ...
- LeetCode第十题-正则表达式匹配
Regular Expression Matching 问题简介:给定字符串,给定匹配模式,判断字符串是否满足匹配模式 问题详解:一共有两种特殊模式: ‘.’ 匹配任何单个字符 ‘*’ 匹配前面元素的 ...