์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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
- TCA
- ๋ฐฑ์ค
- tableView
- rxcocoa
- BFS
- node.js
- realm
- MVVM
- RxSwift
- Xcode
- UIKit
- Lv2
- CollectionView
- Swfit
- SnapKit
- SwiftUI
- swift
- designpattern
- ios
- Flutter
- combine
- raywenderlich
- visionOS
- BOJ
- arkit
- ํจ์คํธ์บ ํผ์ค
- reactorkit
- Kuring
- XCTest
- ํ๋ก๊ทธ๋๋จธ์ค
Archives
- Today
- Total
lgvv98
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฟผ๋ ์์ถ ํ ๊ฐ์ ์ธ๊ธฐ ๋ณธ๋ฌธ
์ฝ๋ฉํ
์คํธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฟผ๋ ์์ถ ํ ๊ฐ์ ์ธ๊ธฐ
๐ฅ ์บ๋ฟ๋งจ 2022. 4. 13. 15:58ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฟผ๋ ์์ถ ํ ๊ฐ์ ์ธ๊ธฐ
โ ๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ ์ฌ๊ท๋ก ํด์ผํ๋ค๊ณ ์๊ฐ์ด ๋ค์์.
์์ ์ ํ๊ต ์์ ์์ ์๋ฃ๊ตฌ์กฐ ์๊ณ ๋ฆฌ์ฆ ์๊ฐ์ divide and conquer๋ก ๋ฌธ์ ๋ฅผ ํ์๋ ๊ธฐ์ต์ด ์๋๋ฐ, ๊ทธ๋์ ์ด๋ฅผ ํ์ฉํจ.
๊ทผ๋ฐ ๊ทธ๋๋ ์์ ๋ฌธ์ ์์ ํฐ ๋ฌธ์ ๋ก ๊ฐ์๊ณ , ์ด๋ฒ์๋ ํฐ ๋ฌธ์ ์์ ์์ ๋ฌธ์ ๋ก ๋ด๋ ค๊ฐ์ผ ํ์.
โ ์ฝ๋
// https://programmers.co.kr/learn/courses/30/lessons/68936
import Foundation
struct p68936 {
static func run() {
print(p68936.solution([[1,1,0,0],[1,0,0,0],[1,0,0,1],[1,1,1,1]])) // [4,9]
}
static var zeroCount = 0
static var oneCount = 0
static func solution(_ arr:[[Int]]) -> [Int] {
// ์ฌ๊ท ์ฐ๋ฉด ๊ธ๋ฐฉ ํ๊ฒ ๋ค! -> divide and conquer ํด์ผํ๋ค.
reculsive(arr: arr, row: 0, col: 0, n: arr.count)
return [zeroCount, oneCount]
}
static func reculsive(arr: [[Int]], row: Int, col: Int, n: Int) {
let point = arr[row][col] // ์์ํ๋ ์ง์
for i in row..<row + n {
for j in col..<col + n {
if point != arr[i][j] {
reculsive(arr: arr, row: row, col: col, n: n/2)
reculsive(arr: arr, row: row, col: col + n/2, n: n/2)
reculsive(arr: arr, row: row + n/2, col: col, n: n/2)
reculsive(arr: arr, row: row + n/2, col: col + n/2, n: n/2)
return
}
}
}
// ํ๋์ ์์ญ์ผ๋ก ๋ฌถ์์ ๊ฒฝ์ฐ or ๋๊น์ง ๋ฌถ์ด์ง ์์ n์ด 1์ธ ๊ฒฝ์ฐ
if point == 1 { oneCount += 1 }
else { zeroCount += 1}
}
}
'์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (0) | 2022.04.16 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ํฐ ์ ๋ง๋ค๊ธฐ (4) | 2022.04.13 |
[Swift] BOJ 1753 ์ต๋จ๊ฒฝ๋ก (0) | 2022.04.13 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ ๋ ฅ๋ง์ ๋๋ก ๋๋๊ธฐ (0) | 2022.04.06 |
[Swift] BOJ 11724 ์ฐ๊ฒฐ ์์์ ๊ฐ์ (0) | 2022.04.05 |
Comments