diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_135_coin_change.py b/src/my_project/interviews/top_150_questions_round_22/ex_135_coin_change.py new file mode 100644 index 00000000..e38a217e --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_135_coin_change.py @@ -0,0 +1,12 @@ +from typing import List + + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [amount + 1] * (amount + 1) + dp[0] = 0 + for i in range(1, amount + 1): + for coin in coins: + if coin <= i: + dp[i] = min(dp[i], dp[i - coin] + 1) + return dp[amount] if dp[amount] <= amount else -1 \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_135_coin_change.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_135_coin_change.ts new file mode 100644 index 00000000..0ee80f28 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_135_coin_change.ts @@ -0,0 +1,12 @@ +function coinChange(coins: number[], amount: number): number { + const dp = new Array(amount + 1).fill(amount + 1); + dp[0] = 0; + for (let i = 1; i <= amount; i++) { + for (const coin of coins) { + if (coin <= i) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + } + return dp[amount] <= amount ? dp[amount] : -1; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_135_coin_change_round_22.py b/tests/test_150_questions_round_22/test_135_coin_change_round_22.py new file mode 100644 index 00000000..21e5ccc4 --- /dev/null +++ b/tests/test_150_questions_round_22/test_135_coin_change_round_22.py @@ -0,0 +1,29 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_135_coin_change import Solution + + +class CoinChangeTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + self.assertEqual(self.solution.coinChange([1, 2, 5], 11), 3) + + def test_example_2(self): + self.assertEqual(self.solution.coinChange([2], 3), -1) + + def test_example_3(self): + self.assertEqual(self.solution.coinChange([1], 0), 0) + + def test_single_coin_exact(self): + self.assertEqual(self.solution.coinChange([5], 5), 1) + + def test_large_amount(self): + self.assertEqual(self.solution.coinChange([1, 5, 10, 25], 100), 4) + + def test_no_solution_zero_amount(self): + self.assertEqual(self.solution.coinChange([3, 7], 0), 0) + + def test_single_coin_multiple(self): + self.assertEqual(self.solution.coinChange([3], 9), 3)