返回介绍

solution / 2200-2299 / 2218.Maximum Value of K Coins From Piles / README_EN

发布于 2024-06-17 01:03:08 字数 5068 浏览 0 评论 0 收藏 0

2218. Maximum Value of K Coins From Piles

中文文档

Description

There are n piles of coins on a table. Each pile consists of a positive number of coins of assorted denominations.

In one move, you can choose any coin on top of any pile, remove it, and add it to your wallet.

Given a list piles, where piles[i] is a list of integers denoting the composition of the ith pile from top to bottom, and a positive integer k, return _the maximum total value of coins you can have in your wallet if you choose exactly_ k _coins optimally_.

 

Example 1:

Input: piles = [[1,100,3],[7,8,9]], k = 2
Output: 101
Explanation:
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.

Example 2:

Input: piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
Output: 706
Explanation:
The maximum total can be obtained if we choose all coins from the last pile.

 

Constraints:

  • n == piles.length
  • 1 <= n <= 1000
  • 1 <= piles[i][j] <= 105
  • 1 <= k <= sum(piles[i].length) <= 2000

Solutions

Solution 1

class Solution:
  def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:
    presum = [list(accumulate(p, initial=0)) for p in piles]
    n = len(piles)
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i, s in enumerate(presum, 1):
      for j in range(k + 1):
        for idx, v in enumerate(s):
          if j >= idx:
            dp[i][j] = max(dp[i][j], dp[i - 1][j - idx] + v)
    return dp[-1][-1]
class Solution {
  public int maxValueOfCoins(List<List<Integer>> piles, int k) {
    int n = piles.size();
    List<int[]> presum = new ArrayList<>();
    for (List<Integer> p : piles) {
      int m = p.size();
      int[] s = new int[m + 1];
      for (int i = 0; i < m; ++i) {
        s[i + 1] = s[i] + p.get(i);
      }
      presum.add(s);
    }
    int[] dp = new int[k + 1];
    for (int[] s : presum) {
      for (int j = k; j >= 0; --j) {
        for (int idx = 0; idx < s.length; ++idx) {
          if (j >= idx) {
            dp[j] = Math.max(dp[j], dp[j - idx] + s[idx]);
          }
        }
      }
    }
    return dp[k];
  }
}
class Solution {
public:
  int maxValueOfCoins(vector<vector<int>>& piles, int k) {
    vector<vector<int>> presum;
    for (auto& p : piles) {
      int m = p.size();
      vector<int> s(m + 1);
      for (int i = 0; i < m; ++i) s[i + 1] = s[i] + p[i];
      presum.push_back(s);
    }
    vector<int> dp(k + 1);
    for (auto& s : presum) {
      for (int j = k; ~j; --j) {
        for (int idx = 0; idx < s.size(); ++idx) {
          if (j >= idx) dp[j] = max(dp[j], dp[j - idx] + s[idx]);
        }
      }
    }
    return dp[k];
  }
};
func maxValueOfCoins(piles [][]int, k int) int {
  var presum [][]int
  for _, p := range piles {
    m := len(p)
    s := make([]int, m+1)
    for i, v := range p {
      s[i+1] = s[i] + v
    }
    presum = append(presum, s)
  }
  dp := make([]int, k+1)
  for _, s := range presum {
    for j := k; j >= 0; j-- {
      for idx, v := range s {
        if j >= idx {
          dp[j] = max(dp[j], dp[j-idx]+v)
        }
      }
    }
  }
  return dp[k]
}

Solution 2

class Solution:
  def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:
    presum = [list(accumulate(p, initial=0)) for p in piles]
    dp = [0] * (k + 1)
    for s in presum:
      for j in range(k, -1, -1):
        for idx, v in enumerate(s):
          if j >= idx:
            dp[j] = max(dp[j], dp[j - idx] + v)
    return dp[-1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文