-
Notifications
You must be signed in to change notification settings - Fork 29
/
name.java
47 lines (35 loc) · 1.26 KB
/
name.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int result = countUniqueMaxNumbers(arr, n);
System.out.println(result);
}
public static int countUniqueMaxNumbers(int[] arr, int n) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
maxHeap.offer(arr[i]);
frequencyMap.put(arr[i], frequencyMap.getOrDefault(arr[i], 0) + 1);
}
int uniqueCount = 0;
while (!maxHeap.isEmpty()) {
int maxNum = maxHeap.poll();
if (frequencyMap.containsKey(maxNum) && frequencyMap.get(maxNum) == 1) {
uniqueCount++;
}
frequencyMap.remove(maxNum);
int halfNum = maxNum / 2;
if (halfNum > 0) {
maxHeap.offer(halfNum);
frequencyMap.put(halfNum, frequencyMap.getOrDefault(halfNum, 0) + 1);
}
}
return uniqueCount;
}
}