-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_52.java
More file actions
55 lines (49 loc) · 1.33 KB
/
Copy pathDay_52.java
File metadata and controls
55 lines (49 loc) · 1.33 KB
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
48
49
50
51
52
53
54
55
// Relative Ranks
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
String[] str = new String[n];
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> Integer.compare(b[0],a[0]));
for(int i=0;i<n;i++){
pq.offer(new int[]{score[i],i});
}
int k = 0;
while(!pq.isEmpty()){
int[] arr = pq.poll();
int val = arr[1];
k++;
if(k <= 3){
if(k == 1) str[val] = "Gold Medal";
else if(k == 2) str[val] = "Silver Medal";
else str[val] = "Bronze Medal";
}
else{
str[val] = k + "";
}
}
return str;
}
}
// Longest Palindrome
class Solution {
public int longestPalindrome(String s) {
int n = s.length();
int[] hash = new int[125];
for(int i=0;i<n;i++){
hash[s.charAt(i)]++;
}
Arrays.sort(hash);
boolean flag = false;
int cnt = 0;
for(int i=124;i>=0;i--){
int freq = hash[i];
if(freq % 2 == 0) cnt+=freq;
else{
int val = freq - 1;
cnt+=val;
flag = true;
}
}
return flag ? cnt+1: cnt;
}
}