Skip to content

Commit a910d04

Browse files
committed
refactor: add types and complexity
1 parent ee6474f commit a910d04

15 files changed

+85
-24
lines changed

containermaxwater.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def container_with_max_water(height):
5+
def container_with_max_water(height: list[int]) -> int:
6+
"""
7+
Space Complexity: O(1)
8+
Time Complexity: O(n)
9+
"""
610
ans, count = height[0], len(height)
711
for i in range(1, count - 2):
812
j = count - i

evalrpn.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44

55
def eval_rpn(tokens: list[str]) -> int:
6+
"""
7+
Space Complexity: O(1)
8+
Time Complexity: O(n)
9+
"""
610
stack = []
711
for i, token in enumerate(tokens):
812
if token not in "+-*/":

generateparens.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"""
33

44

5-
def generate_parenthesis(n: int) -> list:
6-
ans = []
7-
def generate(open: int, close: int, gen: str):
8-
if open == n and close == n:
5+
def generate_parenthesis(n: int) -> list[str]:
6+
"""
7+
Space Complexity: O(1)
8+
Time Complexity: O(n)
9+
"""
10+
def generate(open_count: int, close_count: int, gen: str):
11+
if open_count == n and close_count == n:
912
ans.append(gen)
1013
return
11-
if open < n:
12-
generate(open + 1, close, gen + '(')
13-
if open > close:
14-
generate(open, close+1, gen + ')')
14+
if open_count < n:
15+
generate(open_count + 1, close_count, gen + '(')
16+
if open_count > close_count:
17+
generate(open_count, close_count + 1, gen + ')')
1518

19+
ans = []
1620
generate(0, 0, "")
1721
return ans
1822

groupanagrams.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
"""
33

44

5-
def group_anagrams(strings):
5+
def group_anagrams(strings: list[str]) -> list[str]:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n)
9+
"""
610
groups = {}
711
for string in strings:
812
sort = "".join(sorted(string))
913
if sort not in groups:
1014
groups[sort] = []
1115
groups[sort].append(string)
12-
return groups.values()
16+
return list(groups.values())
1317

1418

1519
if __name__ == "__main__":
16-
print(group_anagrams(["abc", "bac", "def", "fed", "efd", "ghi"]))
20+
print(group_anagrams(["abc", "def", "bac", "fed", "ghi", "efd"]))
1721
# => [['abc', 'bac'], ['def', 'fed', 'efd'], ['ghi']]

isanagram.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def is_anagram(string, target):
5+
def is_anagram(string: str, target: str) -> bool:
6+
"""
7+
Space Complexity: O(m)
8+
Time Complexity: O(m+n)
9+
"""
610
if len(string) != len(target):
711
return False
812
counts = {}

longestconsecutiveseq.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def longest_consecutive(nums):
5+
def longest_consecutive(nums: list[int]) -> int:
6+
"""
7+
Space Complexity: O(1)
8+
Time Complexity: O(n)
9+
"""
610
count, nums = len(nums), sorted(nums)
711
run, ans = 1, 1
812
for i, num in enumerate(nums):

longestsubstrlen.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
"""
33

44

5-
def length_of_longest_substring(string):
5+
def length_of_longest_substring(string: str) -> int:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n)
9+
"""
610
left, right, max_left, win = 0, 0, 0, {}
711
while right < len(string):
812
if not win.get(string[right]):
9-
win[string[right]] = (True,)
13+
win[string[right]] = True
1014
max_left = max(right - left + 1, max_left)
1115
else:
1216
while string[left] != string[right]:

productexceptself.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def product_except_self(nums):
5+
def product_except_self(nums: list[int]) -> list[int]:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n+n) => O(n)
9+
"""
610
count = len(nums)
711
res = [1] * count
812
pre, post, zero = 1, 1, 0

stockmaxprofit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def stock_max_profit(prices):
5+
def stock_max_profit(prices: list[int]) -> int:
6+
"""
7+
Space Complexity: O(1)
8+
Time Complexity: O(n)
9+
"""
610
profit, min_price = 0, prices[0]
711
for i in range(1, len(prices) - 1):
812
if prices[i] < min_price:

substrpermute.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def check_str_permute_sub(string1, string2):
5+
def check_str_permute_sub(string1: str, string2: str) -> bool:
6+
"""
7+
Space Complexity: O(m+n)
8+
Time Complexity: O(m+n)
9+
"""
610
s1count, s2count = {}, {}
711
for char in string1:
812
s1count[char] = s1count.get(char, 0) + 1

0 commit comments

Comments
 (0)