Knowee
Questions
Features
Study Tools

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 = 3Output: 9Explanation: 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 = 7Output: 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.Java 1class Solution {2    public long findKthSmallest(int[] coins, int k) {3        4   }5}  Custom TestcaseUse Example Testcases Run Code SubmitCopyright © 2024 LeetCodeHelp CenterJobsBug BountyOnline InterviewStudentsTermsPrivacy PolicyUnited States

Question

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 = 3Output: 9Explanation: 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 = 7Output: 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.Java 1class Solution {2    public long findKthSmallest(int[] coins, int k) {3        4   }5}  Custom TestcaseUse Example Testcases Run Code SubmitCopyright © 2024 LeetCodeHelp CenterJobsBug BountyOnline InterviewStudentsTermsPrivacy PolicyUnited States

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using a priority queue in Java. The priority queue will be used to store the multiples of the coin denominations. We will start by adding all the coin denominations to the queue. Then, we will remove the smallest element from the queue and add the next multiple of that coin to the queue. We will repeat this process until we have removed k elements from the queue. The kth element removed from the queue will be the kth smallest amount that can be made using the coins.

Here is the Java code that implements this approach:

import java.util.*;

class Solution {
    public long findKthSmallest(int[] coins, int k) {
        PriorityQueue<Long> queue = new PriorityQueue<>();
        for (int coin : coins) {
            queue.add((long) coin);
        }
        long kthSmallest = 0;
        for (int i = 0; i < k; i++) {
            kthSmallest = queue.poll();
            for (int coin : coins) {
                long nextMultiple = kthSmallest + coin;
                if (!queue.contains(nextMultiple)) {
                    queue.add(nextMultiple);
                }
            }
        }
        return kthSmallest;
    }
}

This code works by first adding all the coin denominations to the priority queue. Then, it removes the smallest element from the queue and adds the next multiple of that coin to the queue. This process is repeated until the kth smallest element has been removed from the queue. The kth smallest element is then returned as the result.

This problem has been solved

Similar Questions

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.Return 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.You may assume that you have an infinite number of each kind of coin. Example 1:Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1Example 2:Input: coins = [2], amount = 3Output: -1Example 3:Input: coins = [1], amount = 0Output: 0 Constraints:1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104

Pranav is playing a coin game with his friends. Create a simple program to assist Pranav in converting a given amount of cash into the minimum number of coins. Prompt Pranav to input the cash amount and utilize assignment operators to calculate and display the minimum number of coins needed for denominations of 100, 50, 10, 5, 2, and 1. Assume these denominations as coins with values of 100, 50, 10, 5, 2, and 1, respectively

ou are given an array of integers ‘coins’ denoting the denomination of coins and another array of integers ‘freq’ denoting the number of coins of each denomination.You have to find the number of ways to make the sum ‘V’ by selecting some(or all) coins from the array.The answer can be very large. So, return the answer modulo 1000000007.For Example :‘N’ = 3, ‘coins’ = {1, 2, 3}, ‘freq’ = {1, 1, 3}, ‘V’ = 6For the given example, we can make six by using the following coins:{1, 2, 3}{3. 3}Hence, the answer is 2.

Write a program that prints the minimum number of coins to make change for an amount of money.Usage: ./change centswhere cents is the amount of cents you need to give backif the number of arguments passed to your program is not exactly 1, print Error, followed by a new line, and return 1you should use atoi to parse the parameter passed to your programIf the number passed as the argument is negative, print 0, followed by a new lineYou can use an unlimited number of coins of values 25, 10, 5, 2, and 1 cent

Use Dynamic Programming to find the minimum number coins you can change for the given amount of money (W=7) using given coins(1,3,4,5).

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.