You are given an integer array coins representing coins of different denominations and an integer k.
You have an infinite number of coins of each denomination. However, you are not allowed to combine coins of different denominations.
Return the kth smallest amount that can be made using these coins.
Example 1:
Input: coins = [3,6,9], k = 3
Output: 9
Explanation: The given coins can make the following amounts:
Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.
Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.
Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.
All of the coins combined produce: 3, 6, 9, 12, 15, etc.
Example 2:
Input: coins = [5,2], k = 7
Output: 12
Explanation: The given coins can make the following amounts:
Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.
Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.
All of the coins combined produce: 2, 4, 5, 6, 8, 10, 12, 14, 15, etc.
Constraints:
1 <= coins.length <= 151 <= coins[i] <= 251 <= k <= 2 * 109coins contains pairwise distinct integers.This approach uses a min-heap to efficiently find the k-th smallest amount. We can generate multiples of each coin denomination and insert them into a min-heap, keeping track of the smallest values across all sequences.
The solution initializes a min-heap with each coin denomination. The tuple (coin, coin) is inserted into the heap, where every element is a pair of current_value and the base coin denomination. We repeatedly pop the smallest value from the heap, and after k iterations, the k-th smallest element is returned. After popping, we push the next multiple of that coin into the heap.
Java
Time Complexity: O(k log n), where n is the number of coin denominations.
Space Complexity: O(n), where n is the number of coin denominations.
Another approach is to count numbers that are multiples of any given denomination using mathematics and number theory concepts to find the k-th smallest efficiently. This avoids insertion into a heap by logically deducing the k-th smallest multiple.
This approach uses binary search to find the k-th smallest value by searching over a range of potential answers. For each midpoint in the range, we calculate how many numbers are divisible by each coin denomination and adjust the search region based on whether the count is less than or greater than k.
Java
Time Complexity: O(n log(max * k)), where n is the number of coin denominations and max is the largest denomination.
Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Min-Heap Approach for Multiples | Time Complexity: O(k log n), where n is the number of coin denominations. |
| Mathematical Counting | Time Complexity: O(n log(max * k)), where n is the number of coin denominations and max is the largest denomination. |
How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer • CrioDo • 304,695 views views
Watch 9 more video solutions →Practice Kth Smallest Amount With Single Denomination Combination with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor