forked from hamidmayeli/HackerRankSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequencyQueries.cs
92 lines (77 loc) · 2.78 KB
/
FrequencyQueries.cs
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
89
90
91
92
// Frequency Queries
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the freqQuery function below.
static List<int> freqQuery(List<List<int>> queries)
{
var data = new Dictionary<int, int>();
var frequency = new Dictionary<int, int>();
var result = new List<int>();
foreach (var command in queries)
{
if (command[0] == 1)
{
if (data.TryGetValue(command[1], out var val))
{
if (frequency.TryGetValue(val, out var val2))
frequency[val] = Math.Max(0, val2 - 1);
data[command[1]] = ++val;
if (frequency.TryGetValue(val, out var val3))
frequency[val] = val3 + 1;
else
frequency[val] = 1;
}
else
{
data.Add(command[1], 1);
if (frequency.TryGetValue(1, out var val3))
frequency[1] = val3 + 1;
else
frequency[1] = 1;
}
}
else if (command[0] == 2)
{
if (data.TryGetValue(command[1], out var val))
{
if (frequency.TryGetValue(val, out var val2))
frequency[val] = Math.Max(0, val2 - 1);
data[command[1]] = Math.Max(0, --val);
if (frequency.TryGetValue(val, out var val3))
frequency[val] = val3 + 1;
else
frequency[val] = 1;
}
}
else
{
result.Add(frequency.TryGetValue(command[1], out var val) && val > 0 ? 1 : 0);
}
}
return result;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int q = Convert.ToInt32(Console.ReadLine().Trim());
List<List<int>> queries = new List<List<int>>();
for (int i = 0; i < q; i++) {
queries.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(queriesTemp => Convert.ToInt32(queriesTemp)).ToList());
}
List<int> ans = freqQuery(queries);
textWriter.WriteLine(String.Join("\n", ans));
textWriter.Flush();
textWriter.Close();
}
}