3116. Kth Smallest Amount With Single Denomination Combination
</> Solution
/**
* Problem: LeetCode 3116 - Kth Smallest Amount With Single Denomination Combination
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public long findKthSmallest(int[] coins, int k) {
int minCoin = coins[0];
for (int c : coins) {
minCoin = Math.min(minCoin, c);
}
long lo = 1, hi = (long) k * minCoin;
while (lo < hi) {
long mid = lo + (hi - lo) / 2;
if (countUpTo(mid, coins) >= k) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
private long countUpTo(long x, int[] coins) {
int n = coins.length;
long count = 0;
for (int mask = 1; mask < (1 << n); mask++) {
long lcmVal = 1;
boolean tooBig = false;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0) {
lcmVal = lcm(lcmVal, coins[i]);
if (lcmVal > x) {
tooBig = true;
break;
}
}
}
if (tooBig) continue;
long term = x / lcmVal;
if (Integer.bitCount(mask) % 2 == 1) {
count += term;
} else {
count -= term;
}
}
return count;
}
private long gcd(long a, long b) {
while (b != 0) {
long t = b;
b = a % b;
a = t;
}
return a;
}
private long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
}
"""
Problem: LeetCode 3116 - Kth Smallest Amount With Single Denomination Combination
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
import math
from typing import List
class Solution:
def findKthSmallest(self, coins: List[int], k: int) -> int:
min_coin = min(coins)
lo, hi = 1, k * min_coin
n = len(coins)
def count_up_to(x: int) -> int:
count = 0
for mask in range(1, 1 << n):
lcm_val = 1
too_big = False
for i in range(n):
if mask & (1 << i):
lcm_val = math.lcm(lcm_val, coins[i])
if lcm_val > x:
too_big = True
break
if too_big:
continue
term = x // lcm_val
if bin(mask).count("1") % 2 == 1:
count += term
else:
count -= term
return count
while lo < hi:
mid = lo + (hi - lo) // 2
if count_up_to(mid) >= k:
hi = mid
else:
lo = mid + 1
return lo
/**
* Problem: LeetCode 3116 - Kth Smallest Amount With Single Denomination Combination
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <numeric>
#include <algorithm>
/**
* Problem: LeetCode 3116 - Kth Smallest Amount With Single Denomination Combination
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
long long findKthSmallest(std::vector<int>& coins, int k) {
int minCoin = *std::min_element(coins.begin(), coins.end());
long long lo = 1, hi = 1LL * k * minCoin;
while (lo < hi) {
long long mid = lo + (hi - lo) / 2;
if (countUpTo(mid, coins) >= k) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
private:
long long countUpTo(long long x, const std::vector<int>& coins) {
int n = coins.size();
long long count = 0;
for (int mask = 1; mask < (1 << n); ++mask) {
long long lcmVal = 1;
bool tooBig = false;
for (int i = 0; i < n; ++i) {
if (mask & (1 << i)) {
lcmVal = std::lcm(lcmVal, static_cast<long long>(coins[i]));
if (lcmVal > x) {
tooBig = true;
break;
}
}
}
if (tooBig) continue;
long long term = x / lcmVal;
if (__builtin_popcount(mask) % 2 == 1) {
count += term;
} else {
count -= term;
}
}
return count;
}
};
/**
* Problem: LeetCode 3116 - Kth Smallest Amount With Single Denomination Combination
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var findKthSmallest = function(coins, k) {
let minCoin = coins[0];
for (const c of coins) {
if (c < minCoin) minCoin = c;
}
let lo = 1;
let hi = k * minCoin;
function gcd(a, b) {
while (b !== 0) {
const t = b;
b = a % b;
a = t;
}
return a;
}
function lcm(a, b) {
return Math.floor(a / gcd(a, b)) * b;
}
function bitCount(n) {
let count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
const n = coins.length;
function countUpTo(x) {
let count = 0;
for (let mask = 1; mask < (1 << n); mask++) {
let lcmVal = 1;
let tooBig = false;
for (let i = 0; i < n; i++) {
if ((mask & (1 << i)) !== 0) {
lcmVal = lcm(lcmVal, coins[i]);
if (lcmVal > x) {
tooBig = true;
break;
}
}
}
if (tooBig) continue;
const term = Math.floor(x / lcmVal);
if (bitCount(mask) % 2 === 1) {
count += term;
} else {
count -= term;
}
}
return count;
}
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (countUpTo(mid) >= k) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
};
TIME COMPLEXITY
O(2^N * N * log(k * min(coins)))
SPACE COMPLEXITY
O(1)
TOPICS
Array
Binary Search
Bit Manipulation
Combinatorics
Math
Number Theory