题目如下:

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]

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

  1. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  2. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

  3. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  5. 【leetcode】1020. Partition Array Into Three Parts With Equal Sum

    题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...

  6. 【leetcode】915. Partition Array into Disjoint Intervals

    题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...

  7. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. ORACLE Physical Standby DG 之fail over

    SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;确认下是否存在日志间隙,发现gap现象,说明failo ...

  2. redispy

    w wuser@ubuntu:~/redispy$ redis-cli > keys * ) "w" ) "wpy" > set w1 w1valu ...

  3. 三十七、python中的logging介绍

    A.单文件日志 import logging#定义日志文件#文件格式logging.basicConfig(filename='log.log', format='%(asctime)s-%(name ...

  4. CBAM: Convolutional Block Attention Module

    1. 摘要 作者提出了一个简单但有效的注意力模块 CBAM,给定一个中间特征图,我们沿着空间和通道两个维度依次推断出注意力权重,然后与原特征图相乘来对特征进行自适应调整. 由于 CBAM 是一个轻量级 ...

  5. day50—JavaScript鼠标拖拽事件

    转行学开发,代码100天——2018-05-05 今天通过鼠标拖拽事件复习巩固一下鼠标事件. 鼠标拖拽事件需要记住两点: 1.距离不变 2.鼠标事件(按下,移动,抬起) <div id=&quo ...

  6. MVC2: 路由 及 遇到问题记录

    MVC 路由 重定向 问题记录 1)MVC 路由 入口方法: (Global.asax)Application_Start()--->(App_Start/RouteConfig.cs)Regi ...

  7. 【SD系列】SAP 创建销售订单-用外部给号的方法步骤

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP 创建销售订单-用外部给号的方 ...

  8. 疯狂Java学习

    面向对象(下) 6.3:类成员:又讲了一遍static修饰的类成员:   Singleten(单例)类: 通过封装的方式,实现了一个类只能创建一次,应该是为了更好编写代码,创造的一个概念. 6.4:f ...

  9. 2 Vue.js基础

    1 简易计算器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...