题目如下:

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these msubarrays.

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[i][j] 为把 0~j个元素分成i段时最大的一段和,例如 nums = [7,2,5,10,8],dp[0][1] 表示 第0个元素和第一个元素组成子数组的第一段。这里有几个约束条件:如果i是最后一段,那么j的取值只能是len(nums)-1;如果不是最后一段,那么j后面必须留下(m-i)个元素,因为任何一段子数组的长度都不能为0。假设第i-1段的区间范围是 a~b,那么很显然有 dp[i][j] = min( dp[i][j] , max(dp[i-1][m] , sum[m:j] )) ,其中 a <= m <= b,这里的意思是 dp[i-1][m] 表示为把 0~个元素分成i-1段时最大的一段和,那么在m~j这段区间的和如果大于 dp[i-1][m]  ,那么dp[i][j] 就应该等于 m~j这段区间的和,否则令dp[i][j] 等于dp[i-1][m] 。

代码如下:

class Solution(object):
def splitArray(self, nums, m):
"""
:type nums: List[int]
:type m: int
:rtype: int
"""
val = []
amount = 0
for i in nums:
amount += i
val.append(amount)
dp = [[float('inf')] * len(nums) for _ in range(m)] for i in range(len(nums) - m + 1):
dp[0][i] = val[i] for i in range(1,m):
if i == m - 1:
start = len(nums) - 1
end = len(nums)
else:
start = i
end = len(nums) - (m - i) + 1
for j in range(start,end):
for k in range(i-1,j):
dp[i][j] = min(dp[i][j], max(dp[i-1][k],val[j] - val[k])) #print dp
return dp[-1][-1]

最新文章

  1. 摄像头拍照,PHP输入流php://input的使用分析
  2. Electronic Payment App analysis
  3. hdu 4021 n数码
  4. 微信利用PHP创建自定义菜单的方法
  5. Alt.js的入门
  6. java如何得到GET和POST请求URL和参数列表
  7. 解决DataTable中的DataColumn类型默认为int类型时, 导致不能修改其列值为其他类型的解决办法
  8. Lua多重继承
  9. IE回车的怪异行为
  10. Android精品源码与技术博文
  11. CSS之clearfix清除浮动
  12. HTML学习——表单标签
  13. MonogoDB 查询小结
  14. 总结各类错误(always online)
  15. Pycharm:书签的使用
  16. Windows核心编程第一章.错误处理
  17. Python进阶教程001内置数据类型
  18. 环境判断:区别h5打开还是weixin打开?
  19. day24_python_1124
  20. 获取post请求数据工具类

热门文章

  1. nginx排查502错误
  2. log() exp()函数
  3. 【ABAP系列】SAP ABAP MIR7预制凭证BAPI
  4. 函数参数中经常见到的*args和**kwargs
  5. centos7 VM虚拟机在主机关机重启后,无法ping通
  6. 使用app.config中的数据对数据库链接信息初始化
  7. mysql先分组,然后取每个分组中的第2大的记录
  8. docker--搭建docker swarm集群
  9. [JS] 鼠标点击文本框清空默认值,离开文本框恢复默认值
  10. java中的继承关系