Calculate Cost
You are given the prices of N items. You need to remember all the distinct prices. The problem is you can remember at most k distinct prices.unknown
java
a year ago
424 B
0
Indexable
Never
static long getCost(int X, int k, int[] price, int N) { Arrays.sort(price); int distinctPrices = 1; long cost = 0; for (int i = 1; i < N; i++) { if (price[i] != price[i - 1]) { distinctPrices++; } } if (distinctPrices <= k) { return 0; } int extraDistinct = distinctPrices - k; cost = (long) extraDistinct * X; return cost; }