【leetcode】1043. Partition Array for Maximum Sum

题目如下:

Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

Return the largest sum of the given array after partitioning.

Example 1:

Input: A = [1,15,7,9,2,5,10], K = 3
Output: 84
Explanation: A becomes [15,15,15,9,10,10,10]

Note:

  1. 1 <= K <= A.length <= 500
  2. 0 <= A[i] <= 10^6

解题思路:假设dp[i][j] 表示第i个元素为第j个子数组的最后一个元素时,A[0:i]可以获得的最大值。那么有dp[i][j] = max(dp[i][j], dp[m][j-1] + max_val[m+1][i] * (i-m))   ( i-k < m < i) 。

代码如下:

class Solution(object):
    def maxSumAfterPartitioning(self, A, K):
        """
        :type A: List[int]
        :type K: int
        :rtype: int
        """
        import math
        dp = []
        max_val = []
        sub = int(math.ceil(float(len(A))/K))
        for i in A:
            dp.append([0] * sub)
            max_val.append([0]*len(A))
        for i in range(len(A)):
            max_val[i][i] = A[i]
            for j in range(i+1,len(A)):
                max_val[i][j] = max(max_val[i][j-1],A[j])
        dp[0][0] = A[0]
        for i in range(len(A)):
            for j in range(sub):
                #print i,j
                if i-K< 0:
                    dp[i][j] = max(A[0:i+1]) * (i+1)
                else:
                    for m in range(i-K,i):
                        dp[i][j] = max(dp[i][j], dp[m][j-1] + max_val[m+1][i] * (i-m))
        #print dp
        return dp[-1][-1]

原文地址:https://www.cnblogs.com/seyjs/p/11044749.html

时间: 2024-10-13 14:16:28

【leetcode】1043. Partition Array for Maximum Sum的相关文章

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

【Leetcode】Shuffle an Array

题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and retur

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

【LeetCode】189. Rotate Array 解题小结

题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 这道题目 有很多种解法,我用的是这种,相对也好理解,先所有元素reverse,再0-(n-k)的元素reverse,然后(n-k)-n的元素reverse. class Solution { pub

【LeetCode】Product of Array Except Self

Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2

【LeetCode】189. Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

【leetcode】Convert Sorted Array to Binary Search Tree (easy)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)

【leetcode】Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 采用二分递归. 1 class Solution { 2 public: 3 TreeNode *createTree(vector<int> &num,int begin,int end) 4 { 5 if(begin>end) return NULL; 6 int mid=(begi

【leetcode】Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialize