Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.length1 <= n <= 10001 <= piles[i][j] <= 1051 <= k <= sum(piles[i].length) <= 2000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[1,100,3],[7,8,9]] 2
[[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]] 7
coin-change)coin-change-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2218: Maximum Value of K Coins From Piles
class Solution {
public int maxValueOfCoins(List<List<Integer>> piles, int k) {
int n = piles.size();
int[][] f = new int[n + 1][k + 1];
for (int i = 1; i <= n; i++) {
List<Integer> nums = piles.get(i - 1);
int[] s = new int[nums.size() + 1];
s[0] = 0;
for (int j = 1; j <= nums.size(); j++) {
s[j] = s[j - 1] + nums.get(j - 1);
}
for (int j = 0; j <= k; j++) {
for (int h = 0; h < s.length && h <= j; h++) {
f[i][j] = Math.max(f[i][j], f[i - 1][j - h] + s[h]);
}
}
}
return f[n][k];
}
}
// Accepted solution for LeetCode #2218: Maximum Value of K Coins From Piles
func maxValueOfCoins(piles [][]int, k int) int {
n := len(piles)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, k+1)
}
for i := 1; i <= n; i++ {
nums := piles[i-1]
s := make([]int, len(nums)+1)
for j := 1; j <= len(nums); j++ {
s[j] = s[j-1] + nums[j-1]
}
for j := 0; j <= k; j++ {
for h, w := range s {
if j < h {
break
}
f[i][j] = max(f[i][j], f[i-1][j-h]+w)
}
}
}
return f[n][k]
}
# Accepted solution for LeetCode #2218: Maximum Value of K Coins From Piles
class Solution:
def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:
n = len(piles)
f = [[0] * (k + 1) for _ in range(n + 1)]
for i, nums in enumerate(piles, 1):
s = list(accumulate(nums, initial=0))
for j in range(k + 1):
for h, w in enumerate(s):
if j < h:
break
f[i][j] = max(f[i][j], f[i - 1][j - h] + w)
return f[n][k]
// Accepted solution for LeetCode #2218: Maximum Value of K Coins From Piles
/**
* [2218] Maximum Value of K Coins From Piles
*
* 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 i^th 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2019/11/09/e1.png" style="width: 600px; height: 243px;" />
* 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] <= 10^5
* 1 <= k <= sum(piles[i].length) <= 2000
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/
// discuss: https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn max_value_of_coins(piles: Vec<Vec<i32>>, k: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2218_example_1() {
let piles = vec![vec![1, 100, 3], vec![7, 8, 9]];
let k = 2;
let result = 101;
assert_eq!(Solution::max_value_of_coins(piles, k), result);
}
#[test]
#[ignore]
fn test_2218_example_2() {
let piles = vec![
vec![100],
vec![100],
vec![100],
vec![100],
vec![100],
vec![100],
vec![1, 1, 1, 1, 1, 1, 700],
];
let k = 7;
let result = 706;
assert_eq!(Solution::max_value_of_coins(piles, k), result);
}
}
// Accepted solution for LeetCode #2218: Maximum Value of K Coins From Piles
function maxValueOfCoins(piles: number[][], k: number): number {
const n = piles.length;
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
for (let i = 1; i <= n; i++) {
const nums = piles[i - 1];
const s = Array(nums.length + 1).fill(0);
for (let j = 1; j <= nums.length; j++) {
s[j] = s[j - 1] + nums[j - 1];
}
for (let j = 0; j <= k; j++) {
for (let h = 0; h < s.length && h <= j; h++) {
f[i][j] = Math.max(f[i][j], f[i - 1][j - h] + s[h]);
}
}
}
return f[n][k];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.