๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ–ฅ CS/Algorithm

[Algorithm/Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Lv.3) - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ

์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค :)

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ ๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•ด ๊ตฌ๊ฐ„์˜ ๋ณ€ํ™”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋˜์–ด ๋ฌธ์ œ ํ’€์ด์™€ ํ•จ๊ป˜ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Lv.3) - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ> ๋ฌธ์ œ ํ’€์ด๋ฅผ ํ†ตํ•ด ๋ˆ„์ ํ•ฉ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ ๋งํฌ

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๋ฌธ์ œ ํ’€์ด

์ฒ˜์Œ ์ ‘๊ทผ

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ ๊ตฐ์ด ํŠน์ • ๊ฑด๋ฌผ๋“ค์„ ๊ณต๊ฒฉํ•˜๊ณ  ์•„๊ตฐ์ด ํšŒ๋ณตํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ ์ตœ์ข…์ ์œผ๋กœ ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 2์ฐจ์› ๋ฐฐ์—ด ์š”์†Œ์˜ ๊ฐ’์˜ ๋ณ€ํ™”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ด€๊ฑด์ž…๋‹ˆ๋‹ค! (ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ์กด์žฌ) ์ €๋Š” ํšจ์œจ์ ์ธ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์•„์ด๋””์–ด๊ฐ€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์ดํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๋‹น์—ฐํžˆ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ..๐Ÿฅฒ

์™„์ „ ํƒ์ƒ‰ ํ’€์ด (์ •ํ™•์„ฑ 100%, ํšจ์œจ์„ฑ 0%)

import Foundation

func solution(_ board:[[Int]], _ skill:[[Int]]) -> Int {
    var board = board
    
    func modifyBoard(_ type: Int, _ r1: Int, _ c1: Int, _ r2: Int, _ c2: Int, _ degree: Int) {
        for i in r1...r2 {
            for j in c1...c2 {
                board[i][j] = type == 1 ? (board[i][j] - degree) : (board[i][j] + degree)
            }
        }
    }
    
    for arr in skill {
        modifyBoard(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5])
    }
    
    return board.flatMap{$0}.filter{$0 > 0}.count
}

 

ํšจ์œจ์„ฑ ๊ฐœ์„ ์„ ์œ„ํ•ด ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด์•˜๋Š”๋ฐ์š”, ํ•ด๋‹น ๋ฌธ์ œ๊ฐ€ ์นด์นด์˜ค ๊ธฐ์ถœ๋ฌธ์ œ๋ผ ๊ณต์‹ ํ…Œํฌ ๋ธ”๋กœ๊ทธ์— ํ’€์ด์— ์ด์šฉํ•ด์•ผ ํ•˜๋Š” ์•„์ด๋””์–ด์— ๋Œ€ํ•ด ๊ต‰์žฅํžˆ ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์žˆ๋”๋ผ๊ตฌ์š”! ๊ณต์‹ ํ•ด์„ค์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•ด ๊ตฌ๊ฐ„์˜ ๋ณ€ํ™”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ด๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ณ , ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ Swift๋กœ ํ’€์ดํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

https://tech.kakao.com/2022/01/14/2022-kakao-recruitment-round-1/#%EB%AC%B8%EC%A0%9C-6-%ED%8C%8C%EA%B4%B4%EB%90%98%EC%A7%80-%EC%95%8A%EC%9D%80-%EA%B1%B4%EB%AC%BC

 

2022 ์นด์นด์˜ค ์‹ ์ž… ๊ณต์ฑ„ 1์ฐจ ์˜จ๋ผ์ธ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ for Tech developers ๋ฌธ์ œํ•ด์„ค

์ง€๋‚œ 2021๋…„ 9์›” 11์ผ ํ† ์š”์ผ ์˜คํ›„ 2์‹œ๋ถ€ํ„ฐ 7์‹œ๊นŒ์ง€ 5์‹œ๊ฐ„ ๋™์•ˆ 2022 KAKAO BLIND RECRUITMENT 1์ฐจ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๊ฐ€ ์ง„ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์—๋Š” ์ด 7๊ฐœ์˜ ๋ฌธ์ œ๊ฐ€ ์ถœ์ œ๋˜์—ˆ์œผ๋ฉฐ, ๊ฐœ๋ฐœ ์–ธ์–ด๋Š” C++, Java, JavaScript, K

tech.kakao.com

๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

