https://school.programmers.co.kr/learn/courses/30/lessons/68936?language=swift
1. Python 풀이
def solution(arr):
answer = [0,0]
def allSatisfy(row, col, length, value):
for i in range(row, row+length):
for j in range(col, col+length):
if arr[i][j] != value:
return False
return True
def dfs(row, col, length):
if allSatisfy(row, col, length, 1):
answer[1] += 1
elif allSatisfy(row, col, length, 0):
answer[0] += 1
else :
newLen = int(length/2)
for (i, j) in [(row, col),(row+newLen, col),(row, col+newLen),(row+newLen, col+newLen)]:
dfs(i, j, newLen)
dfs(0,0,len(arr))
return answer
2. Swift 풀이
import Foundation
func solution(_ arr:[[Int]]) -> [Int] {
var answer = [0,0]
func allSatisfy(_ row: Int, _ col: Int, _ length: Int, _ value: Int) -> Bool {
for i in row..<row+length {
for j in col..<col+length {
if arr[i][j] != value {
return false
}
}
}
return true
}
func dfs(_ row: Int, _ col: Int, _ length: Int) {
if allSatisfy(row,col,length,1) {
answer[1] += 1
}
else if allSatisfy(row, col, length, 0) {
answer[0] += 1
}
else {
for (i, j) in [(row, col), (row+length/2, col), (row, col+length/2), (row+length/2, col+length/2)] {
dfs(i, j, length/2)
}
}
}
dfs(0,0,arr.count)
return answer
}
[프로그래머스] 베스트앨범 (1) | 2024.01.24 |
---|---|
[프로그래머스] 다리를 지나는 트럭 (1) | 2024.01.23 |
[프로그래머스] 기지국 설치 (0) | 2024.01.18 |
[프로그래머스] 숫자 게임 (0) | 2024.01.17 |
[프로그래머스] 택배상자 (1) | 2024.01.16 |