Skip to content

Commit e68357f

Browse files
author
Мето Трајковски
committed
Fixed space complexities
1 parent 6d2f350 commit e68357f

10 files changed

+11
-15
lines changed

Diff for: Arrays/kth_smallest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
This solution is based on the quick sort algorithm (pivoting, divide and conquer).
1717
More precisly in-place quick sort. Recursive solution.
1818
Time Complexity: O(N) , O(N + N/2 + N/4 + N/8 + ... + 1 = 2*N = N)
19-
Space Complexity: O(LogN) , because of the recursion stack (if this doesn't count, then O(1))
19+
Space Complexity: O(LogN) , because of the recursion stack
2020
Completely the same algorithm as the previous one, but without recursion. This solution is cleaner.
2121
Time Complexity: O(N)
2222
Space Complexity: O(1)

Diff for: Arrays/reverse_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
=========================================
1010
Reverse the whole array by swapping pair letters in-place (first with last, second with second from the end, etc).
11-
Exist 2 more "Pythonic" ways of reversing arrays/strings:
11+
Exist 2 more "Pythonic" ways of reversing arrays/strings (but not in-place, they're creating a new list):
1212
- reversed_arr = reversed(arr)
1313
- reversed_arr = arr[::-1]
1414
But I wanted to show how to implement a reverse algorithm step by step so someone will know how to implement it in other languages.

Diff for: Linked Lists/remove_nth_ll.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Space Complexity: O(1)
1313
Recursive solution.
1414
Time Complexity: O(N)
15-
Space Complexity: O(N) (Because of the stack of recursive calls)
15+
Space Complexity: O(N) , because of the recursive calls stack
1616
'''
1717

1818

Diff for: Math/unique_paths.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def unique_paths(n, m):
4545
comb /= i
4646
lvl -= 1
4747

48-
return int(comb + 0.001) # 0.001 just in case because of overflow
48+
return int(comb + 0.001) # 0.001 just in case, because of overflow
4949

5050

5151
###########

Diff for: Other/find_min_path.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ def find_min_path(board, start, end):
3838
m = len(board[0])
3939

4040
# create a visited array
41-
visited = []
42-
for row in range(n):
43-
visited.append([])
44-
for el in range(m):
45-
visited[row].append(False)
41+
visited = [[False for el in range(m)] for row in range(n)]
4642

4743
queue = deque()
4844
queue.append((start, 0))

Diff for: Other/generate_parentheses.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
55
66
Input: 3
7-
Output:
7+
Output:
88
[
99
'((()))',
1010
'(()())',
@@ -16,7 +16,7 @@
1616
=========================================
1717
This problem could be solved in several ways (using stack, queue, or just a simple list - see letter_combinations.py), all of them have the same complexity.
1818
I'll solve it using simple recursive algorithm.
19-
Time Complexity: O(4^N) , O(2^(2*N)) = O(2^N)
19+
Time Complexity: O(4^N) , O(2^(2*N)) = O(4^N)
2020
Space Complexity: O(4^N)
2121
'''
2222

Diff for: Other/power.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
=========================================
77
Using divide and conquer approach.
88
Time Complexity: O(LogB)
9-
Space Complexity: O(LogB) , because of recursion call stack
9+
Space Complexity: O(LogB) , because of recursion calls stack
1010
'''
1111

1212
############

Diff for: Other/river_sizes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
This problem can be solved using DFS or BFS.
2222
If 1 is found, find all horizontal or vertical neighbours (1s), and mark them as 0.
2323
Time Complexity: O(N*M)
24-
Space Complexity: O(R) , R = num of rivers
24+
Space Complexity: O(N*M) , because of recursion calls stack
2525
'''
2626

2727

Diff for: Strings/zigzag_conversion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Go row by row and using the steps logic build the new string by jumping chars.
1616
Middle rows have 2 times more elements than the first and last row.
1717
Time Complexity: O(N)
18-
Space Complexity: O(1)
18+
Space Complexity: O(N)
1919
'''
2020

2121

Diff for: Trees/unival_trees.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
=========================================
1818
Simple tree traversal solution.
1919
Time Complexity: O(N)
20-
Space Complexity: O(1)
20+
Space Complexity: O(N) , because of the recursion stack (but this is if the tree is one branch), O(LogN) if the tree is balanced.
2121
'''
2222

2323

0 commit comments

Comments
 (0)