GeezerVison is showing github.com — Added matrix solutions · johnsonhit/coding-problems@be79796
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 clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Search a 2D Matrix | | 3 | +
| | 4 | +Write an efficient algorithm that searches for a value in an m x n matrix. | | 5 | +This matrix has the following properties: | | 6 | +- Integers in each row are sorted in ascending from left to right. | | 7 | +- Integers in each column are sorted in ascending from top to bottom. | | 8 | +
| | 9 | +Input: target = 21 | | 10 | +[ | | 11 | + [1, 4, 7, 11, 15], | | 12 | + [2, 5, 8, 12, 19], | | 13 | + [3, 6, 9, 16, 22], | | 14 | + [10, 13, 14, 17, 24], | | 15 | + [18, 21, 23, 26, 30] | | 16 | +] | | 17 | +Output: True | | 18 | +
| | 19 | +========================================= | | 20 | +Start from bottom left corner and search in top right direction. | | 21 | + Time Complexity: O(N + M) | | 22 | + Space Complexity: O(1) | | 23 | +''' | | 24 | + | | 25 | + | | 26 | +############ | | 27 | +# Solution # | | 28 | +############ | | 29 | + | | 30 | +def search_2d_matrix(matrix, target): | | 31 | + n = len(matrix) | | 32 | + m = len(matrix[0]) | | 33 | + | | 34 | + j = 0 | | 35 | + i = n - 1 | | 36 | + | | 37 | + while (i >= 0) and (j < m): | | 38 | + if matrix[i][j] > target: | | 39 | + i -= 1 | | 40 | + elif matrix[i][j] < target: | | 41 | + j += 1 | | 42 | + else: | | 43 | + return True | | 44 | + | | 45 | + return False | | 46 | + | | 47 | + | | 48 | +########### | | 49 | +# Testing # | | 50 | +########### | | 51 | + | | 52 | +# Test 1 | | 53 | +# Correct result => True | | 54 | +print(search_2d_matrix([[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]], 21)) | | 55 | + | | 56 | +# Test 2 | | 57 | +# Correct result => False | | 58 | +print(search_2d_matrix([[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]], 20)) |
Collapse file Copy file name to clipboard +83Lines changed: 83 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Set Matrix Zeroes | | 3 | +
| | 4 | +Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. | | 5 | +
| | 6 | +Input: | | 7 | +[ | | 8 | + [1,1,1], | | 9 | + [1,0,1], | | 10 | + [1,1,1] | | 11 | +] | | 12 | +Output: | | 13 | +[ | | 14 | + [1,0,1], | | 15 | + [0,0,0], | | 16 | + [1,0,1] | | 17 | +] | | 18 | +
| | 19 | +========================================= | | 20 | +Use first column and first row for marking when 0 is found. | | 21 | + Time Complexity: O(N*M) | | 22 | + Space Complexity: O(1) | | 23 | +''' | | 24 | + | | 25 | + | | 26 | +############ | | 27 | +# Solution # | | 28 | +############ | | 29 | + | | 30 | +def set_matrix_zeroes(matrix): | | 31 | + n = len(matrix) | | 32 | + if n == 0: | | 33 | + return | | 34 | + m = len(matrix[0]) | | 35 | + | | 36 | + # check if 0 exist in first row | | 37 | + is_row = False | | 38 | + for j in range(m): | | 39 | + if matrix[0][j] == 0: | | 40 | + is_row = True | | 41 | + # check if 0 exist in first column | | 42 | + is_col = False | | 43 | + for i in range(n): | | 44 | + if matrix[i][0] == 0: | | 45 | + is_col = True | | 46 | + | | 47 | + # find which columns and rows should be 0 | | 48 | + for i in range(1, n): | | 49 | + for j in range(1, m): | | 50 | + if matrix[i][j] == 0: | | 51 | + matrix[i][0] = matrix[0][j] = 0 | | 52 | + | | 53 | + # set 0 if the row or column where this element is located is 0 | | 54 | + for i in range(1, n): | | 55 | + for j in range(1, m): | | 56 | + if (matrix[i][0] == 0) or (matrix[0][j] == 0): | | 57 | + matrix[i][j] = 0 | | 58 | + | | 59 | + # fill the first row with 0 if needed | | 60 | + if is_row: | | 61 | + for j in range(m): | | 62 | + matrix[0][j] = 0 | | 63 | + # fill the first column with 0 if needed | | 64 | + if is_col: | | 65 | + for i in range(n): | | 66 | + matrix[i][0] = 0 | | 67 | + | | 68 | + | | 69 | +########### | | 70 | +# Testing # | | 71 | +########### | | 72 | + | | 73 | +# Test 1 | | 74 | +# Correct result => [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]] | | 75 | +mat = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]] | | 76 | +set_matrix_zeroes(mat) | | 77 | +print(mat) | | 78 | + | | 79 | +# Test 2 | | 80 | +# Correct result => [[1, 0, 1], [0, 0, 0], [1, 0, 1]] | | 81 | +mat = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] | | 82 | +set_matrix_zeroes(mat) | | 83 | +print(mat) |
Collapse file Copy file name to clipboard +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Spiral Matrix | | 3 | +
| | 4 | +Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. | | 5 | +
| | 6 | +Input: | | 7 | +[ | | 8 | + [ 1, 2, 3 ], | | 9 | + [ 4, 5, 6 ], | | 10 | + [ 7, 8, 9 ] | | 11 | +] | | 12 | +Output: [1, 2, 3, 6, 9, 8, 7, 4, 5] | | 13 | +
| | 14 | +Input: | | 15 | +[ | | 16 | + [1, 2, 3, 4], | | 17 | + [5, 6, 7, 8], | | 18 | + [9, 10, 11, 12] | | 19 | +] | | 20 | +Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] | | 21 | +
| | 22 | +========================================= | | 23 | +Simulate spiral moving, start from (0,0) and when a border is reached change the X or Y direction. | | 24 | + Time Complexity: O(N*M) | | 25 | + Space Complexity: O(N*M) | | 26 | +''' | | 27 | + | | 28 | + | | 29 | +############ | | 30 | +# Solution # | | 31 | +############ | | 32 | + | | 33 | +def spiral_matrix(matrix): | | 34 | + n = len(matrix) | | 35 | + if n == 0: | | 36 | + return [] | | 37 | + | | 38 | + m = len(matrix[0]) | | 39 | + if m == 0: | | 40 | + return [] | | 41 | + | | 42 | + total = n * m | | 43 | + res = [] | | 44 | + | | 45 | + n -= 1 | | 46 | + xDir, yDir = 1, 1 | | 47 | + x, y = 0, -1 | | 48 | + | | 49 | + while len(res) < total: | | 50 | + for i in range(m): | | 51 | + y += yDir | | 52 | + res.append(matrix[x][y]) | | 53 | + m -= 1 # decrease horizontal moving steps | | 54 | + yDir *= -1 # change the Y direction | | 55 | + | | 56 | + for i in range(n): | | 57 | + x += xDir | | 58 | + res.append(matrix[x][y]) | | 59 | + n -= 1 # decrease vertical moving steps | | 60 | + xDir *= -1 # change the Y direction | | 61 | + | | 62 | + return res | | 63 | + | | 64 | + | | 65 | +########### | | 66 | +# Testing # | | 67 | +########### | | 68 | + | | 69 | +# Test 1 | | 70 | +# Correct result => [1, 2, 3, 6, 9, 8, 7, 4, 5] | | 71 | +print(spiral_matrix([[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]])) | | 72 | + | | 73 | +# Test 2 | | 74 | +# Correct result => [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] | | 75 | +print(spiral_matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])) |
|