λ¬Έμ
νμ΄ κ³Όμ
μ€λμΏ
λ₯Ό ꡬννλ λ¬Έμ μ
λλ€.
λμ λͺ¨λ λ‘μ§μ ꡬννλ κ²μ μλκ³ μ±μμ§ μ«μλ€μ λν΄μ μ€λμΏ μ‘°κ±΄μ λ§μ‘±νλμ§ νλ¨νλ λ¬Έμ μ
λλ€.
λ¨Όμ 3x3
ν¬κΈ°μ μλΈ λ°μ€λ€μμ 19 μ¬μ΄μ μ«μλ€ μ€ μ€λ³΅μ΄ μ‘΄μ¬νλμ§ κ²μ¬ν©λλ€.9 μ¬μ΄μ μ«μ μ€λ³΅μ±μ κ²μ¬ν©λλ€.
μ΄ν κ° νκ³Ό μ΄μ μννλ©° λ§μ°¬κ°μ§λ‘ 1
μ½λ
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
const isSubboxRepetition = checkSubboxes(board);
if (!isSubboxRepetition) {
return false;
}
for (let row = 0; row < 9; row += 1) {
const cache = [];
for (let col = 0; col < 9; col += 1) {
if (board[row][col] === ".") {
continue;
}
if (cache.includes(board[row][col])) {
return false;
}
cache.push(board[row][col]);
}
}
for (let col = 0; col < 9; col += 1) {
const cache = [];
for (let row = 0; row < 9; row += 1) {
if (board[row][col] === ".") {
continue;
}
if (cache.includes(board[row][col])) {
return false;
}
cache.push(board[row][col]);
}
}
return true;
};
function checkSubboxes(board) {
const hashTable = new Map();
for (let row = 0; row < 9; row += 1) {
for (let col = 0; col < 9; col += 1) {
if (board[row][col] !== ".") {
const code = encode(row, col);
if (hashTable.has(code)) {
const digits = hashTable.get(code);
if (digits.includes(board[row][col])) {
return false;
}
digits.push(board[row][col]);
hashTable.set(code, digits);
} else {
hashTable.set(code, [board[row][col]]);
}
}
}
}
return true;
}
function encode(row, col) {
return `${~~(row / 3)}${~~(col / 3)}`;
}
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 229 - Majority Element II (Medium) (0) | 2022.02.22 |
---|---|
LeetCode 300 - Longest Increasing Subsequence (Medium) (0) | 2021.06.14 |
LeetCode 120 - Triangle (Medium) (0) | 2021.04.13 |
LeetCode 38 - Count and Say (Medium) (0) | 2021.04.13 |
LeetCode 199 - Binary Tree Right Side View (Medium) (0) | 2021.04.05 |
π¬ λκΈ