-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhanced-hashmap-add-a-number-to-all-keys-values.cpp
95 lines (88 loc) · 3.07 KB
/
enhanced-hashmap-add-a-number-to-all-keys-values.cpp
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
93
94
95
// Problem:
// You've created a new programming language, and now you've decided to add
// hashmap support to it. Actually you are quite disappointed that in common
// programming languages it's impossible to add a number to all hashmap keys, or
// all its values. So you've decided to take matters into your own hands and
// implement your own hashmap in your new language that has the following
// operations:
// insert x y - insert an object with key x and value y.
// get x - return the value of an object with key x.
// addToKey x - add x to all keys in map.
// addToValue y - add y to all values in map.
// To test out your new hashmap, you have a list of queries in the form of two
// arrays: queryTypes contains the names of the methods to be called (eg:
// insert, get, etc), and queries contains the arguments for those methods (the
// x and y values).
//
// Your task is to implement this hashmap, apply the given queries, and to find
// the sum of all the results for get operations.
//
// Example
//
// For queryType = ["insert", "insert", "addToValue", "addToKey", "get"] and
// query = [[1, 2], [2, 3], [2], [1], [3]], the output should be
// hashMap(queryType, query) = 5.
//
// The hashmap looks like this after each query:
//
// 1 query: {1: 2}
// 2 query: {1: 2, 2: 3}
// 3 query: {1: 4, 2: 5}
// 4 query: {2: 4, 3: 5}
// 5 query: answer is 5
// The result of the last get query for 3 is 5 in the resulting hashmap.
//
// For queryType = ["insert", "addToValue", "get", "insert", "addToKey",
// "addToValue", "get"] and query = [[1, 2], [2], [1], [2, 3], [1], [-1], [3]],
// the output should be hashMap(queryType, query) = 6.
//
// The hashmap looks like this after each query:
//
// 1 query: {1: 2}
// 2 query: {1: 4}
// 3 query: answer is 4
// 4 query: {1: 4, 2: 3}
// 5 query: {2: 4, 3: 3}
// 6 query: {2: 3, 3: 2}
// 7 query: answer is 2
// The sum of the results for all the get queries is equal to 4 + 2 = 6.
//
// Approach: the key & value we store in the map is the real one(without the
// bias) and we use 2 variables, ck and cv to record the bias soo no matter how
// the user add to key / value, only change the bias, but not the real one every
// time when return the value(GET), we add the bias to the real one
long long solution(vector<string> queryType, vector<vector<int>> query) {
int ck, cv = 0;
unordered_map<int, int> mp;
int n = query.size();
int ans = 0;
// loop every cmd
for (int i = 0; i < n; i++) {
string cmd = queryType[i];
vector<int> queryParam = query[i];
if (cmd == "addToKey") {
// update the key bias
ck += queryParam[0];
}
if (cmd == "addToValue") {
// update the value bias
cv += queryParam[0];
}
if (cmd == "insert") {
int key = queryParam[0];
int val = queryParam[1];
// only store the real one without bias
mp[key - ck] = val - cv;
}
// if GET
else {
int key = queryParam[0];
// the real key stored in the mp without bias
key -= ck;
// the real value need to add the cv bias
int val = mp[key] + val;
ans += val;
}
}
return ans;
}