Re: [闲聊] 每日LeetCode

楼主: SecondRun (雨夜琴声)   2024-02-03 15:00:23
练习一下DP
C# code:
public class Solution {
public int MaxSumAfterPartitioning(int[] arr, int k) {
int n = arr.Length;
int[] dp = new int[n+1];
int max;
for (int i=1; i<=n; i++)
{
max = 0;
for (int j=1; j<=Math.Min(i,k); j++)
{
max = Math.Max(max, arr[i-j]);
dp[i] = Math.Max(dp[i], dp[i-j] + max*j);
}
}
return dp[n];
}
}
作者: JIWP (JIWP)   2024-02-03 15:04:00
大师
作者: oin1104 (是oin的说)   2024-02-03 15:05:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com