题意大概就是,给定一个包含非负整数的序列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. 深入理解JVM(1)——栈和局部变量操作指令

    将常量压入栈的指令 aconst_null 将null对象引用压入栈iconst_m1 将int类型常量-1压入栈iconst_0 将int类型常量0压入栈iconst_1 将int类型常量1压入栈i ...

  2. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. Pandas系列(十四)- 实战案例

    一.series import pandas as pd import string #创建Series的两种方式 #方式一 t = pd.Series([1,2,3,4,43],index=list ...

  4. ArcGis Classic COM Add-Ins插件开发的一般流程 C#

    COM add-ins是我对这种开发方式的称呼,Esri的官方文档里称其为“Extending ArcObject”或者“Classic COM extensibility”,Esri所称的addin ...

  5. [再寄小读者之数学篇](2015-06-24 Series)

    (AMM. Problems and Solutions. 2015. 03) Let $\sed{a_n}$ be a monotone decreasing sequence of real nu ...

  6. django drf 基础学习4

    0  简介:介绍ModelViewSet基本使用规则1 views引用以及初始化   from rest_framework.viewsets import ModelViewSet     clas ...

  7. 401AM 随笔~ 322~330 的总结

    web简介:html:超文本标记语言css:层叠样式表 或者css样式JavaScript:一门脚本语言前端:html css javascript<!---->:注释段落与文字p.b加粗 ...

  8. 再说C模块的编写(2)

    [前言] 在<再说C模块的编写(1)>中主要总结了Lua调用C函数时,对数组和字符串的操作,而这篇文章将重点总结如何在C函数中保存状态. 什么叫做在C函数中保存状态?比如你现在使用Lua调 ...

  9. 树·AVL树/平衡二叉树

    1.AVL树 带有平衡条件的二叉查找树,所以它必须满足条件: 1 是一棵二叉查找树 2 满足平衡条件 1.1 平衡条件: 1)严格的平衡条件:每个节点都必须有相同高度的左子树和右子树(过于严格而不被使 ...

  10. unity 使用方法

    1.Rotaion 想要设定一个实例的rotation的时候不能使用Vector3来直接设定:应改为 rotation = Quaternion.Euler (0.0f, 180.0f, 0.0f); ...