์™„์ „ํƒ์ƒ‰์œผ๋กœ 2์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฐ’์ด ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ์š”์†Œ์— ์ ‘๊ทผํ•ด ์ƒˆ๋กœ์šด ๊ฐ’์„ ๊ณ„์†ํ•ด์„œ ์ €์žฅํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ•œ๋‹ค๋ฉด, ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋ชจ๋‘ ๋งž์ถœ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(N * M * K)๊ฐ€ ๋˜์–ด ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ๊ฐœ์„ ํ•˜๋ ค๋ฉด ๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•ด ๊ตฌ๊ฐ„์˜ ๋ณ€ํ™”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด์— ์ ์šฉํ•˜๊ธฐ ์ „์—, ๋จผ์ € 1์ฐจ์› ๋ฐฐ์—ด์˜ ์˜ˆ์‹œ๋กœ ๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ดํ•ดํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์˜ˆ๋ฅผ ๋“ค์–ด, [1, 2, 4, 8, 9]์˜ ๋ฐฐ์—ด์ด ์žˆ๊ณ , 0๋ฒˆ์งธ๋ถ€ํ„ฐ 3๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€ 2๋งŒํผ ๋นผ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐฐ์—ด์„ [-1, 0, 2, 6, 9]๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด 0~3๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•ด -2๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒ ์ฃ ! but ์ด ๋ฐฉ๋ฒ•์€ O(M)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ๋ˆ„์ ํ•ฉ์„ ์ด์šฉํ•œ๋‹ค๋ฉด O(M)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ O(1)๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! (wow..)

 

ํ•ด๋‹น ์˜ˆ์‹œ์— ์ ์šฉํ•ด๋ด…์‹œ๋‹ค. ๋จผ์ €, ๋ณ€ํ™”๋ฅผ ์ฃผ๊ณ ์ž ํ•˜๋Š” ๋ฐฐ์—ด๊ณผ ๋˜‘๊ฐ™์€ ํฌ๊ธฐ์ธ๋ฐ ๋ชจ๋“  ์š”์†Œ๊ฐ€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด([0, 0, 0, 0, 0])์„ ๋จผ์ € ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ, ํŠน์ • ๊ตฌ๊ฐ„์— c๋งŒํผ์˜ ๋ณ€ํ™”๋ฅผ ์ฃผ๊ธธ ์›ํ•œ๋‹ค๋ฉด ์ดˆ๊ธฐํ™”๋œ ๋ฐฐ์—ด์˜ ๊ตฌ๊ฐ„์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ์— +c, ๊ตฌ๊ฐ„์˜ ๋งˆ์ง€๋ง‰+1๋ฒˆ์งธ ์›์†Œ์— -c๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. (์ฆ‰, 1์ฐจ์› ๋ฐฐ์—ด์˜ a๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ b๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€ c๋งŒํผ์˜ ๋ณ€ํ™”๋ฅผ ์ฃผ๊ฒ ๋‹ค๊ณ  ํ•˜๋ฉด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์˜ a๋ฒˆ์งธ ์›์†Œ์— c๋ฅผ ๋”ํ•˜๊ณ  b+1๋ฒˆ์งธ ์›์†Œ์— c๋ฅผ ๋นผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.) ํ•ด๋‹น ์˜ˆ์‹œ์—์„œ๋Š” 0~3๋ฒˆ์งธ ์›์†Œ์— -2๋งŒํผ์˜ ๋ณ€ํ™”๋ฅผ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0๋ฒˆ์งธ ์›์†Œ์— -2, 4๋ฒˆ์งธ ์›์†Œ์— +2๋ฅผ ํ•ด์„œ ๋ฐฐ์—ด์„ [-2, 0, 0, 0, 2]๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด ๋ฐฐ์—ด์„ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ˆ„์ ํ•ฉํ•˜๋ฉด [-2, -2, -2, -2, 0] ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์›๋ž˜ ๋ฐฐ์—ด๊ณผ ๋”ํ•ด์ฃผ๋ฉด [-1, 0, 2, 6, 9]๋ผ๋Š” ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด O(N * M * K)์˜ ๋ณต์žก๋„๋ฅผ O(N * K)๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๋˜ํ•œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด ์•„์ด๋””์–ด๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ™•์žฅํ•œ๋‹ค๋ฉด O(N * K)๋ฅผ O(1)๊นŒ์ง€ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋ฒˆ์—๋Š” 2์ฐจ์› ๋ฐฐ์—ด์˜ (0,0)๋ถ€ํ„ฐ (2,2)๊นŒ์ง€ n๋งŒํผ ๋ณ€ํ™”์‹œํ‚ค๋Š” ๊ฒฝ์šฐ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ €, ์œ„์—์„œ 1์ฐจ์› ๋ฐฐ์—ด์— ๋ณ€ํ™”๋ฅผ ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ 2์ฐจ์› ๋ฐฐ์—ด์ด์ง€๋งŒ ํ–‰(๊ฐ€๋กœ)๋งŒ ๋”ฐ๋กœ ๋ด์„œ ์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ์ ์šฉ์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฐ์—ด์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. (๊ฐ ํ–‰์˜ 0๋ฒˆ์งธ ์›์†Œ์— +n, 3๋ฒˆ์งธ ์›์†Œ์— -n)

 

