当前位置:网站首页>Leetcode-1043. Separate arrays for maximum sum
Leetcode-1043. Separate arrays for maximum sum
2022-06-12 05:57:00 【Taoist scholar】
link
1043. Separate the arrays to get the maximum and
subject
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
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 :83Example 3:
Input :arr = [1], k = 1
Output :1
explain
1 <= arr.length <= 5000 <= arr[i] <= 10e91 <= k <= arr.length
Ideas Dynamic programming (DP)
We use an array dp[i] Express arr Every position in the array i The maximum solution of the sequence composed of all previous numbers , We use K=3 For example ,dp[i] There are three situations :
![dp[i] = dp[i-1]+1*arr[i]](http://img.inotgo.com/imagesLocal/202203/01/202203010613332405_0.gif)
![dp[i] = dp[i-2]+2*max(arr[i],arr[i-1])](http://img.inotgo.com/imagesLocal/202203/01/202203010613332405_1.gif)
![dp[i] = dp[i-3]+3*max(arr[i],arr[i-1],arr[i-2])](http://img.inotgo.com/imagesLocal/202203/01/202203010613332405_2.gif)
and dp[i] Is the above K The maximum value in each case , thus , We can get the state transfer equation :
![dp[i]=max(dp[i],dp[j]+(i-j)*MAX)](http://img.inotgo.com/imagesLocal/202203/01/202203010613332405_6.gif)
among ,
, and MAX Then for
To
Maximum value between .
From this two-layer traversal , The first layer traverses each location , The second traversal is for k In this case , The maximum value at each position can be calculated dynamically , And the answer is the result of the last position .
C++ Code
class Solution {
public:
int maxSumAfterPartitioning(vector<int>& A, int K) {
// f[i] = max(f[j] + (i-j)*max(A[j..i]))
int n = A.size();
vector<int> f(n+1);
for (int i = 0; i <= n; i++) {
int curMax = 0;
// consider past integers [j...i]
for (int j = i-1; (i-j)<=K && j>=0; j--) {
curMax = max(curMax, A[j]);
f[i] = max(f[i], f[j] + (i-j)*curMax);
}
}
return f[n];
}
};
边栏推荐
- Research Report on market supply and demand and strategy of China's digital camera lens industry
- [gin] gin framework for golang web development
- Conversion of Halcon 3D depth map to 3D image
- Tabulation skills and matrix processing skills
- The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
- XML参数架构,同一MTK SW版本兼容两套不同的音频参数
- Halcon 3D 深度图转换为3D图像
- Win10 desktop unlimited refresh
- [speech] how to customize ring back tone according to different countries
猜你喜欢

Project technical structure

yolov5

Stack and queue classic interview questions

Review notes of naturallanguageprocessing based on deep learning

March 22, 2021

Understanding of distributed transactions

MySQL master-slave, 6 minutes to master

TCP and UDP introduction

CCF noi2022 quota allocation scheme

Login authentication filter
随机推荐
Redis persistence
[gpio] how to modify / display GPIO status through ADB shell
Flex/fixed upper, middle and lower (mobile end)
个人申请OV类型SSL证书
log4j 1. X upgrade to 2 Solution of X dependency incompatibility
Json-c common APIs
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
How much Ma is the driving current of SIM card signal? Is it adjustable?
A month's worth of DDD will help you master it
Annotation configuration of filter
Conversion of Halcon 3D depth map to 3D image
Redis cache data consistency and problems
Chapter 7 - pointer learning
Automatic annotation of target detection based on lffd model to generate XML file
Recursive implementation of exponential, permutation and combination enumerations
Laravel8 when search
登录验证过滤器
Unity vscode cannot jump to definition
Glossary of Chinese and English terms for pressure sensors
Thesis reading_ Figure neural network gin