https://leetcode.com/problems/sudoku-solver/


Time Limit Exceeded


```python
from pprint import pprint 
import numpy
from functools import reduce

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        #2022/11/30 07:02
        new_board = numpy.array(board.copy())

        number_1_to_9 = numpy.arange(1, 10)
        convert_number_to_string = numpy.vectorize(lambda x: str(x))
        number_1_to_9 = convert_number_to_string(number_1_to_9)

        def get_sub_matrix_of_a_cell(matrix, y, x):
            x_start_index = (x // 3) * 3
            x_end_index = x_start_index + 3
            y_start_index = (y // 3) * 3
            y_end_index = y_start_index + 3
            sub_matrix = matrix[y_start_index:y_end_index, x_start_index:x_end_index]
            #pprint(sub_matrix)
            return sub_matrix
        
        def get_row_of_a_cell(matrix, y, x):
            return matrix[y, :]
        
        def get_column_of_a_cell(matrix, y, x):
            return matrix[:, x]
        
        def get_missing_number(a_numpy_matrix):
            a_1d_numpy_array = a_numpy_matrix.flatten()
            #print(number_1_to_9, a_1d_numpy_array)
            return numpy.setdiff1d(number_1_to_9, a_1d_numpy_array)
        
        def get_possible_numbers(matrix, index_y, index_x):
            a_row = get_row_of_a_cell(matrix, index_y, index_x)
            missing_number1 = get_missing_number(a_row)

            a_column = get_column_of_a_cell(matrix, index_y, index_x)
            missing_number2 = get_missing_number(a_column)

            a_sub_matrix = get_sub_matrix_of_a_cell(matrix, index_y, index_x)
            missing_number3 = get_missing_number(a_sub_matrix)

            result = reduce(numpy.intersect1d, (missing_number1, missing_number2, missing_number3))
            return result
        
        def modify_original_matrix(matrix):
            for index_y, index_x in numpy.ndindex(new_board.shape):
                old_value = new_board[index_y, index_x]
                new_value = matrix[index_y, index_x] 
                if old_value == ".":
                    board[index_y][index_x] = new_value

        def go_through(a_board, position_list):
            if len(position_list) == 0:
                # do some modification for the original matrix
                print(a_board)
                modify_original_matrix(a_board)
                return
            else:
                y, x = position_list[0]
                possible_numbers_for_this_cell = get_possible_numbers(a_board, y, x)
                if (possible_numbers_for_this_cell.size == 0):
                    # not possible to go on, drop it
                    pass
                else:
                    for one_number in possible_numbers_for_this_cell:
                        temp_board = numpy.copy(a_board)
                        temp_board[y][x] = one_number
                        go_through(temp_board, position_list[1:])

        positions_that_need_to_have_a_check = []
        for index_y, index_x in numpy.ndindex(new_board.shape):
            value = new_board[index_y, index_x] 
            if value == ".":
                positions_that_need_to_have_a_check.append([index_y, index_x])
        
        go_through(numpy.copy(new_board), positions_that_need_to_have_a_check)

        #2022/11/30 09:13
```


___


What you think is what you get.

For this problem, it is not that hard if you do it step by step by using recursive function.
