Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
													Tags
													
											
												
												- 취미
- 게임
- 롱라이플
- 제프딕슨
- 포켓몬
- 노노그램
- 코테
- 누룽지소금빵
- 발더스게이트
- 지리데칼
- 미앤아이
- DIY
- 발더스모드
- 건담헤드
- 바질토마토뭐시기
- javascript
- 지쿠악스
- LeetCode
- 맛집
- 밥무하마드
- 눈알빠지겠네
- 가챠
- 메탈퍼즐
- 건담
- 프라모델
- 30mf
- 우리시대의역설
- 유루건
- 블라인드박스
- 코딩테스트
													Archives
													
											
												
												- Today
- Total
.Zzumbong
[lettCode/JS] 36. Valid Sudoku 본문
문제 설명
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9without repetition.
- Each column must contain the digits 1-9without repetition.
- Each of the nine 3 x 3sub-boxes of the grid must contain the digits1-9without repetition.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
스도쿠가 있는데, rules에서 정한 조건으로만 틀렸는지 잘 풀고있는지 체크하는 로직이다.
board[row][0] -> 1~9 까지 중복이 없는지
board[0][col] -> 1~9 까지 중복이 없는지
3x3 칸에 1~9까지 중복이 없는지
3개의 조건으로 체크를 한다.
스도쿠 특성상 이렇게 체크하면 풀 수 없는 "false" 인 경우도 나온다.
입출력 예
Example 1:

Input: board = 
[["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"]]
Output: trueExample 2:
Input: board = 
[["8","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"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. 
Since there are two 8's in the top left 3x3 sub-box, it is invalid.Constraints
- board.length == 9
- board[i].length == 9
- board[i][j]is a digit- 1-9or- '.'.
내 솔루션
- row 1줄 씩 체크, col 1줄 씩 체크 로직에 3x3 칸마다 따로 nine 이라는 Array에 추가하여 검사하는 방식으로 풀었다.
- 스도쿠 보드의 순회가 많은 것보다 isUnique()가 반복이 1번더 있기 때문에 느렸던 것으로 추측하고 있다.
- 해결법은 isUnique()를 최적화 하거나cols, rows, nine을Set()으로 초기화해서has()함수를 써서push()전에 체크하여false를return해버리면 조금 더 빨라질 것 같다.
var isValidSudoku = function(board) {
  const isUnique = (nums) => {
    const newNums = nums.filter(n => n !== '.')
    const unique = new Set(newNums);
    return newNums.length === unique.size;
  }
  const nine = Array.from(Array(9), e => Array());
  for(let row = 0; row < board.length; row++) {
    if(!isUnique(board[row])) return false;
    const cols = [];
    for(let col = 0; col < board[0].length; col++) {
      const idx = Math.floor(col / 3) + Math.floor(row / 3) * 3;
      cols.push(board[col][row])
      nine[idx].push(board[col][row])
      if(nine[idx].length === board.length && !isUnique(nine[idx])) return false;
    }
    if(!isUnique(cols)) return false;
  }  
  return true;
};
감상평

- 생각보다 오래 걸렸다. nested array Declare하는 것이 헷갈려서 ㅋㅋ
'coding test > leetCode' 카테고리의 다른 글
| [leetCode/JS] 2047. Number of Valid Words in a Sentence (1) | 2022.11.24 | 
|---|---|
| [leetCode/JS] 9. Palindrome Number (1) | 2022.11.23 | 
| [leetCode/JS] 9. Palindrome Number (0) | 2022.11.23 | 
| [leetCode/JS] 587. Erect the Fence (1) | 2022.11.23 | 
| [leetCode/JS] 696. Count Binary Substrings (2) | 2022.11.23 | 
			  Comments