<commit_before>"""Leetcode 322. Coin Change
Medium

URL: https://leetcode.com/problems/coin-change/

You are given coins of different denominations and a total amount of 
money amount. Write a function to compute the fewest number of coins 
that you need to make up that amount. 
If that amount of money cannot be made up by any combination of the 
coins, return -1.

Example 1:
Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:
Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.
"""

class SolutionDp(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int

        Time complexity: O(c * a), where c is number of coins, and a is amount.
        Space complexity: O(c * a).
        """
        # Why sorted coin list? Since we want to start from smaller coins.
        coins = sorted(coins)

        n_coins = len(coins)
        T = [[float('inf')]*(amount + 1) for _ in range(n_coins)]

        # Base case for amount 0.
        for c in range(n_coins):
            T[c][0] = 0

        # Start from smallest coin to change from amount 0 to total amount.
        for c in range(n_coins):
            for a in range(1, amount + 1):
                if a == coins[c]:
                    # Directly use coin c to change total amount.
                    T[c][a] = 1
                elif a >= coins[c]:
                    # If coin c can be included, decide which uses less coins:
                    # 1. previous coins without coin c to make a.
                    # 2. previous coins without coin c to make a - coins[c]
                    #    plus this 1 extra coin c.
                    T[c][a] = min(T[c - 1][a], 1 + T[c][a - coins[c]])
                else:
                    # If coin c cannot be included, use previous coins.
                    T[c][a] = T[c - 1][a]

        if T[-1][-1] != float('inf'):
            return T[-1][-1]
        else:
            return -1


def main():
    coins = [1, 2, 5]
    amount = 11
    print SolutionDp().coinChange(coins, amount)

    coins = [2]
    amount = 3
    print SolutionDp().coinChange(coins, amount)


if __name__ == '__main__':
    main()<commit_msg>Complete coin change by memo<commit_after>"""Leetcode 322. Coin Change
Medium

URL: https://leetcode.com/problems/coin-change/

You are given coins of different denominations and a total amount of 
money amount. Write a function to compute the fewest number of coins 
that you need to make up that amount. 
If that amount of money cannot be made up by any combination of the 
coins, return -1.

Example 1:
Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:
Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.
"""

class SolutionMemo(object):
    def _coin_change_memo(self, coins, amount, T):
        """Helper function for coin_change_memo()."""
        if amount < 0:
            return -1
        if amount == 0:
            return 0

        if T[amount] > 0:
            return T[amount]

        min_coins = float('inf')

        for c in coins:
            extra_coins = self._coin_change_memo(coins, amount - c, T)
            if extra_coins >= 0 and extra_coins < min_coins:
                min_coins = 1 + extra_coins

        if min_coins != float('inf'):
            T[amount] = min_coins
        else:
            T[amount] = -1

        return T[amount]


    def coinChange(self, coins, amount):
        """Change minimum coins by top-down dynamic programming: 
        recursion + memoization.

        Time complexity: O(c * a), where c is number of coins, and a is amount.
        Space complexity: O(a).
        """
        T = [0] * (amount + 1)
        return self._coin_change_memo(coins, amount, T)


class SolutionDp(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int

        Time complexity: O(c * a), where c is number of coins, and a is amount.
        Space complexity: O(c * a).
        """
        # Why sorted coin list? Since we want to start from smaller coins.
        coins = sorted(coins)

        n_coins = len(coins)
        T = [[float('inf')]*(amount + 1) for _ in range(n_coins)]

        # Base case for amount 0.
        for c in range(n_coins):
            T[c][0] = 0

        # Start from smallest coin to change from amount 0 to total amount.
        for c in range(n_coins):
            for a in range(1, amount + 1):
                if a == coins[c]:
                    # Directly use coin c to change total amount.
                    T[c][a] = 1
                elif a >= coins[c]:
                    # If coin c can be included, decide which uses less coins:
                    # 1. previous coins without coin c to make a.
                    # 2. previous coins without coin c to make a - coins[c]
                    #    plus this 1 extra coin c.
                    T[c][a] = min(T[c - 1][a], 1 + T[c][a - coins[c]])
                else:
                    # If coin c cannot be included, use previous coins.
                    T[c][a] = T[c - 1][a]

        if T[-1][-1] != float('inf'):
            return T[-1][-1]
        else:
            return -1


def main():
    coins = [1, 2, 5]
    amount = 11
    print SolutionMemo().coinChange(coins, amount)
    print SolutionDp().coinChange(coins, amount)

    coins = [2]
    amount = 3
    print SolutionMemo().coinChange(coins, amount)
    print SolutionDp().coinChange(coins, amount)


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.10555652325378855, 'fraction_numerical': 0.01776693955756837, 'mean_word_length': 1.7968826108134437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2930527', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1670, 'n_words': 675}