Lv.0 - ์•ˆ์ „์ง€๋Œ€_01.09๐Ÿฅฒ

์†ก์ฒ ์ง„ยท2023๋…„ 1์›” 8์ผ
0

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
48/117
post-custom-banner

๋ฌธ์ œ ์„ค๋ช…

๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ง€๋ขฐ์— ์ธ์ ‘ํ•œ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  ์นธ์„ ๋ชจ๋‘ ์œ„ํ—˜์ง€์—ญ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.
image.png
์ง€๋ขฐ๋Š” 2์ฐจ์› ๋ฐฐ์—ด board์— 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๊ณ  board์—๋Š” ์ง€๋ขฐ๊ฐ€ ๋งค์„ค ๋œ ์ง€์—ญ 1๊ณผ, ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์ง€๋ขฐ๊ฐ€ ๋งค์„ค๋œ ์ง€์—ญ์˜ ์ง€๋„ board๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์•ˆ์ „ํ•œ ์ง€์—ญ์˜ ์นธ ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
board๋Š” n * n ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
1 โ‰ค n โ‰ค 100
์ง€๋ขฐ๋Š” 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
board์—๋Š” ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ 1๊ณผ ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

boardresult
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]16
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0]]13
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]0

๋‚˜์˜ ์ฝ”๋“œ

function solution(board) {
    const max = board.length
    let arr = makeZero(max)
    let one = []
    for(let i=0; i<max; i++){
        for(let j=0; j<max; j++){
            if(board[i][j] === 1) one.push([i+1,j+1])
        }
    }
    for(let [x,y] of one){
        for(let i=x-1; i<=x+1; i++){
            for(let j=y-1; j<=y+1; j++){
                arr[i][j] = 1
            }
        }
    }
    let count = 0
    for(let i=1; i<=max; i++){
        for(let j=1; j<=max; j++){
            if(arr[i][j] === "0") count++
        }
    }
    return count
}
function makeZero(num){
    num += 2
    const five = "0".repeat(num)
    let arr = []
    for(let i=0; i<num; i++){
        arr.push(five)
    }
    return arr.map(el=>[...el])
} 

ํ’€์ด

  1. 2์ฐจ์› ๋ฐฐ์—ด board์˜ ๊ธธ์ด๋ณด๋‹ค 2๋งŒํผ ํฐ 2์ฐจ์› ๋ฐฐ์—ด arr์„ ์ƒ์„ฑํ•œ๋‹ค
    ๐Ÿ‘‰ ์ƒ์„ฑํ•˜๋Š” ์ด์œ : ์ƒํ•˜์ขŒ์šฐ๋Œ€๊ฐ์„ ์— ์œ„ํ—˜์ง€์—ญ์„ ํ‘œ์‹œํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
    ๊ทผ๋ฐ ์˜ˆ๋ฅผ ๋“ค์–ด, board[0][0] = 1์ด๋ฉด ์ˆœํšŒํ•  ๋•Œ board[-1][-1]๋ถ€ํ„ฐ ํ•˜๊ฒŒ๋˜๋Š”๋ฐ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์œ„์น˜์— ์ ‘๊ทผํ•˜๋ คํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œจ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ!
function solution(board) {
    const max = board.length
    let arr = makeZero(max)
	// ์ดํ•˜ 2 ~ 4.
}
function makeZero(num){
    num += 2
    const five = "0".repeat(num)
    let arr = []
    for(let i=0; i<num; i++){
        arr.push(five)
    }
    return arr.map(el=>[...el])
} 

  1. board์˜ 1์˜ ์œ„์น˜๋ฅผ arr์— ๋งž๊ฒŒ ์กฐ์ ˆํ•œ ์œ„์น˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด one์„ ์ƒ์„ฑํ•œ๋‹ค
	let one = []
    for(let i=0; i<max; i++){
        for(let j=0; j<max; j++){
            if(board[i][j] === 1) one.push([i+1,j+1])
        }
    }

  1. arr์— ๋Œ€ํ•˜์—ฌ one์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ๋Œ€๊ฐ์„ ์˜ ์œ„์น˜์— 1์„ ํ• ๋‹นํ•œ๋‹ค
    for(let [x,y] of one){
        for(let i=x-1; i<=x+1; i++){
            for(let j=y-1; j<=y+1; j++){
                arr[i][j] = 1
            }
        }
    }

  1. arr์— ๋Œ€ํ•˜์—ฌ ๊ฐ’์ด "0"์ด๋ฉด count๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    ์ˆœํšŒ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด count๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    let count = 0
    for(let i=1; i<=max; i++){
        for(let j=1; j<=max; j++){
            if(arr[i][j] === "0") count++
        }
    }
    return count

์ด๊ฒŒ 3์ ์งœ๋ฆฌ๋ผ๋‹ˆ ๊ฑฐ์ง“๋ง์ด์ง€..?

์˜๋ฌธโญ๏ธ

2์ฐจ์› ๋ฐฐ์—ด board์˜ ๊ธธ์ด๋ณด๋‹ค 2๋งŒํผ ํฐ 2์ฐจ์› ๋ฐฐ์—ด arr์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ํ•œ~~~์ฐธ์ด๋‚˜ ๊ฑธ๋ ค๋ฒ„๋ ธ๋‹ค
๊ทธ ์ด์œ ๋Š” ์ฒ˜์Œ์—” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋Š”๋ฐ

function makeZero(num){
    let arr = []
    let zeros = []
    for(let i=0; i<num+2; i++){
        zeros.push(0)
        arr.push(zeros)
    }
    return arr
} 

์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ. ์™œ์ผ๊นŒ?

์œ„์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋„์›€์„ ๋ฐ›์•„;

let arr1 = [[0,0,0],[0,0,0],[0,0,0]]
  let arr2 = []
  let zeros = []
  for(let i=0; i<3; i++){
    zeros.push(0)
    arr2.push(zeros)        
  }
  console.log("arr1 ์ „: ", arr1)
  arr1[2][2] = 1
  console.log("arr1 ํ›„: ", arr1)    

  console.log("arr2 ์ „: ", arr2)
  arr2[2][2] = 1
  console.log("arr2 ํ›„: ", arr2)      
}

  • [0, 0, 0]์œผ๋กœ ์™„์„ฑ์ด ๋˜์–ด์žˆ๋Š” ๋ฐฐ์—ดzeros๋ฅผ arr1์—๊ฒŒ pushํ•œ๋‹ค๋Š” ๊ฒƒ์€ arr1 = [zeros, zeros, zeros] ์ฒ˜๋Ÿผ ๋œ๋‹ค๋Š” ๊ฒƒ.
  • arr2[2][2] = 1 ์ด๊ฒƒ์€ zeros[2] = 1๊ณผ ๊ฐ™์€์˜๋ฏธ
  • arr2์˜ 3๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ๊ฐ’์„ ์žฌํ• ๋‹น ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ zeros์˜ 3๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ

๋ผ๊ณ  ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค

profile
๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ธฐ๋กํ•˜๋ฉฐ ํ•™์Šตํ•˜๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€