-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequencyCount.java
88 lines (73 loc) · 2.69 KB
/
FrequencyCount.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
/*
Name: Frequency Queries
Source: HackerRank
Link: https://www.hackerrank.com/challenges/frequency-queries/problem
*/
public class FrequencyCount {
// Complete the freqQuery function below.
static void freqQuery(List<List<Integer>> queries) {
int l1 = queries.size();
List<Integer> ans = new ArrayList<Integer>();
System.out.println("Size: "+l1);
Hashtable <Integer, Integer> ht1 = new Hashtable<Integer, Integer>();
Hashtable <Integer, Integer> ht2 = new Hashtable<Integer, Integer>();
for(int i =0; i<l1; i++)
{
int type = queries.get(i).get(0);
int value = queries.get(i).get(1);
System.out.println("Type: "+type+" Value: "+value);
if(type==1)
{
int a = ht1.getOrDefault(value, 0)+1;
ht1.put(value, a);
ht2.put(a, ht2.getOrDefault(a,0)+1);
if(a-1>0){ ht2.put(a-1, ht2.get(a-1)-1); }
}
else if(type==2)
{
if(ht1.containsKey(value) && ht1.get(value)>0 )
{
int b = ht1.get(value);
ht2.put(b, ht2.get(b)-1);
if(b-1>0)
{
ht2.put(b-1, ht2.getOrDefault(b-1, 0)+1);
}
ht1.put(value, b-1);
}
}
else
{
int temp = ht2.getOrDefault(value, 0);
if(temp>0){ ans.add(1); }
else{ ans.add(0); }
}
System.out.println("\n HashTable one");
ht1.forEach((k,v)->System.out.println("Key: "+k+" value: "+v));
System.out.println("\n HashTable two");
ht2.forEach((k,v)->System.out.println("Key: "+k+" value: "+v));
}
System.out.println("-----------Answer------------");
for (int i: ans ) {
System.out.println(i);
}
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int q = scanner.nextInt();
List<List<Integer>> queries = new ArrayList<>();
for(int i=0; i<q ; i++){
queries.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
}
freqQuery(queries);
}
}