n 0 0 -n
n 0 0 -n
n 0 0 -n

 

์ด์ œ, ์—ด(์„ธ๋กœ)๋งŒ ๋”ฐ๋กœ ๋ด…์‹œ๋‹ค. 2์ฐจ์› ๋ฐฐ์—ด์ด์ง€๋งŒ ์—ด๋งŒ ๋”ฐ๋กœ ๋ณธ๋‹ค๋ฉด ์ฒซ๋ฒˆ์งธ ์—ด์—๋งŒ 0~2๋ฒˆ์งธ ์›์†Œ์— n๋งŒํผ์˜ ๋ณ€ํ™”, 3๋ฒˆ์งธ ์—ด์—๋งŒ 0~2๋ฒˆ์งธ ์›์†Œ์— -n๋งŒํผ์˜ ๋ณ€ํ™”๋ฅผ ์ค€ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ! ์—ฌ๊ธฐ์— ์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ๋˜ ์ ์šฉ์‹œํ‚ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (0๋ฒˆ์งธ ์—ด์˜ 0๋ฒˆ์งธ ์›์†Œ์— +n, 3๋ฒˆ์งธ ์›์†Œ์— -n. 3๋ฒˆ์งธ ์—ด์˜ 0๋ฒˆ์งธ ์›์†Œ์— -n, 3๋ฒˆ์งธ ์›์†Œ์— -(-n))

 

n 0 0 -n
0 0 0 0
0 0 0 0
-n 0 0 n

 

์ฆ‰, 2์ฐจ์› ๋ฐฐ์—ด์—์„œ (x1, y1)๋ถ€ํ„ฐ (x2, y2)๊นŒ์ง€ n๋งŒํผ์˜ ๋ณ€ํ™”๋Š” (x1, y1)์— +n, (x1, y2+1)์— -n, (x2+1, y1)์— -n, (x2+1, y2+1)์— +n์„ ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

์œ„ ๋ฐฐ์—ด์„ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ˆ„์ ํ•ฉ ํ•œ ๋’ค, ์œ„์—์„œ ์•„๋ž˜๋กœ ๋ˆ„์ ํ•ฉํ•˜๋ฉด ์ฒ˜์Œ์— ์˜๋„ํ•œ (0,0)๋ถ€ํ„ฐ (2,2)๊นŒ์ง€ n๋งŒํผ ๋ณ€ํ™”์‹œํ‚ค๋Š” ๋ฐฐ์—ด์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์™ผ -> ์˜ค, ์œ„ -> ์•„๋ž˜ ์ˆœ์„œ ๋ฐ”๋€Œ์–ด๋„ ๊ฒฐ๊ณผ ๊ฐ’ ๋™์ผ)

 

n n n 0
n n n 0
n n n 0
0 0 0 0

 

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ skill์˜ ํ•œ ์›์†Œ๋ฅผ O(1)๋งŒ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ๋ฐฉ๋ฒ•์œผ๋กœ K๊ฐœ์˜ skill์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š”๋ฐ O(K)๊ฐ€ ๊ฑธ๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข… ๊ฒฐ๊ณผ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์›๋ž˜ ๋ฐฐ์—ด๊ณผ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์„ ๋”ํ•˜๋Š” ๊ณผ์ •์€ O(N * M)๊ฐ€ ๊ฑธ๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ O(K + N * M)์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ Swift ์ฝ”๋“œ๋กœ ํ’€์ดํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Swift ํ’€์ด ์ฝ”๋“œ

import Foundation

