原题链接在这里: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. CSP(noip)中的简单对拍写法

    以a+b为例 这是随机数据 #include<iostream> #include<cstdio> #include<ctime> using namespace ...

  2. 【C++/C】指针基本用法简介-A Summary of basic usage of Pointers.

    基于谭浩强老师<C++程序设计(第三版)>做简要Summary.(2019-07-24) 一.数组与指针 1. 指针数组 一个数组,其元素均为指针类型数据,该数组称为指针数组.(type_ ...

  3. CapsLock Enhancement via AutoHotKey

    上次写了一篇博文,讲如何通过AutoHotKey改造CaspLock,使其成为一个方便的编辑按键,并特意给出了设计的思路方便参考. 见地址:http://www.cnblogs.com/Vonng/p ...

  4. 《JAVA高并发编程详解》-类的加载过程简介

  5. Calico网络模型

    由于两台物理机的容器网段不同,我们完全可以将两台物理机配置成为路由器,并按照容器的网段配置路由表. 在物理机A中,我们可以这样配置:要想访问网段172.17.9.0/24,下一跳是192.168.10 ...

  6. Java 阿拉伯数字转换为中文大写数字

    Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...

  7. kubernetes使用securityContext和sysctl

    前言 在运行一个容器时,有时候需要使用sysctl修改内核参数,比如net..vm..kernel等,sysctl需要容器拥有超级权限,容器启动时加上--privileged参数即可.那么,在kube ...

  8. MongoDB和Java(1):Linux下的MongoDB安装

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  9. sense chrome扩展工具安装问题

    下载一个chrome的插件,只能在国内下了.有个什么插件网 .下载完之后是  .crx 格式的.直接拖到 chrome的扩展工具里,显示,插件有问题.无语 参考了一个网上的教程. 1.先将扩展名 .c ...

  10. C#中的委托、事件及事件的订阅

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...