GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixed tab spaces · letscode-17/coding-problems@f6719fa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Backtracking
    • jumping_numbers.py
    • power_set.py
    • queens_problem.py
    • river_sizes.py
  • Dynamic Programming
    • coin_change.py
    • max_subarray_sum.py
    • min_cost_coloring.py
    • number_of_decodings.py
    • sum_non-adjecent.py
    • transform_number_ascending_digits.py
    • word_break.py
  • Linked Lists
    • add_two_numbers.py
    • intersecting_ll.py
    • merge_k_sorted_ll.py
    • merge_sorted_ll.py
    • remove_nth_ll.py
    • reverse_ll.py
  • Lists
    • container_with_most_water.py
    • find_duplicates.py
    • find_first_missing_positive.py
    • find_one_missing_number.py
    • find_pairs_with_sum_k.py
    • find_peak_element.py
    • find_two_missing_numbers.py
    • jump_game.py
    • product_of_array_except_self.py
    • reverse_array.py
    • rotate_array.py
    • running_median.py
    • sort_rgb_array.py
    • subarray_with_sum_k.py
    • trapped_watter.py
  • Math
    • factorial_trailing_zeroes.py
    • smallest_multiple.py
    • sum_of_multiples.py
    • unique_paths.py
  • Other
    • power.py
    • valid_parentheses.py
  • Strings
    • anagram_indices.py
    • create_palindrome.py
    • encoding_string.py
    • longest_substring_without_repeating_characters.py
    • reverse_string.py
    • reverse_words_in_sentence.py
    • swap_first_and_last_word.py
    • zigzag_conversion.py
  • Trees
    • find_max_branch_sum.py
    • find_max_path_sum.py
    • find_second_largest_node.py
    • find_second_largest_node_bst.py
    • unival_trees.py
    • valid_bst.py

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Backtracking/jumping_numbers.py‎

Copy file name to clipboardExpand all lines: Backtracking/jumping_numbers.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def jumping_numbers(x):
2727
# take all 9 possible starting combinations
2828
for i in range(1, 10):
2929
jumping_num(i, x, result)
30-
30+
3131
return result
3232

3333
def jumping_num(num, x, result):
3434
if num > x:
3535
return
36-
36+
3737
result.append(num)
3838

3939
last_digit = num % 10
@@ -42,11 +42,11 @@ def jumping_num(num, x, result):
4242
# decrease the last digit by one
4343
if last_digit != 0:
4444
jumping_num(next_num + last_digit - 1, x, result)
45-
45+
4646
# increase the last digit by one
4747
if last_digit != 9:
4848
jumping_num(next_num + last_digit + 1, x, result)
49-
49+
5050

5151
###########
5252
# Testing #
Collapse file

‎Backtracking/power_set.py‎

Copy file name to clipboardExpand all lines: Backtracking/power_set.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
1111
=========================================
1212
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)
1515
'''
1616

1717

@@ -27,10 +27,10 @@ def power_set(arr):
2727
def combinations(arr, taken, pos):
2828
result = []
2929
result.append([arr[i] for i in taken]) # create the current combination
30-
30+
3131
if len(arr) == pos:
3232
return result
33-
33+
3434
n = len(arr)
3535
# start from the last position (don't need duplicates)
3636
for i in range(pos, n):
Collapse file

‎Backtracking/queens_problem.py‎

Copy file name to clipboardExpand all lines: Backtracking/queens_problem.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
=========================================
99
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)
1212
* There are much faster solutions, like O(N^2)
1313
'''
1414

@@ -29,7 +29,7 @@ def backtracking(columns, order):
2929

3030
if len(order) == n:
3131
return 1
32-
32+
3333
total = 0
3434

3535
for i in range(n):
Collapse file

‎Backtracking/river_sizes.py‎

Copy file name to clipboardExpand all lines: Backtracking/river_sizes.py
+48-48Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
=========================================
2222
This problem can be solved using DFS or BFS.
2323
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
2626
'''
2727

2828

@@ -31,53 +31,53 @@
3131
############
3232

3333
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+
5050
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
8181

8282

8383
###########
Collapse file

‎Dynamic Programming/coin_change.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/coin_change.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def coin_change_1(coins, amount):
3434
max_value = amount + 1 # use this instead of math.inf
3535
dp = [max_value for i in range(max_value)]
3636
dp[0] = 0
37-
37+
3838
for i in range(1, max_value):
3939
for c in coins:
4040
if c <= i:
@@ -60,7 +60,7 @@ def coin_change_2(coins, amount):
6060
max_coin = min(max_value, max(coins) + 1)
6161
dp = [max_value for i in range(max_coin)]
6262
dp[0] = 0
63-
63+
6464
for i in range(1, max_value):
6565
i_mod = i % max_coin
6666
dp[i_mod] = max_value # reset current position
Collapse file

‎Dynamic Programming/max_subarray_sum.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/max_subarray_sum.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Need only one iteration, in each step add the current element to the current sum.
1212
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)
1313
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)
1616
'''
1717

