Skip to content

Commit 7e2afd3

Browse files
committed
Feb 8
1 parent c7bd716 commit 7e2afd3

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from heapq import heappush, heappop
2+
from collections import defaultdict
3+
4+
5+
class NumberContainers:
6+
def __init__(self):
7+
self.idx_num_map = defaultdict(int)
8+
self.num_idxs_map = defaultdict(list)
9+
10+
def change(self, index: int, number: int) -> None:
11+
self.idx_num_map[index] = number
12+
heappush(self.num_idxs_map[number], index)
13+
14+
def find(self, number: int) -> int:
15+
while self.num_idxs_map[number]:
16+
smallest_idx = self.num_idxs_map[number][0]
17+
if self.idx_num_map[smallest_idx] == number:
18+
return smallest_idx
19+
heappop(self.num_idxs_map[number])
20+
return -1
21+
22+
23+
def main():
24+
nc = NumberContainers()
25+
assert nc.find(10) == -1
26+
nc.change(2, 10)
27+
nc.change(1, 10)
28+
nc.change(3, 10)
29+
nc.change(5, 10)
30+
assert nc.find(10) == 1
31+
nc.change(1, 20)
32+
assert nc.find(10) == 2
33+
34+
35+
if __name__ == '__main__':
36+
main()

2025-02-February-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| February 05 | [1790. Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/) | Easy | Solved |
1212
| February 06 | [1726. Tuple with Same Product](https://leetcode.com/problems/tuple-with-same-product/) | Medium | Solved |
1313
| February 07 | [3160. Find the Number of Distinct Colors Among the Balls](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/) | Medium | Solved |
14-
| February 08 | []() | | |
14+
| February 08 | [2349. Design a Number Container System](https://leetcode.com/problems/design-a-number-container-system/) | Medium | Solved |
1515
| February 09 | []() | | |
1616
| February 10 | []() | | |
1717
| February 11 | []() | | |
@@ -38,5 +38,5 @@
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 5 | 5 | 0 |
41-
| Medium | 2 | 2 | 0 |
41+
| Medium | 3 | 3 | 0 |
4242
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)