diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_35_rotate_image.py b/src/my_project/interviews/top_150_questions_round_22/ex_35_rotate_image.py new file mode 100644 index 00000000..a24bf5b0 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_35_rotate_image.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + + """ + :type matrix: List[List[int]] + :rtype: void Do not return anything, modify matrix in-place instead. + """ + + n = len(matrix) + + for i in range(n): + for j in range(i+1,n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for r in matrix: + r.reverse() + + return matrix \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_36_set_matrix_zeroes.py b/src/my_project/interviews/top_150_questions_round_22/ex_36_set_matrix_zeroes.py new file mode 100644 index 00000000..9185c767 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_36_set_matrix_zeroes.py @@ -0,0 +1,54 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + if not matrix or not matrix[0]: + return + + m, n = len(matrix), len(matrix[0]) + + # Flags to track if first row/column should be zeroed + first_row_zero = False + first_col_zero = False + + # Check if first row has any zeros + for j in range(n): + if matrix[0][j] == 0: + first_row_zero = True + break + + # Check if first column has any zeros + for i in range(m): + if matrix[i][0] == 0: + first_col_zero = True + break + + # Use first row and column as markers + # Check rest of matrix and mark first row/column + for i in range(1, m): + for j in range(1, n): + if matrix[i][j] == 0: + matrix[i][0] = 0 # Mark row + matrix[0][j] = 0 # Mark column + + # Set zeros based on markers (skip first row/column) + for i in range(1, m): + for j in range(1, n): + if matrix[i][0] == 0 or matrix[0][j] == 0: + matrix[i][j] = 0 + + # Handle first row + if first_row_zero: + for j in range(n): + matrix[0][j] = 0 + + # Handle first column + if first_col_zero: + for i in range(m): + matrix[i][0] = 0 + + return matrix \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_35_rotate_image_round_22.py b/tests/test_150_questions_round_22/test_35_rotate_image_round_22.py new file mode 100644 index 00000000..89a076fd --- /dev/null +++ b/tests/test_150_questions_round_22/test_35_rotate_image_round_22.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_35_rotate_image import Solution + +class RotateImageTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.rotate(matrix = [[1,2,3],[4,5,6],[7,8,9]]) + target = [[7,4,1],[8,5,2],[9,6,3]] + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.rotate(matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]) + target = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] + self.assertEqual(output, target) + diff --git a/tests/test_150_questions_round_22/test_36_set_matrix_zeroes_round_22.py b/tests/test_150_questions_round_22/test_36_set_matrix_zeroes_round_22.py new file mode 100644 index 00000000..341e1080 --- /dev/null +++ b/tests/test_150_questions_round_22/test_36_set_matrix_zeroes_round_22.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_36_set_matrix_zeroes import Solution + +class MatrixZeroesTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.setZeroes(matrix = [[1,1,1],[1,0,1],[1,1,1]]) + target = [[1,0,1],[0,0,0],[1,0,1]] + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.setZeroes(matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]) + target = [[0,0,0,0],[0,4,5,0],[0,3,1,0]] + self.assertEqual(output, target) +