forked from Adespinoza/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_054.js
77 lines (68 loc) Β· 1.98 KB
/
problem_054.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// https://leetcode.com/problems/sudoku-solver/description/
// Algorithmic Paradigm: Backtracking
// 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 solve(board) {
calculate(board);
}
/**
* Calculates possible outcomes on board
* @param {number[][]} board
*/
function calculate(board) {
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[row].length; col++) {
if (board[row][col] !== '.') continue;
// Check possible layout
for(let i = 1; i <= 9; i++) {
const num = String(i);
// Check validity
if(isValid(board, row, col, num)) {
board[row][col] = num;
if(calculate(board)) return true;
board[row][col] = '.';
}
}
return false;
}
}
return true;
}
/**
* Checks if 3x3 subgrid contains all digits from 1 to 9
* @param {number[][]} board
* @param {number} row
* @param {number} col
* @param {number} num
*/
function isValid(board, row, col, num) {
let regionRow = 3 * Math.floor(row / 3);
let regionCol = 3 * Math.floor(col / 3);
for(let i = 0; i < 9; i++) {
if(board[i][col] === num) return false;
if(board[row][i] === num) return false;
let squareRow = regionRow + Math.floor(i / 3);
let squareCol = regionCol + (i % 3);
// Check before moving on
if(board[squareRow][squareCol] === num) return false;
}
return true;
}
const sudoku = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
solve(sudoku);
console.log(sudoku);