This commit is contained in:
parent
e959868af3
commit
fe48fc667a
1 changed files with 25 additions and 0 deletions
25
medium/product_of_the_last_k_numbers.py
Normal file
25
medium/product_of_the_last_k_numbers.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# https://leetcode.com/problems/product-of-the-last-k-numbers
|
||||
|
||||
from typing import List
|
||||
|
||||
class ProductOfNumbers:
|
||||
prefix: List[int]
|
||||
|
||||
def __init__(self):
|
||||
self.prefix = []
|
||||
|
||||
def add(self, num: int) -> None:
|
||||
if num == 0:
|
||||
self.prefix = []
|
||||
elif len(self.prefix) >= 1:
|
||||
self.prefix.append(self.prefix[-1] * num)
|
||||
else:
|
||||
self.prefix.append(num)
|
||||
|
||||
def getProduct(self, k: int) -> int:
|
||||
if k > len(self.prefix):
|
||||
return 0
|
||||
if k == len(self.prefix):
|
||||
return self.prefix[-1]
|
||||
return self.prefix[-1] // self.prefix[-k -1]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue