当前位置:网站首页>LeetCode 1043. 分隔数组以得到最大和 每日一题
LeetCode 1043. 分隔数组以得到最大和 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给你一个整数数组 arr,请你将该数组分隔为长度最多为 k 的一些(连续)子数组。分隔完成后,每个子数组的中的所有值都会变为该子数组中的最大值。
返回将数组分隔变换后能够得到的元素最大和。
注意,原数组和分隔后的数组对应顺序应当一致,也就是说,你只能选择分隔数组的位置而不能调整数组中的顺序。
示例 1:
输入:arr = [1,15,7,9,2,5,10], k = 3
输出:84
解释:
因为 k=3 可以分隔成 [1,15,7] [9] [2,5,10],结果为 [15,15,15,9,10,10,10],和为 84,是该数组所有分隔变换后元素总和最大的。
若是分隔成 [1] [15,7,9] [2,5,10],结果就是 [1, 15, 15, 15, 10, 10, 10] 但这种分隔方式的元素总和(76)小于上一种。
示例 2:输入:arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
输出:83
示例 3:输入:arr = [1], k = 1
输出:1
提示:
1 <= arr.length <= 500
0 <= arr[i] <= 109
1 <= k <= arr.length来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/partition-array-for-maximum-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
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];
}
//得到指定区间的最大值
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;
}
}边栏推荐
- Pisa-Proxy SQL 解析之 Lex & Yacc
- 【DesignMode】外观模式 (facade patterns)
- JS中null NaN undefined这三个值有什么区别
- Record the migration process of a project
- Lowcode: four ways to help transportation companies enhance supply chain management
- 谎牛计数(春季每日一题 53)
- os、sys、random标准库主要功能
- ByteDance Android gold, silver and four analysis, Android interview question app
- 最新Android高级面试题汇总,Android面试题及答案
- 模块六
猜你喜欢

Pycharm terminal enables virtual environment

Sort out several important Android knowledge and advanced Android development interview questions

预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
ByteDance Android gold, silver and four analysis, Android interview question app
3000 words speak through HTTP cache

Personal notes of graphics (1)

网关Gateway的介绍与使用

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

Statistical learning method -- perceptron
![[medical segmentation] attention Unet](/img/f4/cf5b8fe543a19a5554897a09b26e68.png)
[medical segmentation] attention Unet
随机推荐
一文读懂数仓中的pg_stat
字节跳动高工面试,轻松入门flutter
字节跳动Android面试,知识点总结+面试题解析
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
值得一看,面试考点与面试技巧
LeetCode-SQL第一天
time标准库
AutoLISP series (3): function function 3
Prediction - Grey Prediction
Three. JS series (3): porting shaders in shadertoy
Laravel service provider instance tutorial - create a service provider test instance
记录Servlet学习时的一次乱码
Three. JS series (1): API structure diagram-1
最新Android高级面试题汇总,Android面试题及答案
Talk about the realization of authority control and transaction record function of SAP system
Direct dry goods, 100% praise
Sqlserver2014+: create indexes while creating tables
Vs2019 configuration matrix library eigen
【C 语言】 题集 of Ⅹ
整理几个重要的Android知识,高级Android开发面试题