Data structure refresher: Day 5

Table of contents

1. Effective Sudoku

 1. One traversal (hash table)

Complexity analysis

2. Return the matrix to zero

1. Use tag array

Ideas and algorithms

Complexity analysis

1. Effective Sudoku

36. Valid Sudoku - LeetCode https://leetcode.cn/problems/valid-sudoku/

 1. One traversal (hash table)

A valid Sudoku satisfies the following three conditions:

The same number can only appear once in each line;

The same number can only appear once in each column;

The same number can only appear once in each small nine-square grid.

You can use a hash table to record the number of times each number appears in each row, each column and each small grid. You only need to traverse the Sudoku once, update the count in the hash table during the traversal, and determine whether the conditions for a valid Sudoku are met.

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int rows[9][9];
        int columns[9][9];
        int subboxes[3][3][9];
        
        memset(rows,0,sizeof(rows));
        memset(columns,0,sizeof(columns));
        memset(subboxes,0,sizeof(subboxes));
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.') {
                    int index = c - '0' - 1;
                    rows[i][index]++;
                    columns[j][index]++;
                    subboxes[i / 3][j / 3][index]++;
                    if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
};

Complexity analysis

Time complexity: O(1). There are 81 cells in Sudoku, and each cell only needs to be traversed once.

Space complexity: O(1). Since the size of the Sudoku is fixed, the space of the hash table is also fixed.

2. Return the matrix to zero

73. Set matrix zeroes - LeetCode https://leetcode.cn/problems/set-matrix-zeroes/

1. Use tag array

Ideas and algorithms

We can use two marker arrays to record whether zero appears in each row and column.

Specifically, we first traverse the array once. If an element is 0, then set the position of the mark array corresponding to the row and column of the element to true. Finally, we traverse the array again and update the original array with the tag array.

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> row(m), col(n);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!matrix[i][j]) {
                    row[i] = col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
};

Complexity analysis

Time complexity: O(mn), where m is the number of rows of the matrix and n is the number of columns of the matrix. We only need to traverse the matrix twice at most.

Space complexity: O(m+n), where m is the number of rows of the matrix and n is the number of columns of the matrix. We need to record whether zero occurs in each row or column separately.

Guess you like

Origin blog.csdn.net/m0_63309778/article/details/126595000