func solution(_ board:[[Int]], _ skill:[[Int]]) -> Int {
    let sumBoard = prefixSum(board, skill)
    let newBoard = makeNewBoard(board, sumBoard)

    return newBoard.flatMap{$0}.filter{$0 > 0}.count
}

// ์Šคํ‚ฌ์„ ์ด์šฉํ•œ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ
func prefixSum(_ board: [[Int]], _ skills: [[Int]]) -> [[Int]] {
    let (n, m) = (board.count, board[0].count)

    // ๋ชจ๋“  ํ–‰,์—ด 0์œผ๋กœ ์ดˆ๊ธฐํ™”
    var prefixBoard: [[Int]] = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)

    for skill in skills {
        let type = skill.first!
        var degree = skill.last!

        let (si, sj) = (skill[1], skill[2]) // ์‹œ์ž‘์ 
        let (ei, ej) = (skill[3], skill[4]) // ๋์ 

        if type == 1 {
            degree *= -1
        }

        prefixBoard[si][sj] += degree // ์‹œ์ž‘์ ์— degree ์„ค์ •
        prefixBoard[si][ej + 1] -= degree // ์‹œ์ž‘ํ–‰์˜ ๋์ +1์— -degree๊ฐ’ ์„ค์ •
        prefixBoard[ei + 1][sj] -= degree // ์‹œ์ž‘์—ด์˜์˜ ๋์ +1์— -degree๊ฐ’ ์„ค์ •
        prefixBoard[ei + 1][ej + 1] += degree // ํ–‰์—ด ๋์ +1์— degree ์„ค์ •
    }

    // ์™ผ -> ์˜ค ๋ˆ„์ ํ•ฉ
    for i in 0 ..< n {
        for j in 1 ..< m {
            prefixBoard[i][j] += prefixBoard[i][j - 1]
        }
    }
    // ์œ„ -> ์•„๋ž˜ ๋ˆ„์ ํ•ฉ
    for j in 0 ..< m {
        for i in 1 ..< n {
            prefixBoard[i][j] += prefixBoard[i - 1][j]
        }
    }

    return prefixBoard
}

// ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์„ ์›๋ž˜ ๋ฐฐ์—ด๊ณผ ๋”ํ•˜๋Š” ๋ฉ”์„œ๋“œ
func makeNewBoard(_ board: [[Int]], _ sumBoard: [[Int]]) -> [[Int]] {
    let (n, m) = (board.count, board[0].count)
    var newBoard = Array(repeating: Array(repeating: 0, count: m), count: n)

    for i in 0..<n {
        for j in 0..<m {
            newBoard[i][j] = board[i][j] + sumBoard[i][j]
        }
    }

    return newBoard
}

 

2์ฐจ์› ๋ฐฐ์—ด์˜ ์š”์†Œ์— ์—ฌ๋Ÿฌ๋ฒˆ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์—์„œ ์ด ๋ˆ„์ ํ•ฉ ์•„์ด๋””์–ด๋ฅผ ์ž˜ ์ดํ•ดํ•ด๋‘๊ณ  ์‘์šฉํ•˜๋ฉด ํ›จ์”ฌ ํšจ์œจ์ ์ธ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!


[์ฐธ๊ณ  ์ž๋ฃŒ]

 

2022 ์นด์นด์˜ค ์‹ ์ž… ๊ณต์ฑ„ 1์ฐจ ์˜จ๋ผ์ธ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ for Tech developers ๋ฌธ์ œํ•ด์„ค

์ง€๋‚œ 2021๋…„ 9์›” 11์ผ ํ† ์š”์ผ ์˜คํ›„ 2์‹œ๋ถ€ํ„ฐ 7์‹œ๊นŒ์ง€ 5์‹œ๊ฐ„ ๋™์•ˆ 2022 KAKAO BLIND RECRUITMENT 1์ฐจ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๊ฐ€ ์ง„ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์—๋Š” ์ด 7๊ฐœ์˜ ๋ฌธ์ œ๊ฐ€ ์ถœ์ œ๋˜์—ˆ์œผ๋ฉฐ, ๊ฐœ๋ฐœ ์–ธ์–ด๋Š” C++, Java, JavaScript, K

tech.kakao.com

 

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ (Swift)

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ (Swift) https://programmers.co.kr/learn/courses/30/lessons/92344 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ [[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]] [[1,0,0,3,4,4],[1,2,0,2,3,2],[2,1,0,3,1,2

trumanfromkorea.tistory.com