当前位置:网站首页>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;
}
}边栏推荐
- Introduction and use of gateway
- Opencv personal notes
- 整理几个重要的Android知识,高级Android开发面试题
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- 【Seaborn】组合图表:PairPlot和JointPlot
- 数据中台落地实施之法
- QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
- 模拟Servlet的本质
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
猜你喜欢
Direct dry goods, 100% praise

Arduino 控制的双足机器人

The latest interview experience of Android manufacturers in 2022, Android view+handler+binder

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

二叉搜索树(基操篇)

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars

Record the migration process of a project

模块六

time标准库

skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
随机推荐
LeetCode 403. 青蛙过河 每日一题
Inner monologue of accidental promotion
time标准库
LeetCode 152. 乘积最大子数组 每日一题
AutoLISP series (2): function function 2
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
【图像传感器】相关双采样CDS
Opencv personal notes
数据中台落地实施之法
值得一看,面试考点与面试技巧
Binary search tree (features)
作为Android开发程序员,android高级面试
最新阿里P7技术体系,妈妈再也不用担心我找工作了
URL和URI的关系
应用在温度检测仪中的温度传感芯片
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Horizontal and vertical centering method and compatibility
Opportunity interview experience summary
Direct dry goods, 100% praise
Geoserver2.18 series (5): connect to SQLSERVER database