【leetcode】1043. Partition Array for Maximum Sum
题目如下:
Given an integer array
A, you partition the array into (contiguous) subarrays of length at mostK. 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 <= K <= A.length <= 5000 <= 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的更多相关文章
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【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 ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- 消息队列之 ActiveMQ
简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效.可扩展.稳定.安全的企业级消息通信. 它的设计目标是提供标准的.面向消息的.多语言 ...
- tcp中的常见定时器
(1)超时重传定时器tcp的靠谱特性,通过确认机制,保证每一个包都被对方收到,那么什么时候需要重传呢?就是靠这个超时重传定时器,每次发送报文前都启动这个定时器,如果定时器超时之前收到了应答则关闭定时器 ...
- UI自动化之日志的处理
写自动化时,我们常常希望打印出浏览器的操作记录,也同时希望报错的记录能够保留并用于问题的排查,这时候可以用到loggging模块 目录 1.logging文件 2.调用日志 1.logging文件 # ...
- 关于hadoop登陆kerberos时设置环境变量问题的思考
中心思想,设置kerberos环境变量时,发现JDK源码当中的一个问题,故描述如下. 在平时的使用中,如果hadoop集群配置kerberos认证的话,使用java访问hdfs或者hive时,需要先进 ...
- Tensorflow创建和读取17flowers数据集
http://blog.csdn.net/sinat_16823063/article/details/53946549 Tensorflow创建和读取17flowers数据集 标签: tensorf ...
- vue-methods方法与computed计算属性的差别
好吧,我就是单纯的举个例子:实现显示变量 message 的翻转字符串 第一种:methods:我们可以通过在表达式中调用方法来达到同样的效果: 第二种:computed:计算属性 上面的2中方法都实 ...
- IDEA开发环境设置
IDEA开发环境设置 1.关闭自动更新 IntelliJ IDEA默认会自动进行版本的更新,在网络异常时经常会导致各种各样的问题,因此强烈建议关闭自动更新. File->Settings 2.隐 ...
- 关于服务器无法在已发送http表头之后设置状态问题
Response.ClearHeaders()方法 ClearHeaders方法只删除头信息,而不删除Response显示输出信息. this.Response.BufferOutput = true ...
- Python 中的 os 模块常见方法?
os.remove() 删除文件 os.rename() 重命名文件 os.walk() 生成目录树下的所有文件名 os.chdir() 改变目录 os.mkdir/makedirs 创建目录/多层目 ...
- restTemplate工具类
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.sprin ...