题意大概就是,给定一个包含非负整数的序列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强制转换即可
 
  1. public int splitArray(int[] nums,int m) {
  2. int nlen = nums.length;
  3. int[]num = new int[nlen+1];
  4. double[][]dp = new double[nlen+1][m+1];
  5. double temp = 0;
  6. num[0] = nums.length;
  7. for(int i = 1;i<=nlen;i++)
  8. num[i] = nums[i-1];
  9. for(int i = 0;i<=nlen;i++)
  10. dp[i][0] = 0;
  11. for(int i = 0;i<=m;i++)
  12. dp[0][i] = 0;
  13. for(int i = 1;i<=m;i++) {
  14. for(int j = i;j<=nlen;j++) {
  15. if(i==1)dp[j][i] = dp[j-1][i]+num[j];
  16. else {
  17. dp[j][i] = dp[nlen][1];
  18. for(int k = i-1;k<j;k++) {
  19. temp = dp[k][i-1]>(dp[j][1]-dp[k][1])?dp[k][i-1]:(dp[j][1]-dp[k][1]);
  20. dp[j][i] = dp[j][i]<temp?dp[j][i]:temp;
  21. }
  22. }
  23. }
  24. }
  25.  
  26. for(int i = 1;i<=m;i++) {
  27. for(int j = 1;j<=nlen;j++)
  28. System.out.print(dp[j][i]+" ");
  29. System.out.println();
  30. }
  31.  
  32. return (int) dp[nlen][m];
  33. }

动态规划——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. pgsql 并行相关配置

  2. Redmine简易安装与系统优化

    安装版本为bitnami-redmine-2.6.5-0 ,用的Bitnami的一键安装包 . 下载地址https://bitnami.com/stack/redmine/installer 简要安装 ...

  3. 用django2.1开发公司官网(上)

    1.在MySQL中新建数据库 show databases;//查看已经有的数据库 create database guanwang; 2.新建django项目guan 1.使用pycharm新建dj ...

  4. java-查看java源码

    安装jdk后,自己的pc下自然而然就可以找到java的源码包.

  5. 实战Google深度学习框架-C3-TensorFlow入门

    第三章:TensorFlow入门 TensorFlow存在计算模型,数据模型和运算模型(本文用TF代表TensorFlow) 3.1 计算模型-计算图 3.1.1 计算图的概念 TensorFlow这 ...

  6. 用Java编写第一个区块链

    原文地址:https://www.cnblogs.com/zacky31/p/9057193.html 目标: 创建一个最基本的“区块链” 实现一个简单的挖矿系统 前提: 对面向对象编程有一定的基础 ...

  7. 微信小程序 mpvue vant

    Mpvue中使用Vant Weapp组件库 https://segmentfault.com/a/1190000016228410?utm_source=tag-newest 小程序采坑记 mpvue ...

  8. Latex "Error: File ended while scanning use of \@xdblarge"

    Latex 编译时出现 Error: File ended while scanning use of \@xdblarge" 是因为少了一个 }...

  9. [物理学与PDEs]第2章习题12 严格凸性的转换

    设 $L=L(\xi_0,\xi_1,\cdots,\xi_n)$ 关于变量 $\xi_0>0,\xi_1,\cdots,\xi_n$ 为严格凸的. 证明函数 $$\bex M=\cfrac{1 ...

  10. Python3 字符串与hex之间的相互转换

    在字符串转换上,python2和python3是不同的,在查看一些python2的脚本时候,总是遇到字符串与hex之间之间的转换出现问题,记录一下解决方法. 1. 在Python2.7.x上,hex字 ...