★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10852127.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

Return the largest sum of the given array after partitioning.

Example 1:

Input: A = [1,15,7,9,2,5,10], K = 3
Output: 84
Explanation: A becomes [15,15,15,9,10,10,10]

Note:

  1. 1 <= K <= A.length <= 500
  2. 0 <= A[i] <= 10^6

给出整数数组 A,将该数组分隔为长度最多为 K 的几个(连续)子数组。分隔完成后,每个子数组的中的值都会变为该子数组中的最大值。

返回给定数组完成分隔后的最大和。

示例:

输入:A = [1,15,7,9,2,5,10], K = 3
输出:84
解释:A 变为 [15,15,15,9,10,10,10]

提示:

  1. 1 <= K <= A.length <= 500
  2. 0 <= A[i] <= 10^6

32ms
 1 class Solution {
 2     func maxSumAfterPartitioning(_ A: [Int], _ K: Int) -> Int {
 3         guard A.count > 0 else { return 0 }
 4
 5         var dp = Array(repeating: 0, count: A.count)
 6         var m = 0
 7         for i in 1...A.count {
 8             let j = A.count - i
 9             m = max(m, A[j])
10             if i <= K {
11                 dp[j] = i * m
12             } else {
13                 var result = 0
14                 var localM = 0
15                 for this in j..<(j + K) {
16                     localM = max(localM, A[this])
17                     result = max(result, localM * (this - j + 1) + dp[this + 1])
18                 }
19                 dp[j] = result
20             }
21         }
22
23         return dp[0]
24     }
25 }


Runtime: 56 ms

Memory Usage: 20.9 MB
 1 class Solution {
 2     func maxSumAfterPartitioning(_ A: [Int], _ K: Int) -> Int {
 3         let n:Int = A.count
 4         var dp:[Int] = [Int](repeating:0,count:n + 1)
 5         for i in 1...n
 6         {
 7             var maxs:Int = 0
 8             var j:Int = 1
 9             while(j <= K && i - j >= 0)
10             {
11                 maxs = max(maxs, A[i - j])
12                 dp[i] = max(dp[i], dp[i - j] + maxs * j)
13                 j += 1
14             }
15         }
16         return dp[n]
17     }
18 }


64ms 
 1 class Solution {
 2     func maxSumAfterPartitioning(_ A: [Int], _ K: Int) -> Int {
 3         let N = A.count
 4         var dp = [Int](repeating: 0, count: N)
 5         for i in 0..<N {
 6             var curMax = 0
 7             for k in 1...K where i - k + 1 >= 0 {
 8                 curMax = max(curMax, A[i - k + 1])
 9                 dp[i] = max(dp[i], (i >= k ? dp[i-k] : 0) + curMax * k)
10             }
11         }
12         return dp[N-1]
13     }
14 }


144ms

 1 class Solution {
 2
 3     var memo = [Int: Int]()
 4     func maxSumAfterPartitioning(_ A: [Int], _ K: Int) -> Int {
 5         var sum_memo = [[Int]](repeating: [Int](repeating: 0, count: K), count: A.count)
 6         for i in A.indices {
 7             var curMax = A[i]
 8             for j in 0..<K {
 9                 if i+j >= A.count { continue }
10                 curMax = max(curMax, A[i+j])
11                 sum_memo[i][j] = curMax * (j+1)
12             }
13         }
14         return maxPartitioningSum(A, K, 0, A.count-1, sum_memo)
15     }
16
17     func maxPartitioningSum(_ A: [Int], _ K: Int, _ start: Int, _ end: Int, _ summemo: [[Int]]) -> Int {
18         if start > end {
19             return 0
20         }
21
22         if end - start < K {
23             return summemo[start][end-start]
24         }
25         var ans = 0
26         for i in start..<(start+K) {
27             let index = A.count*(i+1) + end
28             if memo[index] == nil {
29                 memo[index] = maxPartitioningSum(A, K, i+1, end, summemo)
30             }
31             ans = max(ans,summemo[start][i-start] + memo[index]!)
32         }
33         return ans
34     }
35 }


