mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add «1043. Partition Array for Maximum Sum»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
45fb950e25
commit
f88f860d0d
1 changed files with 21 additions and 0 deletions
21
rs/partition-array-for-maximum-sum.rs
Normal file
21
rs/partition-array-for-maximum-sum.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use std::cmp;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_sum_after_partitioning(arr: Vec<i32>, k: i32) -> i32 {
|
||||
let mut dp: Vec<i32> = vec![0; arr.len() + 1];
|
||||
|
||||
for i in 1..=arr.len() {
|
||||
let mut m = 0;
|
||||
let mut m_sum = 0;
|
||||
|
||||
for j in (1..=k as usize).take_while(|j| i >= *j) {
|
||||
m = cmp::max(m, arr[i - j]);
|
||||
m_sum = cmp::max(m_sum, dp[i - j] + (j as i32) * m);
|
||||
}
|
||||
|
||||
dp[i] = m_sum;
|
||||
}
|
||||
|
||||
dp[arr.len()]
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue