-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path1352-product-of-the-last-k-numbers.py
45 lines (40 loc) · 1.28 KB
/
1352-product-of-the-last-k-numbers.py
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
# time complexity: O(1)
# space complexity: O(n)
class ProductOfNumbers:
def __init__(self):
self.prefixProduct = [1]
self.zeroIdx = float('inf')
self.n = 0
def add(self, num: int) -> None:
if num == 0:
self.zeroIdx = self.n
num = 1
if self.prefixProduct:
self.prefixProduct.append(self.prefixProduct[-1] * num)
else:
self.prefixProduct.append(num)
self.n += 1
def getProduct(self, k: int) -> int:
if self.zeroIdx != float('inf') and self.n - k <= self.zeroIdx:
return 0
return self.prefixProduct[-1] // self.prefixProduct[-k - 1]
productOfNumbers = ProductOfNumbers()
productOfNumbers.add(0)
productOfNumbers.add(5)
productOfNumbers.add(6)
print(productOfNumbers.getProduct(2))
print(productOfNumbers.getProduct(2))
productOfNumbers.add(8)
print(productOfNumbers.getProduct(4))
productOfNumbers.add(2)
# productOfNumbers = ProductOfNumbers()
# productOfNumbers.add(3)
# productOfNumbers.add(0)
# productOfNumbers.add(2)
# productOfNumbers.add(5)
# productOfNumbers.add(4)
# print(productOfNumbers.getProduct(2))
# print(productOfNumbers.getProduct(3))
# print(productOfNumbers.getProduct(4))
# productOfNumbers.add(8)
# print(productOfNumbers.getProduct(2))