1818

Collapse file

‎Dynamic Programming/min_cost_coloring.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/min_cost_coloring.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(in this case we're sure that the previous house doesn't have the same color).
1313
Time Complexity: O(N * K)
1414
Space Complexity: O(1)
15-
'''
15+
'''
1616

1717
############
1818
# Solution #
@@ -41,14 +41,14 @@ def min_cost_coloring(dp):
4141
dp[i][j] += prev_min[0][0]
4242
else:
4343
dp[i][j] += prev_min[1][0]
44-
44+
4545
# save the current result if smaller than the current 2
4646
if curr_min[0][0] > dp[i][j]:
4747
curr_min[1] = curr_min[0]
4848
curr_min[0] = (dp[i][j], j)
4949
elif curr_min[1][0] > dp[i][j]:
5050
curr_min[1] = (dp[i][j], j)
51-
51+
5252
prev_min = curr_min
5353

5454
# return the min cost of the last house
Collapse file

‎Dynamic Programming/number_of_decodings.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/number_of_decodings.py
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
and in the worst case there will be Fibbionaci(N) combinations, so the worst Time Complexity will be O(Fib(N))
1111
1212
Dynamic programming solution.
13-
Time Complexity: O(N)
14-
Space Complexity: O(N)
13+
Time Complexity: O(N)
14+
Space Complexity: O(N)
1515
'''
1616

1717

@@ -22,24 +22,24 @@
2222
def num_decodings(code):
2323
n = len(code)
2424
dp = [0 for i in range(n)]
25-
25+
2626
if n == 0:
2727
return 0
2828
dp[0] = 1
2929
if n == 1:
3030
return dp[0]
3131
dp[1] = (code[1] != '0') + is_valid(code[0:2])
32-
32+
3333
for i in range(2, n):
3434
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
3636
dp[i] += dp[i-1]
3737
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
3939
dp[i] += dp[i-2]
40-
40+
4141
return dp[n-1]
42-
42+
4343
def is_valid(code):
4444
k = int(code)
4545
return (k < 27) and (k > 9)
Collapse file

‎Dynamic Programming/sum_non-adjecent.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/sum_non-adjecent.py
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
1515
=========================================
1616
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)
1919
'''
2020

2121

@@ -27,17 +27,17 @@ def sum_non_adjacent(arr):
2727
n = len(arr)
2828
# from the dp matrix you only need the last 3 sums
2929
sums = [0, 0, 0]
30-
30+
3131
# TODO: refactor these if-elses, those are to skip using of DP matrix
3232
if n == 0:
3333
return 0
3434

3535
# if negative or zero, the sum will be 0
3636
sums[0] = max(arr[0], 0)
37-
37+
3838
if n == 1:
3939
return sums[0]
40-
40+
4141
sums[1] = arr[1]
4242
# if the second number is negative or zero, then jump it
4343
if sums[1] <= 0:
@@ -56,17 +56,17 @@ def sum_non_adjacent(arr):
5656
# THE SOLUTION
5757
for i in range(3, n):
5858
temp = 0
59-
59+
6060
if arr[i] > 0:
6161
# take this number, because it's positive and the sum will be bigger
6262
temp = max(sums[0], sums[1]) + arr[i]
6363
else:
6464
# don't take this number, because the sum will be same or smaller
6565
temp = max(sums)
66-
66+
6767
# remove the first sum
6868
sums = sums[1:] + [temp]
69-
69+
7070
# return the max sum
7171
return max(sums)
7272

Collapse file

‎Dynamic Programming/transform_number_ascending_digits.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/transform_number_ascending_digits.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
1212
=========================================
1313
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)
1616
'''
1717

1818

@@ -33,7 +33,7 @@ def operations(number):
3333
# find the min value for the previous digit and add it to the current value
3434
curr_dp[j] += min(prev_dp[0 : j + 1])
3535
prev_dp = curr_dp
36-
36+
3737
# min value from the last digit
3838
min_dist = min(prev_dp)
3939

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.