본문 바로가기
CodeSignal/Interview Practice

[Arrays] sudoku2 - Apple, Uber

by 벤진[Benzene] 2020. 5. 29.

Problem:

Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with numbers in such a way that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid all contain all of the numbers from 1 to 9 one time.

Implement an algorithm that will check whether the given grid of numbers represents a valid Sudoku puzzle according to the layout rules described above. Note that the puzzle represented by grid does not have to be solvable.

 

Example

 

the output should be
sudoku2(grid) = false.

The given grid is not correct because there are two 1s in the second column. Each column, each row, and each 3 × 3 subgrid can only contain the numbers 1 through 9 one time.

 

-Summary-

1. 3개의 constraints들을 check 해줄 function 3개를 따로 만들어준다. (ValidRow, ValidCol, ValidBox)

2. ValidRow - 각 row들의 element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)

   ValidCol - 각 column 의 element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)

   ValidBox - 각 3X3의 박스안의  element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)

3. 모든 helper function 들을 돌고난후에 return 값이 false 가 없으면 valid 한 sudoku 이므로 True값 return.

이 문제 또한, Leetcode 에서 봤던 똑같은 문제가 나왔다. 각 Row/Col/3X3 box 를 해주는 validation helper function을 만들어 풀면 좀 더 쉽게 풀수 있다.

 

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

댓글