Tensorum
YouTubeশেখা শুরু করুন
LeetCode 322 · Unbounded Knapsack

Coin Change

LeetCode 322 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Unbounded knapsack: include ⇒ 1 + dp[same coin][amount − coin].

মাঝারিArrayDynamic ProgrammingBreadth-First SearchKnapsack ProblemComplete Knapsack
LeetCode-এ খুলুন ↗

আমি Coin Change (LeetCode 322) এভাবে সমাধান করেছি — Unbounded Knapsack প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Each coin can be used any number of times. For each coin you either skip it for good or use one more of it and stay on the same coin — that 'stay on the same index' is what makes it an unbounded knapsack. Minimise the number of coins used.

Approach

  1. 1

    dp[i][a] = fewest coins from coins[0..i] that make amount a.

  2. 2

    Row 0: amounts divisible by coins[0] need a / coins[0] coins; others are ∞.

  3. 3

    For each later coin: exclude = row above; include = 1 + same row at a − coin.

  4. 4

    Take the min; ∞ at the end means the amount is impossible, so return -1.

আমি কীভাবে সমাধান করেছি · প্রথম ভাবনা থেকে চূড়ান্ত সমাধান

1
Recursion + memoO(n·amount) time

lru_cache dfs(index, target) with include/exclude.

2
TabulationAcceptedO(n·amount) time · O(n·amount) space

2-D unbounded knapsack table.

Recursion + memoPythonO(n·amount) time
from functools import lru_cache
class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        #unbounded knapsack
        @lru_cache(maxsize=None)
        def dfs(index,cur_target):
            if cur_target==0:
                return 0
            if index<0:
                return float("inf")
            exclude = dfs(index-1,cur_target)
            include = float("inf")
            if coins[index]<=cur_target:
                include=1+dfs(index,cur_target-coins[index])
            return min(include,exclude)
        res= dfs(len(coins)-1,amount)
        return res if res!=float("inf") else -1
TabulationPythonO(n·amount) time · O(n·amount) space
class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        #unbounded knapsack
        n = len(coins)
        dp = [[float("inf")]*(amount+1) for _ in range(n)]
        #how many conins we need if we are taking the first coin
        for a in range(amount+1):
            if a%coins[0]==0:
                dp[0][a]= a//coins[0]
        for index in range(1,n):
            for cur_target in range(amount+1):
                    exclude = dp[index-1][cur_target]
                    include = float("inf")
                    if coins[index]<=cur_target:
                        include=1+dp[index][cur_target-coins[index]]
                    dp[index][cur_target]=min(include,exclude)
        res= dp[n-1][amount]
        return res if res != float("inf") else -1
স্টেপ-থ্রু লোড হচ্ছে…

Coin Change — সাধারণ প্রশ্ন

Coin Change-এর পেছনে intuition কী?

Each coin can be used any number of times. For each coin you either skip it for good or use one more of it and stay on the same coin — that 'stay on the same index' is what makes it an unbounded knapsack. Minimise the number of coins used.

Coin Change (LeetCode 322) কীভাবে সমাধান করবেন?

1. dp[i][a] = fewest coins from coins[0..i] that make amount a. 2. Row 0: amounts divisible by coins[0] need a / coins[0] coins; others are ∞. 3. For each later coin: exclude = row above; include = 1 + same row at a − coin. 4. Take the min; ∞ at the end means the amount is impossible, so return -1.

Coin Change-এর time আর space complexity কত?

Time: O(n · amount)। Space: O(n · amount)। (Accepted approach: Tabulation।)

Coin Change-এ সাধারণ ভুলগুলো কী?

Greedy 'largest coin first' fails (e.g. coins [1,3,4], amount 6). Amount 0 needs 0 coins, not -1.

Coin Change সমাধানের কি একাধিক উপায় আছে?

Recursion + memo (O(n·amount) time): lru_cache dfs(index, target) with include/exclude. Tabulation (O(n·amount) time · O(n·amount) space): 2-D unbounded knapsack table.