GeezerVison is showing github.com — Fixed space complexities · danasmile/coding-problems@e68357f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Arrays/kth_smallest.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | This solution is based on the quick sort algorithm (pivoting, divide and conquer). | 17 | 17 | More precisly in-place quick sort. Recursive solution. | 18 | 18 | 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 | 20 | 20 | Completely the same algorithm as the previous one, but without recursion. This solution is cleaner. | 21 | 21 | Time Complexity: O(N) | 22 | 22 | Space Complexity: O(1) | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/reverse_array.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | ========================================= | 10 | 10 | 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): | 12 | 12 | - reversed_arr = reversed(arr) | 13 | 13 | - reversed_arr = arr[::-1] | 14 | 14 | 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. | |
Collapse file Copy file name to clipboardExpand all lines: Linked Lists/remove_nth_ll.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | Space Complexity: O(1) | 13 | 13 | Recursive solution. | 14 | 14 | 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 | 16 | 16 | ''' | 17 | 17 |
| 18 | 18 |
| |
Collapse file Copy file name to clipboardExpand all lines: Math/unique_paths.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ def unique_paths(n, m): | 45 | 45 | comb /= i | 46 | 46 | lvl -= 1 | 47 | 47 |
| 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 | 49 | 49 |
| 50 | 50 |
| 51 | 51 | ########### | |
Collapse file Copy file name to clipboardExpand all lines: Other/find_min_path.py +1-5Lines changed: 1 addition & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,11 +38,7 @@ def find_min_path(board, start, end): | 38 | 38 | m = len(board[0]) | 39 | 39 |
| 40 | 40 | # 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)] | 46 | 42 |
| 47 | 43 | queue = deque() | 48 | 44 | queue.append((start, 0)) | |
Collapse file Copy file name to clipboardExpand all lines: Other/generate_parentheses.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. | 5 | 5 |
| 6 | 6 | Input: 3 | 7 | | -Output: | | 7 | +Output: | 8 | 8 | [ | 9 | 9 | '((()))', | 10 | 10 | '(()())', | | 16 | 16 | ========================================= | 17 | 17 | 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. | 18 | 18 | 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) | 20 | 20 | Space Complexity: O(4^N) | 21 | 21 | ''' | 22 | 22 |
| |
Collapse file Copy file name to clipboardExpand all lines: Other/power.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | ========================================= | 7 | 7 | Using divide and conquer approach. | 8 | 8 | Time Complexity: O(LogB) | 9 | | - Space Complexity: O(LogB) , because of recursion call stack | | 9 | + Space Complexity: O(LogB) , because of recursion calls stack | 10 | 10 | ''' | 11 | 11 |
| 12 | 12 | ############ | |
Collapse file Copy file name to clipboardExpand all lines: Other/river_sizes.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | This problem can be solved using DFS or BFS. | 22 | 22 | If 1 is found, find all horizontal or vertical neighbours (1s), and mark them as 0. | 23 | 23 | 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 | 25 | 25 | ''' | 26 | 26 |
| 27 | 27 |
| |
Collapse file Copy file name to clipboardExpand all lines: Strings/zigzag_conversion.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | Go row by row and using the steps logic build the new string by jumping chars. | 16 | 16 | Middle rows have 2 times more elements than the first and last row. | 17 | 17 | Time Complexity: O(N) | 18 | | - Space Complexity: O(1) | | 18 | + Space Complexity: O(N) | 19 | 19 | ''' | 20 | 20 |
| 21 | 21 |
| |
Collapse file Copy file name to clipboardExpand all lines: Trees/unival_trees.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | ========================================= | 18 | 18 | Simple tree traversal solution. | 19 | 19 | 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. | 21 | 21 | ''' | 22 | 22 |
| 23 | 23 |
| |
|