【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 <= 500
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的更多相关文章
- 【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 ...
随机推荐
- Linux驱动开发6——DDR内存分配
1.kmalloc和kfree #include <linux/slab.h> void *kmalloc(size_t size, int flags); flag: GFP_ATOMI ...
- original和source的区别
最近跟网页翻译怼上了,在给翻译前的页面起名是纠结于使用original page还是source page,就查了一下original和source的区别. original: n. 原件:原作:原物 ...
- github.com/oschwald/maxminddb-golang 安装报错
安装 maxminddb-golang错误: dill@ubuntu-vm:~/workspace/go/src/github.com$ go get github.com/oschwald/maxm ...
- Python笔记(二十二)_魔法方法_基本魔法方法
__init__(self[,...]) __init__和__new__组成python的构造器,但__init__更多的是负责初始化操作,相当于一个项目中的配置文件,__new__才是真正的构造函 ...
- SQL server 2008r2 file is corrupt
下载的SQLManagement studio有问题,重新下载一遍后再安装就好了.安装顺序没问题. 在卸载SQL Server开始——运行:输入regedit 进入注册表编辑器,进入之后执行下列操作: ...
- [Git] 006 在本地新建一个仓库
1. 方法一 1.1 思路 在 GitHub 上新建一个仓库 clone 到本地 1.2 行动 1.2.1 在 GitHub 上选好自己已有的仓库 点击 "Clone or download ...
- [Markdown] 02 简单应用 第二弹
目录 4. 插入链接 4.1 Markdown 的方式 用法 1 用法 2 4.2 HTML5 的方法 用法 1 用法 2 5. 插入图片 5.1 使用网络地址 5.2 使用本地地址 5.2.1 小括 ...
- POJ 1383题解(树的直径)(BFS)
题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...
- IETester——用来测试IE5.5~IE11兼容性的工具
IETester是一款ie浏览器多版本测试工具,能很方便在ie5.5,ie6,ie7,ie8,ie9,ie10,ie11切换,只需安装一个软件,就可以解决N多ie浏览器的问题,满足大部分IE浏览器兼容 ...
- 《快学scala》读书笔记(1)
第一章 基础 1.安装scala解释器 (1)scala-2.12.1.msi (2)配置环境变量:SCALA_HOME = D:\Program Files\scala Path= %SCALA_H ...