分割数组的最小值
410 分割数组的最小值
1 class Solution(object): 2 def splitArray(self, nums, m): 3 """ 4 :type nums: List[int] 5 :type m: int 6 :rtype: int 7 """ 8 9 n=len(nums) 10 11 # preSum[i]表示 sum[0]...sum[i-1]闭区间的前缀和 12 preSum=[0 for j in range(n+1)] 13 # dp[i][j] 将j个数划分成i组时各个子数组的最大和 中的最小值 14 dp=[[float('inf') for j in range(n+1)] for i in range(m+1)] 15 16 dp[0][0]=0 17 18 # 求解前缀和数组 19 for i in range(1,n+1): 20 preSum[i]+=preSum[i-1]+nums[i-1] 21 22 for i in range(1,m+1): 23 for j in range(1,n+1): 24 for k in range(i-1,j): 25 tmp_max=max(dp[i-1][k], preSum[j]-preSum[k]) 26 dp[i][j]=min(dp[i][j],tmp_max) 27 28 return dp[m][n]