-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSongSelect.java
62 lines (59 loc) · 1.74 KB
/
SongSelect.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/*/
/*My Approach*/
class Solution {
public int numPairsDivisibleBy60(int[] time)
{
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<time.length;i++)
{
if(map.containsKey(time[i]%60))
{
map.put(time[i]%60,map.get(time[i]%60)+1);
}
else
{
map.put(time[i]%60,1);
}
}
int count = 0;
for(Map.Entry<Integer,Integer> entry:map.entrySet())
{
if(entry.getValue()==0)continue;
int i = entry.getKey();
int c = entry.getValue();
if(i==0||i==30)count+=(c*(c-1)/2);
else
{
if(map.containsKey(60-i))
{
int d = map.get(60-i);
//System.out.println(i+" "+c+" "+d);
count += (c*d);
map.put(60-i,0);
map.put(i,0);
}
}
}
return count;
}
}
/*Same logic but used array so efficient*/
class Solution {
public int numPairsDivisibleBy60(int[] time) {
int[] freq = new int[60];
for (int i = 0; i < time.length; i++) {
time[i] = time[i] % 60;
}
for (int i = 0; i < time.length; i++) {
freq[time[i]] = freq[time[i]] + 1;
}
int count = 0;
for (int i = 0; i <= 30; i++) {
if (i == 0 || i == 30) {
count += (freq[i] * (freq[i] - 1)) / 2;
} else
count = count + (freq[i] * freq[60-i]);
}
return count;
}
}