LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/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 <= K <= A.length <= 5000 <= A[i] <= 10^6
题解:
When encounter such kind of problem.
Could think from a simpler example. Say only one element, then 2 elements and more. Virtualize them, find routines.
Use array dp to memorize maxmimum sum up to i.
If, A = [1], then A becomes A[1]. dp = [1].
A = [1, 15], then A becomes A[15, 15]. dp = [1, 30].
A = [1, 15, 7], then A becomes A[15, 15, 15]. dp = [1, 30, 45].
A = [1, 15, 7, 9], then A becomes A[15, 15, 15, 9]. dp = [1, 30, 45, 54].
...
The routine is like from i back k(<= K) steps, find the maxmimum element, curMax * k + dp[i-k](if available).
Finally return dp[A.length-1].
Time Complexity: O(n*K). n = A.length.
Space: O(n).
AC Java:
class Solution {
public int maxSumAfterPartitioning(int[] A, int K) {
int len = A.length;
int [] dp = new int[len];
for(int i = 0; i<len; i++){
dp[i] = Integer.MIN_VALUE;
int curMax = A[i];
for(int k = 1; k<=K & i-k+1>=0; k++){
curMax = Math.max(curMax, A[i-k+1]);
dp[i] = Math.max(dp[i], (i-k<0 ? 0 : dp[i-k]) + curMax*k);
}
}
return dp[len-1];
}
}
LeetCode 1043. Partition Array for Maximum Sum的更多相关文章
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- LeetCode 1013 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 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- [LeetCode] 698. Partition to K Equal Sum Subsets
Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...
- [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
随机推荐
- 用LabVIEW做声源定位系统
前一阵子,研发部举办了为期三天的第一届Innovation Day,让大家用3天时间去完成工作之外的一些创意.有人做微信小程序,有人继续研究一些工作中用到的Tool,有人把一直想解决而没时间解决的老b ...
- U9期间在手量控制
1.路径 库存管理->参数控制 2.设置节点 期间在手量控制时机 :单据实时控制.日关账控制.不控制 3.实现效果
- 深入玩转K8S之外网如何访问业务应用
有一个问题就是现在我的业务分配在多个Pod上,那么如果我某个Pod死掉岂不是业务完蛋了,当然也会有人说Pod死掉没问题啊,K8S自身机制Deployment和Controller会动态的创建和销毁Po ...
- CF891E Lust 生成函数
传送门 设在某一次操作之后的\(a\)数组变为了\(a'\)数组,那么\(\prod\limits_{i \neq x} a_i = \prod a_i - \prod a_i'\).那么就不难发现我 ...
- .NET Core随笔把数据库数据查出来转JSON并输出
直接创建WEB项目即可: public class Startup { //startup.cs是站点启动时具体做了哪些事情,主要是开启了一个mvc服务. public Startup(IConfig ...
- 2019 超级老板APPjava面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.超级老板等公司offer,岗位是Java后端开发,因为发展原因最终选择去了超级老板,入职一年时间了,也成为了面 ...
- Vue父子,子父,非父子组件之间传值
Vue组件基础 纯属随笔记录,具体详细教程,请查阅vue.js网站 子组件给父组件传值: <body> <div id="app"> <my-app& ...
- centos7 上Docker安装与启动
1. docker centos 文档地址 https://docs.docker.com/install/linux/docker-ce/centos/ 2. 安装环境说明: docker社区版 ...
- HDFS之安全模式
1.namenode启动的时候,首先将映像文件[fsimage]载入内存,并执行编辑日志[edits]中的各项操作. 2.一旦在内存中成功建立文件系统元数据的映射,则创建一个新的fsimage文件[这 ...
- 【RMAN】RMAN脚本中使用替换变量
[RMAN]RMAN脚本中使用替换变量--windows 下rman全备脚本 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也 ...