LeetCode 3116: Kth Smallest Amount With Single Denomination Combination Solution
Master LeetCode problem 3116 (Kth Smallest Amount With Single Denomination Combination), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3116. Kth Smallest Amount With Single Denomination Combination
Problem Explanation
Explanation:
To find the kth smallest amount that can be made using the given coins with single denomination combinations, we can use a priority queue (min heap) to store the amounts in ascending order. We start by adding the smallest amount possible using the first coin, then we iteratively generate the next smallest amounts by adding multiples of each coin to the amounts already present in the priority queue. We keep track of the visited amounts to avoid duplicates. We repeat this process until we reach the kth smallest amount. :
Solution Code
import java.util.*;
class Solution {
public int kthSmallestAmount(int[] coins, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
Set<Integer> visited = new HashSet<>();
pq.offer(coins[0]);
visited.add(coins[0]);
while (k > 1) {
int curr = pq.poll();
for (int coin : coins) {
int next = curr + coin;
if (!visited.contains(next)) {
pq.offer(next);
visited.add(next);
}
}
k--;
}
return pq.poll();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3116 (Kth Smallest Amount With Single Denomination Combination)?
This page provides optimized solutions for LeetCode problem 3116 (Kth Smallest Amount With Single Denomination Combination) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3116 (Kth Smallest Amount With Single Denomination Combination)?
The time complexity for LeetCode 3116 (Kth Smallest Amount With Single Denomination Combination) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3116 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3116 in Java, C++, or Python.