GeezerVison is showing github.com — Fixed tab spaces · letscode-17/coding-problems@f6719fa
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Backtracking/jumping_numbers.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,13 +27,13 @@ def jumping_numbers(x): | 27 | 27 | # take all 9 possible starting combinations | 28 | 28 | for i in range(1, 10): | 29 | 29 | jumping_num(i, x, result) | 30 | | - | | 30 | + | 31 | 31 | return result | 32 | 32 |
| 33 | 33 | def jumping_num(num, x, result): | 34 | 34 | if num > x: | 35 | 35 | return | 36 | | - | | 36 | + | 37 | 37 | result.append(num) | 38 | 38 |
| 39 | 39 | last_digit = num % 10 | @@ -42,11 +42,11 @@ def jumping_num(num, x, result): | 42 | 42 | # decrease the last digit by one | 43 | 43 | if last_digit != 0: | 44 | 44 | jumping_num(next_num + last_digit - 1, x, result) | 45 | | - | | 45 | + | 46 | 46 | # increase the last digit by one | 47 | 47 | if last_digit != 9: | 48 | 48 | jumping_num(next_num + last_digit + 1, x, result) | 49 | | - | | 49 | + | 50 | 50 |
| 51 | 51 | ########### | 52 | 52 | # Testing # | |
Collapse file Copy file name to clipboardExpand all lines: Backtracking/power_set.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 |
| 11 | 11 | ========================================= | 12 | 12 | Simple recursive combinations algorithm. | 13 | | - Time Complexity: O(Sum(C(I, N))) , sum of all combinations between 0 and N = C(0, N) + C(1, N) + ... + C(N, N) | 14 | | - Space Complexity: O(Sum(C(I, N))) , this is for the result array, if we print the number then the space complexity will be O(1) | | 13 | + Time Complexity: O(Sum(C(I, N))) , sum of all combinations between 0 and N = C(0, N) + C(1, N) + ... + C(N, N) | | 14 | + Space Complexity: O(Sum(C(I, N))) , this is for the result array, if we print the number then the space complexity will be O(1) | 15 | 15 | ''' | 16 | 16 |
| 17 | 17 |
| @@ -27,10 +27,10 @@ def power_set(arr): | 27 | 27 | def combinations(arr, taken, pos): | 28 | 28 | result = [] | 29 | 29 | result.append([arr[i] for i in taken]) # create the current combination | 30 | | - | | 30 | + | 31 | 31 | if len(arr) == pos: | 32 | 32 | return result | 33 | | - | | 33 | + | 34 | 34 | n = len(arr) | 35 | 35 | # start from the last position (don't need duplicates) | 36 | 36 | for i in range(pos, n): | |
Collapse file Copy file name to clipboardExpand all lines: Backtracking/queens_problem.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | ========================================= | 9 | 9 | Backtracking solution. | 10 | | - Time Complexity: O(N!) (but I think it's much faster!) | 11 | | - Space Complexity: O(N) | | 10 | + Time Complexity: O(N!) (but I think it's much faster!) | | 11 | + Space Complexity: O(N) | 12 | 12 | * There are much faster solutions, like O(N^2) | 13 | 13 | ''' | 14 | 14 |
| @@ -29,7 +29,7 @@ def backtracking(columns, order): | 29 | 29 |
| 30 | 30 | if len(order) == n: | 31 | 31 | return 1 | 32 | | - | | 32 | + | 33 | 33 | total = 0 | 34 | 34 |
| 35 | 35 | for i in range(n): | |
Collapse file Copy file name to clipboardExpand all lines: Backtracking/river_sizes.py +48-48Lines changed: 48 additions & 48 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | ========================================= | 22 | 22 | This problem can be solved using DFS or BFS. | 23 | 23 | If 1 is found, find all horizontal or vertical neighbours (1s), and mark them as 0. | 24 | | - Time Complexity: O(N*M) | 25 | | - Space Complexity: O(R) , R = num of rivers | | 24 | + Time Complexity: O(N*M) | | 25 | + Space Complexity: O(R) , R = num of rivers | 26 | 26 | ''' | 27 | 27 |
| 28 | 28 |
| | 31 | 31 | ############ | 32 | 32 |
| 33 | 33 | def river_sizes(matrix): | 34 | | - n = len(matrix) | 35 | | - m = len(matrix[0]) | 36 | | - | 37 | | - results = [] | 38 | | - | 39 | | - for i in range(n): | 40 | | - for j in range(m): | 41 | | - if matrix[i][j] != 0: | 42 | | - # find the river size | 43 | | - size = dfs((i, j), matrix) | 44 | | - | 45 | | - # save the river size | 46 | | - results.append(size) | 47 | | - | 48 | | - return results | 49 | | - | | 34 | + n = len(matrix) | | 35 | + m = len(matrix[0]) | | 36 | + | | 37 | + results = [] | | 38 | + | | 39 | + for i in range(n): | | 40 | + for j in range(m): | | 41 | + if matrix[i][j] != 0: | | 42 | + # find the river size | | 43 | + size = dfs((i, j), matrix) | | 44 | + | | 45 | + # save the river size | | 46 | + results.append(size) | | 47 | + | | 48 | + return results | | 49 | + | 50 | 50 | def dfs(coord, matrix): | 51 | | - (i, j) = coord | 52 | | - | 53 | | - if i < 0 or j < 0: | 54 | | - # invalid position | 55 | | - return 0 | 56 | | - | 57 | | - n = len(matrix) | 58 | | - m = len(matrix[0]) | 59 | | - | 60 | | - if i == n or j == m: | 61 | | - # invalid position | 62 | | - return 0 | 63 | | - | 64 | | - if matrix[i][j] == 0: | 65 | | - # not a river | 66 | | - return 0 | 67 | | - | 68 | | - # update the matrix, the matrix is passed by reference | 69 | | - matrix[i][j] = 0 | 70 | | - # this position is part of river | 71 | | - size = 1 | 72 | | - | 73 | | - # directions: down, left, up, right | 74 | | - dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)] | 75 | | - | 76 | | - # check all 4 directions | 77 | | - for d in dirs: | 78 | | - size += dfs((i + d[0], j + d[1]), matrix) | 79 | | - | 80 | | - return size | | 51 | + (i, j) = coord | | 52 | + | | 53 | + if i < 0 or j < 0: | | 54 | + # invalid position | | 55 | + return 0 | | 56 | + | | 57 | + n = len(matrix) | | 58 | + m = len(matrix[0]) | | 59 | + | | 60 | + if i == n or j == m: | | 61 | + # invalid position | | 62 | + return 0 | | 63 | + | | 64 | + if matrix[i][j] == 0: | | 65 | + # not a river | | 66 | + return 0 | | 67 | + | | 68 | + # update the matrix, the matrix is passed by reference | | 69 | + matrix[i][j] = 0 | | 70 | + # this position is part of river | | 71 | + size = 1 | | 72 | + | | 73 | + # directions: down, left, up, right | | 74 | + dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)] | | 75 | + | | 76 | + # check all 4 directions | | 77 | + for d in dirs: | | 78 | + size += dfs((i + d[0], j + d[1]), matrix) | | 79 | + | | 80 | + return size | 81 | 81 |
| 82 | 82 |
| 83 | 83 | ########### | |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/coin_change.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,7 @@ def coin_change_1(coins, amount): | 34 | 34 | max_value = amount + 1 # use this instead of math.inf | 35 | 35 | dp = [max_value for i in range(max_value)] | 36 | 36 | dp[0] = 0 | 37 | | - | | 37 | + | 38 | 38 | for i in range(1, max_value): | 39 | 39 | for c in coins: | 40 | 40 | if c <= i: | @@ -60,7 +60,7 @@ def coin_change_2(coins, amount): | 60 | 60 | max_coin = min(max_value, max(coins) + 1) | 61 | 61 | dp = [max_value for i in range(max_coin)] | 62 | 62 | dp[0] = 0 | 63 | | - | | 63 | + | 64 | 64 | for i in range(1, max_value): | 65 | 65 | i_mod = i % max_coin | 66 | 66 | dp[i_mod] = max_value # reset current position | |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/max_subarray_sum.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | Need only one iteration, in each step add the current element to the current sum. | 12 | 12 | When the sum is equal or less than 0, reset the sum to 0 and continue with adding. (we care only about the positive sums) | 13 | 13 | After each addition, check if the current sum is greater than the max sum. (Called Kadane's algorithm) | 14 | | - Time Complexity: O(N) | 15 | | - Space Complexity: O(1) | | 14 | + Time Complexity: O(N) | | 15 | + Space Complexity: O(1) | 16 | 16 | ''' | 17 | 17 |
| 18 | 18 |
| |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/min_cost_coloring.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | (in this case we're sure that the previous house doesn't have the same color). | 13 | 13 | Time Complexity: O(N * K) | 14 | 14 | Space Complexity: O(1) | 15 | | -''' | | 15 | +''' | 16 | 16 |
| 17 | 17 | ############ | 18 | 18 | # Solution # | @@ -41,14 +41,14 @@ def min_cost_coloring(dp): | 41 | 41 | dp[i][j] += prev_min[0][0] | 42 | 42 | else: | 43 | 43 | dp[i][j] += prev_min[1][0] | 44 | | - | | 44 | + | 45 | 45 | # save the current result if smaller than the current 2 | 46 | 46 | if curr_min[0][0] > dp[i][j]: | 47 | 47 | curr_min[1] = curr_min[0] | 48 | 48 | curr_min[0] = (dp[i][j], j) | 49 | 49 | elif curr_min[1][0] > dp[i][j]: | 50 | 50 | curr_min[1] = (dp[i][j], j) | 51 | | - | | 51 | + | 52 | 52 | prev_min = curr_min | 53 | 53 |
| 54 | 54 | # return the min cost of the last house | |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/number_of_decodings.py +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | and in the worst case there will be Fibbionaci(N) combinations, so the worst Time Complexity will be O(Fib(N)) | 11 | 11 |
| 12 | 12 | Dynamic programming solution. | 13 | | - Time Complexity: O(N) | 14 | | - Space Complexity: O(N) | | 13 | + Time Complexity: O(N) | | 14 | + Space Complexity: O(N) | 15 | 15 | ''' | 16 | 16 |
| 17 | 17 |
| | 22 | 22 | def num_decodings(code): | 23 | 23 | n = len(code) | 24 | 24 | dp = [0 for i in range(n)] | 25 | | - | | 25 | + | 26 | 26 | if n == 0: | 27 | 27 | return 0 | 28 | 28 | dp[0] = 1 | 29 | 29 | if n == 1: | 30 | 30 | return dp[0] | 31 | 31 | dp[1] = (code[1] != '0') + is_valid(code[0:2]) | 32 | | - | | 32 | + | 33 | 33 | for i in range(2, n): | 34 | 34 | if code[i] != '0': | 35 | | - # looking for how many combinations are there till now if this is a single digit | | 35 | + # looking for how many combinations are there till now if this is a single digit | 36 | 36 | dp[i] += dp[i-1] | 37 | 37 | if is_valid(code[i-1 : i+1]): | 38 | | - # looking for how many combinations are there till now if this is a number of 2 digits | | 38 | + # looking for how many combinations are there till now if this is a number of 2 digits | 39 | 39 | dp[i] += dp[i-2] | 40 | | - | | 40 | + | 41 | 41 | return dp[n-1] | 42 | | - | | 42 | + | 43 | 43 | def is_valid(code): | 44 | 44 | k = int(code) | 45 | 45 | return (k < 27) and (k > 9) | |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/sum_non-adjecent.py +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 |
| 15 | 15 | ========================================= | 16 | 16 | Dynamic programming solution, but don't need the whole DP array, only the last 3 sums (DPs) are needed. | 17 | | - Time Complexity: O(N) | 18 | | - Space Complexity: O(1) | | 17 | + Time Complexity: O(N) | | 18 | + Space Complexity: O(1) | 19 | 19 | ''' | 20 | 20 |
| 21 | 21 |
| @@ -27,17 +27,17 @@ def sum_non_adjacent(arr): | 27 | 27 | n = len(arr) | 28 | 28 | # from the dp matrix you only need the last 3 sums | 29 | 29 | sums = [0, 0, 0] | 30 | | - | | 30 | + | 31 | 31 | # TODO: refactor these if-elses, those are to skip using of DP matrix | 32 | 32 | if n == 0: | 33 | 33 | return 0 | 34 | 34 |
| 35 | 35 | # if negative or zero, the sum will be 0 | 36 | 36 | sums[0] = max(arr[0], 0) | 37 | | - | | 37 | + | 38 | 38 | if n == 1: | 39 | 39 | return sums[0] | 40 | | - | | 40 | + | 41 | 41 | sums[1] = arr[1] | 42 | 42 | # if the second number is negative or zero, then jump it | 43 | 43 | if sums[1] <= 0: | @@ -56,17 +56,17 @@ def sum_non_adjacent(arr): | 56 | 56 | # THE SOLUTION | 57 | 57 | for i in range(3, n): | 58 | 58 | temp = 0 | 59 | | - | | 59 | + | 60 | 60 | if arr[i] > 0: | 61 | 61 | # take this number, because it's positive and the sum will be bigger | 62 | 62 | temp = max(sums[0], sums[1]) + arr[i] | 63 | 63 | else: | 64 | 64 | # don't take this number, because the sum will be same or smaller | 65 | 65 | temp = max(sums) | 66 | | - | | 66 | + | 67 | 67 | # remove the first sum | 68 | 68 | sums = sums[1:] + [temp] | 69 | | - | | 69 | + | 70 | 70 | # return the max sum | 71 | 71 | return max(sums) | 72 | 72 |
| |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/transform_number_ascending_digits.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 |
| 12 | 12 | ========================================= | 13 | 13 | Dynamic programming solution. | 14 | | - Time Complexity: O(N) , O(N * 10 * 10) = O(100 N) = O(N) | 15 | | - Space Complexity: O(1) , O(10 * 10) = O(100) = O(1) | | 14 | + Time Complexity: O(N) , O(N * 10 * 10) = O(100 N) = O(N) | | 15 | + Space Complexity: O(1) , O(10 * 10) = O(100) = O(1) | 16 | 16 | ''' | 17 | 17 |
| 18 | 18 |
| @@ -33,7 +33,7 @@ def operations(number): | 33 | 33 | # find the min value for the previous digit and add it to the current value | 34 | 34 | curr_dp[j] += min(prev_dp[0 : j + 1]) | 35 | 35 | prev_dp = curr_dp | 36 | | - | | 36 | + | 37 | 37 | # min value from the last digit | 38 | 38 | min_dist = min(prev_dp) | 39 | 39 |
| |
|