{"id":"6b093dddbd4755deb67ee05b","slug":"coin-change","title":"Change with the fewest coins","summary":"A vending machine gives change in unusual denominations and wants to use as few coins as possible. Greedy fails here – dynamic programming is needed.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"medium","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["dynamic-programming","greedy","unbounded-knapsack"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Minimum number of coins (unlimited supply of each denomination) summing to amount, or -1. */\nfunction minCoins(coins: number[], amount: number): number {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('basic example', () => {\n  expect(minCoins([1, 2, 5], 11)).toBe(3);\n});\n\ntest('greedy is not enough', () => {\n  expect(minCoins([1, 3, 4], 6)).toBe(2);\n  expect(minCoins([9, 6, 5, 1], 11)).toBe(2);\n});\n\ntest('impossible amounts', () => {\n  expect(minCoins([2], 3)).toBe(-1);\n  expect(minCoins([2, 5], 1)).toBe(-1);\n  expect(minCoins([5, 10, 25], 9999)).toBe(-1);\n});\n\ntest('zero amount', () => {\n  expect(minCoins([5, 10, 25], 0)).toBe(0);\n});\n\ntest('a single coin and coins larger than the amount', () => {\n  expect(minCoins([7], 7)).toBe(1);\n  expect(minCoins([100, 3], 9)).toBe(3);\n});\n\ntest('larger amounts', () => {\n  expect(minCoins([7, 13, 29, 41], 9973)).toBe(245);\n  expect(minCoins([4, 6], 9998)).toBe(1667);\n});\n\ntest('performance: 12 denominations, amount 10 000', () => {\n  expect(minCoins([3, 7, 11, 17, 23, 31, 41, 53, 67, 83, 97, 101], 10000)).toBe(100);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Minimum number of coins (unlimited supply of each denomination) summing to amount, or -1.\n * @param {number[]} coins\n * @param {number} amount\n * @returns {number}\n */\nfunction minCoins(coins, amount) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('basic example', () => {\n  expect(minCoins([1, 2, 5], 11)).toBe(3);\n});\n\ntest('greedy is not enough', () => {\n  expect(minCoins([1, 3, 4], 6)).toBe(2);\n  expect(minCoins([9, 6, 5, 1], 11)).toBe(2);\n});\n\ntest('impossible amounts', () => {\n  expect(minCoins([2], 3)).toBe(-1);\n  expect(minCoins([2, 5], 1)).toBe(-1);\n  expect(minCoins([5, 10, 25], 9999)).toBe(-1);\n});\n\ntest('zero amount', () => {\n  expect(minCoins([5, 10, 25], 0)).toBe(0);\n});\n\ntest('a single coin and coins larger than the amount', () => {\n  expect(minCoins([7], 7)).toBe(1);\n  expect(minCoins([100, 3], 9)).toBe(3);\n});\n\ntest('larger amounts', () => {\n  expect(minCoins([7, 13, 29, 41], 9973)).toBe(245);\n  expect(minCoins([4, 6], 9998)).toBe(1667);\n});\n\ntest('performance: 12 denominations, amount 10 000', () => {\n  expect(minCoins([3, 7, 11, 17, 23, 31, 41, 53, 67, 83, 97, 101], 10000)).toBe(100);\n});\n"},{"lang":"python","runner":"browser","starter":"def min_coins(coins: list[int], amount: int) -> int:\n    \"\"\"Minimum number of coins (unlimited supply of each denomination) summing to amount, or -1.\"\"\"\n    raise NotImplementedError\n","testCount":7,"tests":"def check(coins, amount, expected):\n    got = min_coins(coins, amount)\n    assert got == expected, f\"min_coins({coins}, {amount}): expected {expected}, got {got}\"\n\n\ndef test_basic_example():\n    check([1, 2, 5], 11, 3)\n\n\ndef test_greedy_is_not_enough():\n    check([1, 3, 4], 6, 2)\n    check([9, 6, 5, 1], 11, 2)\n\n\ndef test_impossible_amounts():\n    check([2], 3, -1)\n    check([2, 5], 1, -1)\n    check([5, 10, 25], 9999, -1)\n\n\ndef test_zero_amount():\n    check([5, 10, 25], 0, 0)\n\n\ndef test_single_coin_and_coins_larger_than_amount():\n    check([7], 7, 1)\n    check([100, 3], 9, 3)\n\n\ndef test_larger_amounts():\n    check([7, 13, 29, 41], 9973, 245)\n    check([4, 6], 9998, 1667)\n\n\ndef test_performance_12_denominations():\n    check([3, 7, 11, 17, 23, 31, 41, 53, 67, 83, 97, 101], 10000, 100)\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Minimum number of coins (unlimited supply of each denomination) summing to amount, or -1.</summary>\n    public static int MinCoins(int[] coins, int amount)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":7},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Minimum number of coins (unlimited supply of each denomination) summing to amount, or -1. */\n    static int minCoins(int[] coins, int amount) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":7},{"lang":"go","runner":"judge0","starter":"package main\n\n// MinCoins returns the minimum number of coins (unlimited supply of each denomination) summing to amount, or -1.\nfunc MinCoins(coins []int, amount int) int {\n\tpanic(\"not implemented\")\n}\n","testCount":7}],"questionCount":null,"xp":20,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:53.485Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"The office coffee machine has coins of a few denominations (as many of each as needed) and has to give change "},{"type":"text","text":"amount","marks":[{"type":"code"}]},{"type":"text","text":" using "},{"type":"text","text":"the fewest coins","marks":[{"type":"bold"}]},{"type":"text","text":". The denominations can be unusual (tokens, local currencies), so we cannot assume that \"take the largest coin that fits\" always works."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"The function gets a list of distinct positive denominations "},{"type":"text","text":"coins","marks":[{"type":"code"}]},{"type":"text","text":" and an amount "},{"type":"text","text":"amount ≥ 0","marks":[{"type":"code"}]},{"type":"text","text":". Return the minimum number of coins that add up exactly to "},{"type":"text","text":"amount","marks":[{"type":"code"}]},{"type":"text","text":", or "},{"type":"text","text":"-1","marks":[{"type":"code"}]},{"type":"text","text":" when it cannot be done. For "},{"type":"text","text":"amount = 0","marks":[{"type":"code"}]},{"type":"text","text":" the result is "},{"type":"text","text":"0","marks":[{"type":"code"}]},{"type":"text","text":"."}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"minCoins(coins, amount)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"min_coins(coins, amount)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.MinCoins","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.minCoins","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"MinCoins","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"coins","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"amount","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Result"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Notes"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[1, 2, 5]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"11","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"5 + 5 + 1"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[1, 3, 4]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"6","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3 + 3 – greedy would give 4 + 1 + 1"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[2]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"-1","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph"}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[5, 10, 25]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph"}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"1 ≤ coins.length ≤ 12","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"1 ≤ coins[i] ≤ 10,000","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"0 ≤ amount ≤ 10,000","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O(amount · k)","marks":[{"type":"bold"}]},{"type":"text","text":" time for "},{"type":"text","text":"k","marks":[{"type":"code"}]},{"type":"text","text":" denominations, "},{"type":"text","text":"O(amount)","marks":[{"type":"bold"}]},{"type":"text","text":" memory."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/coin-change/","title":"LeetCode 322 – Coin Change","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:53.487Z","updatedAt":"2026-11-27T08:22:53.487Z"}