当前位置:网站首页>LeetCode 1043. Separate the array to get the maximum and daily questions
LeetCode 1043. Separate the array to get the maximum and daily questions
2022-07-07 16:58:00 【@Little safflower】
Problem description
Give you an array of integers arr, Please separate the array into at most k Some of ( continuity ) Subarray . After separation , All values in each subarray will become the maximum value in that subarray .
Returns the maximum sum of elements that can be obtained after separating and transforming the array .
Be careful , The corresponding order of the original array and the separated array should be the same , in other words , You can only choose the position of the separated array, but you can't adjust the order in the array .
Example 1:
Input :arr = [1,15,7,9,2,5,10], k = 3
Output :84
explain :
because k=3 Can be separated into [1,15,7] [9] [2,5,10], The result is [15,15,15,9,10,10,10], And for 84, Is the largest sum of all elements of the array after separation and transformation .
If separated into [1] [15,7,9] [2,5,10], The result is [1, 15, 15, 15, 10, 10, 10] But the sum of the elements of this separation (76) Less than one .
Example 2:Input :arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
Output :83
Example 3:Input :arr = [1], k = 1
Output :1
Tips :
1 <= arr.length <= 500
0 <= arr[i] <= 109
1 <= k <= arr.lengthsource : Power button (LeetCode)
link :https://leetcode.cn/problems/partition-array-for-maximum-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int maxSumAfterPartitioning(int[] arr, int k) {
int n = arr.length;
int[] dp = new int[n];
for(int i = 0;i < k;i++){
dp[i] = getMaxValue(arr,0,i) * (i + 1);
}
for(int i = k;i < n;i++){
for(int j = 1;j <= k;j++){
dp[i] = Math.max(dp[i],dp[i - j] + getMaxValue(arr,i - j + 1,i) * j);
}
}
return dp[n - 1];
}
// Get the maximum value of the specified interval
public int getMaxValue(int[] arr,int left,int right){
int ans = 0;
for(int i = left;i <= right;i++){
ans = Math.max(ans,arr[i]);
}
return ans;
}
}边栏推荐
猜你喜欢
随机推荐
Prometheus API deletes all data of a specified job
两类更新丢失及解决办法
如何选择合适的自动化测试工具?
Cesium (4): the reason why gltf model is very dark after loading
蓝桥杯 决赛 异或变换 100分
LeetCode 300. 最长递增子序列 每日一题
二叉搜索树(特性篇)
[designmode] proxy pattern
【MySql进阶】索引详解(一):索引数据页结构
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
LeetCode 1654. 到家的最少跳跃次数 每日一题
DNS 系列(一):为什么更新了 DNS 记录不生效?
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
Arduino 控制的双足机器人
【DesignMode】模板方法模式(Template method pattern)
Personal notes of graphics (4)
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
射线与OBB相交检测









