SHOW:
|
|
- or go back to the newest paste.
1 | // Author : Saurav Kalsoor | |
2 | - | // Minimize Unique Numbers - JAVA |
2 | + | // Minimize Unique Numbers - KOTLIN |
3 | ||
4 | - | import java.util.*; |
4 | + | import java.util.* |
5 | ||
6 | - | public class Test { |
6 | + | var sc = Scanner(System.`in`) |
7 | - | static Scanner sc = new Scanner(System.in); |
7 | + | fun main() { |
8 | - | public static void main(String[] args) { |
8 | + | val n: Int = sc.nextInt() |
9 | - | int n = sc.nextInt(); |
9 | + | val k: Int = sc.nextInt() |
10 | - | int k = sc.nextInt(); |
10 | + | val arr = ArrayList<Int>() |
11 | for (i in 0 until n) { | |
12 | - | ArrayList<Integer> arr = new ArrayList<>(); |
12 | + | arr.add(sc.nextInt()) |
13 | - | for(int i = 0;i < n; i++){ |
13 | + | |
14 | - | arr.add(sc.nextInt()); |
14 | + | println(minimizeUnique(arr, k)) |
15 | - | } |
15 | + | |
16 | - | |
16 | + | |
17 | - | System.out.println(minimizeUnique(arr, k)); |
17 | + | fun minimizeUnique(arr: ArrayList<Int>, k: Int): Int { |
18 | var k = k | |
19 | val count = HashMap<Int, Int>() | |
20 | - | public static int minimizeUnique(ArrayList<Integer> arr, int k){ |
20 | + | for (num in arr) { |
21 | - | HashMap<Integer, Integer> count = new HashMap<>(); |
21 | + | count[num] = count.getOrDefault(num, 0) + 1 |
22 | } | |
23 | - | for(int num : arr){ |
23 | + | val st: MutableSet<MyPair> = |
24 | - | count.put(num, count.getOrDefault(num, 0) + 1); |
24 | + | TreeSet { a, b -> |
25 | - | } |
25 | + | if (a.second == b.second) |
26 | a.first - b.first | |
27 | - | Set<MyPair> st = new TreeSet<>(new Comparator<MyPair>() { |
27 | + | else a.second - b.second |
28 | - | @Override |
28 | + | } |
29 | - | public int compare(MyPair a, MyPair b) { |
29 | + | |
30 | - | if(a.second == b.second) |
30 | + | for ((key, value) in count) { |
31 | - | return a.first - b.first; |
31 | + | st.add(MyPair(key, value)) |
32 | - | return a.second - b.second; |
32 | + | |
33 | - | } |
33 | + | |
34 | - | }); |
34 | + | var res = st.size |
35 | for (x in st) { | |
36 | - | for(Map.Entry<Integer, Integer> x : count.entrySet()){ |
36 | + | k -= if (k >= x.second) { |
37 | - | st.add(new MyPair(x.getKey(), x.getValue()) ); |
37 | + | res-- |
38 | - | } |
38 | + | x.second |
39 | } else { | |
40 | - | int res = st.size(); |
40 | + | break |
41 | } | |
42 | - | for(MyPair x : st){ |
42 | + | |
43 | - | if(k >= x.second){ |
43 | + | return res |
44 | - | res--; |
44 | + | |
45 | - | k -= x.second; |
45 | + | |
46 | - | }else{ |
46 | + | internal class MyPair(var first: Int, var second: Int) |
47 | - | break; |
47 | + | |
48 | - | } |
48 | + |