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

+5-1
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

+4
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

+12-8
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

+7-3
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

+5-1
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

+5-1
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

+6-2
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

+5-1
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

+5-1
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

+5-1
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

threesumzero.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def three_sum_zero(nums: list[int]):
5+
def three_sum_zero(nums: list[int]) -> list:
6+
"""
7+
Space Complexity: O(n/3) => O(n)
8+
Time Complexity: O(n^2)
9+
"""
610
res = set() # a+b+c = 0 => c = -a-b
711
count = len(nums)
812
for i in range(count):

topkfrequent.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def top_k_frequent(nums, k):
5+
def top_k_frequent(nums: list[int], k: int) -> list[int]:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n)
9+
"""
610
cts = {}
711
for num in nums:
812
cts[num] = cts.get(num, 0) + 1

traprainwater.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

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

twosum.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
"""
33

44

5-
def two_sum(nums, target):
5+
def two_sum(nums: list[int], target: int) -> list[int]:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n)
9+
"""
610
sums = {}
711
for i, num in enumerate(nums):
812
if num in sums:
913
return [sums[num], i + 1]
1014
sums[target - num] = i + 1
15+
return [-1, -1]
1116

1217

1318
if __name__ == "__main__":

validparens.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"""
33

44

5-
def is_valid_parens(string):
5+
def is_valid_parens(string: str) -> bool:
6+
"""
7+
Space Complexity: O(n)
8+
Time Complexity: O(n)
9+
"""
610
stack = []
711
pair = {")": "(", "}": "{", "]": "["}
812
for char in string:

0 commit comments

Comments
 (0)