-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem54.js
59 lines (51 loc) · 1.49 KB
/
Problem54.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Problem 54
//
// This problem was asked by Dropbox.
//
// Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with digits.
// The objective is to fill the grid with the constraint that every row, column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9.
//
// Implement an efficient sudoku solver.
//
// https://leetcode.com/problems/sudoku-solver/description/
//
// O(9 ^ M) Time complexity
// O(M) Space complixity
// M is the number of empty squares in the sudoku board
/**
* Soduku Solver
* @param {number[][]} board
*/
function sudokuSolve(board) {
solve(board);
}
function solve(board) {
for (let r = 0; r < board.length; r++) {
for (let c = 0; c < board[r].length; c++) {
if (board[r][c] !== '.') continue;
for (let i = 1; i <= 9; i++) {
const num = String(i);
if (isValid(board, r, c, num)) {
board[r][c] = num;
if (solve(board)) return true;
board[r][c] = '.';
}
}
return false;
}
}
return true;
}
function isValid(board, r, c, num) {
const regionRow = 3 * Math.floor(r / 3);
const regionCol = 3 * Math.floor(c / 3);
for (let i = 0; i < 9; i++) {
if (board[i][c] === num) return false; // check row
if (board[r][i] === num) return false; // check col
const squareRow = regionRow + Math.floor(i / 3);
const squareCol = regionCol + (i % 3);
if (board[squareRow][squareCol] === num) return false;
}
return true;
}
export default sudokuSolve;