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 <= 500
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的更多相关文章
- 【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 ...
随机推荐
- 修改Jupyter Notebook的默认打开路径
一: (也可以直接将删除的部分修改成所要存储的文件路径,之后三个步骤就可以省去了) 二: 打开Windows的cmd,在cmd中输入jupyter notebook --generate-config ...
- python 递归\for循环_斐波那契数列
# 递归 def myAdd(a, b): c = a + b print(c) if c > 100: return return myAdd(a + 1, c) #最大递归深度是1000 m ...
- Drool7s kmodule的作用--系列02课
本文是介绍drool7s kmodule. 一.为什么komdule.xml文件一定要放在resources下的META-INF文件夹中 --->直接看源码吧,请看下图,应该都知道为什么要放在固 ...
- javaweb之添加学生信息
1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单选框或下拉框 ...
- java之基本技术点总结博客
泛型的理解 聊一聊-JAVA 泛型中的通配符 T,E,K,V,? 类,接口的继承和实现的规则 类与类之间只能继承,并且是单继承,可以多级继承 类与接口之间可以实现,一个类可以实现多个接口 接口和接口之 ...
- .net core中的Session以及HttpContext对象使用小结
session用于识别用户并保持用户信息,就是一个会话 ,在浏览器不关闭的前提下,可以保存用户的信息,比如登录的保存用户信息从一个网页跳转到另一个网页,你的用户信息就可以用session. .net ...
- C#explicit和implicit关键字实现类型转换
using System; namespace ConsoleTest { class Program { static void Main(string[] args) { //implicit 隐 ...
- 【解决方案】文件上具有 Web 标记,请删除 Web 标记
错误: 无法处理文件 Form1.resx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记.要想处理这些文件,请删除 Web 标记. 解决方法: 文件-右键-属性 点击”解 ...
- writeAsBytes writeAsString
import 'dart:io';import 'dart:convert'; main()async{ File a = File('C:\\aria2\\1.txt'); var c = read ...
- 关于justify-content属性的再学习(区分三个属性)
justify-content属性: 用来表示可伸缩项目在主轴方向上的对齐方式: 取值范围为flex-start,flex-end,center,space-between,space-around: ...