Skip to content

Commit 367283e

Browse files
committed
Runtime: 652 ms (Top 85.73%) | Memory: 179 MB (Top 14.04%)
1 parent 7fc6a4d commit 367283e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
// Runtime: 652 ms (Top 85.73%) | Memory: 179 MB (Top 14.04%)
12
class NumberContainers {
23
public:
34
map<int, int> indexToNumber; // stores number corresponding to an index.
45
map<int, set<int>>numberToIndex; // stores all the indexes corresponding to a number.
56
NumberContainers() {}
6-
7+
78
void change(int index, int number) {
8-
9+
910
if (!indexToNumber.count(index)) { // if there is no number at the given index.
1011
numberToIndex[number].insert(index); // store index corresponding to the given number
1112
indexToNumber[index] = number; // store number corresponding to the index.
1213
}
1314
else { // Update both map.
14-
15+
1516
int num = indexToNumber[index]; // number at given index currently.
16-
17+
1718
// remove the index
18-
numberToIndex[num].erase(index);
19-
if (numberToIndex[num].empty()) numberToIndex.erase(num);
20-
21-
// insert the new number at the given index and store the index corresponding to that number.
19+
numberToIndex[num].erase(index);
20+
if (numberToIndex[num].empty()) numberToIndex.erase(num);
21+
22+
// insert the new number at the given index and store the index corresponding to that number.
2223
numberToIndex[number].insert(index);
2324
indexToNumber[index] = number;
24-
}
25+
}
2526
}
26-
27+
2728
int find(int number) {
28-
if (!numberToIndex.count(number)) return -1;
29+
if (!numberToIndex.count(number)) return -1;
2930
// returning first element in the set as it will be the smallest index always.
30-
return *numberToIndex[number].begin();
31+
return *numberToIndex[number].begin();
3132
}
32-
};
33+
};

0 commit comments

Comments
 (0)