152ms

 1 class Solution {
 2     func maxSumAfterPartitioning(_ A: [Int], _ K: Int) -> Int {
 3         var dp = [Int](repeating: 0, count: A.count + 1)
 4         dp[0] = 0
 5
 6         for i in 1...A.endIndex {
 7             var m = A[i - 1]
 8             for j in stride(from: i, through: max(i - K + 1, 1), by: -1) {
 9             // for j in max(i-K+1, 1)...i {10                 m = max(m, A[j - 1])
11                 dp[i] = max(dp[i], (m * (i - j + 1)) + dp[j - 1])
12             }
13         }
14         return dp.last!
15     }
16 }

转载于:https://www.cnblogs.com/strengthen/p/10852127.html

[Swift]LeetCode1043. 分隔数组以得到最大和 | Partition Array for Maximum Sum相关推荐

  1. Leetcode 1043.分隔数组以达到最大和

    Time: 20190907 Type: Medium 题目描述 出整数数组 A,将该数组分隔为长度最多为 K 的几个(连续)子数组.分隔完成后,每个子数组的中的值都会变为该子数组中的最大值. 返回给 ...

  2. LeetCode 1043. 分隔数组以得到最大和(DP)

    1. 题目 给出整数数组 A,将该数组分隔为长度最多为 K 的几个(连续)子数组.分隔完成后,每个子数组的中的值都会变为该子数组中的最大值. 返回给定数组完成分隔后的最大和. 示例: 输入:A = [ ...

  3. Swift 字典转数组

    Swift 字典转数组 1.Swift 类型的字典(Dictionary) let dic = ["name":"Tom","gender" ...

  4. 在Swift中向数组添加元素

    本文翻译自:Add an element to an array in Swift Suppose I have an array, for example: 假设我有一个数组,例如: var myA ...

  5. [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. Swift语法学习--数组

    Swift语法学习--数组 一维数组 多维数组 数组遍历 数组与字符串转换 数组过滤 数组截取 多维数组转一维 一维数组 多维数组 数组遍历 数组与字符串转换 数组过滤 数组截取 多维数组转一维

  7. 找出数组中第k大和第m大的数字之和

    找出数组中第k大和第m大的数字之和 说明:定义一个函数,接受三个参数getMaxNumber(array,k,m){},找出第k大和第m大的数字之和.重复的数组也需要计算 比如:[1,3,4,5,4, ...

  8. Swift 5 判断数组中是否包含字符串,忽略大小写

    系统: Mac OS 10.15.2, XCode 11.3,swift 5.0 写作时间:2020-01-08 说明 Swift 5 判断数组中是否包含字符串,忽略大小写 let list = [& ...

  9. [Swift]LeetCode1146. 快照数组 | Snapshot Array

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. 怎样在nexus 中 搜索到远程maven仓库中的jar 文件
  2. 假3D场景逼真到火爆外网!超1亿像素无死角,被赞AI渲染新高度
  3. Linux CentOS 7【修改 屏幕(分辨率)大小】
  4. 小余学调度:调度指令票系列讲解1(持续更新中ing)
  5. STM32使用FatFs
  6. 这5个要点让你看清“Salesforce+AWS”
  7. hdu - 3415 Max Sum of Max-K-sub-sequence
  8. 结对项目——最大子数组
  9. NanoDet-Plus的学习笔记
  10. 苹果抛弃 OpenGL!
  11. 入职抖音之后,我变强了,但是营养也有点跟不上了...
  12. centos6 pip install python-ldap报错
  13. python全局变量被覆盖的问题
  14. 个股打板机会:国民技术(实战)
  15. python做后端速度慢吗_【后端开发】python如何提高运行速度
  16. 说说我对[lambda x: x*i for i in range(4)]的理解
  17. 测试独立显卡坏,可以将显示器接口插在集成显卡上试试
  18. 软件工程实践寒假作业
  19. 记录:Flink checkpoint 过期导致失败(线上问题)
  20. 1.机器学习的重要性

热门文章

  1. c语言,在主函数中输入一个整数,求该整数各位数字的乘积,[求助]求由键盘输入的任意两个整数的积...
  2. 使用php简单网页抓取和内容分析,使用PHP简单网页抓取和内容分析_php
  3. js 高级 prototype
  4. SQLAlchemy create table
  5. Python Types
  6. MongoDB Collections
  7. CentOS7没有ftp命令的解决方法
  8. 在 里面_适合县城里面加盟的鞋店推荐
  9. airtest运行脚本_airtest之脚本批量运行
  10. 单片机两个正玄波信号的相位差计算_51单片机的定时器/计数器的原理与使用