原题链接在这里: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. 1 <= K <= A.length <= 500
  2. 0 <= 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的更多相关文章

  1. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  2. 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  ...

  3. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  4. 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 ...

  5. [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 如何同时读取 TDateTimePicker 的 Date 和 Time ?

    由于 TDateTimePicker 只能用于日期或时间,不能同时使用.如果将Kind属性设置为dtkDate,则可以指定自定义DATE格式,但忽略任何TIME格式,并且Time未定义使用该属性.如果 ...

  2. Redis初识01 (简介、安装、使用)

    一.Reids介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  3. 【LEETCODE】71、验证二叉树的前序序列化

    简单粗暴,代码有待优化,不过自己独立完成,没有参考任何材料,还是比较满意的 package y2019.Algorithm.stack.medium; import java.util.Stack; ...

  4. C#JsonConvert.DeserializeObject反序列化json字符

    需求:需要把第一个id替换掉,在序列化成json dynamic dyn = Newtonsoft.Json.JsonConvert.DeserializeObject(json); foreach ...

  5. Sqlserver 总结(2) 存储过程

    目录 写在前面 什么是存储过程 存储过程的优点 存储过程的分类 1.只返回单一记录集的存储过程 2.没有输入输出的存储过程 3.有返回值的存储过程 4.有输入参数和输出参数的存储过程 5.同时具有返回 ...

  6. win10 mars xlog编译

    win10 mars xlog编译   一. 环境准备 安装 cmake 以及 python2.7, 以及下载 ndk-r16b,并配置环境变量 NDK_ROOT 指向 ndk 路径. 如果是 Win ...

  7. Java自学-类和对象 引用

    什么是Java中的引用? 引用的概念,如果一个变量的类型是 类类型,而非基本类型,那么该变量又叫做引用. 步骤 1 : 引用和指向 new Hero(); 代表创建了一个Hero对象 但是也仅仅是创建 ...

  8. PHP CI框架调试开启报错信息方法

    方法如下三种: 1.php.ini 设置 display_errors = On error_reporting = E_ALL | E_STRICT 2.ci index.php 设置 define ...

  9. data:image/png;base64应用

    原文:https://blog.csdn.net/deng_xj/article/details/93731850 data:image/png;base64应用 我们知道任何图片都可以通过base6 ...

  10. 笔记本端查看以前的wifi密码

    家里老人忘记密码了.好像是我改了从,我也忘了,手中安卓手机root后也没找到记录密码的文件,水果机懒得弄了,突然想起来电脑还有记录,应该可以找到. 此篇也顺带记录下怎么通过手中笔记本找到以前练过的wi ...