728x90
๋ฐ์ํ
Problem
Solution
1. ์์์์ ์์น๋ฅผ ํ์ ํ๋ค.
func getSeatLocation(places:[String],xMap:inout [[String]],applicants:inout [Seat]) {
for (y,place) in places.enumerated() {
xMap.append(place.map{String($0)})
for (x,p) in place.map({String($0)}).enumerated() {
if p == "P" {
applicants.append((x,y))
}
}
}
}
2. ๋งจํํผ ๊ฑฐ๋ฆฌ๊ฐ 2์ดํ์ผ ๊ฒฝ์ฐ 3๊ฐ์ง ์กฐ๊ฑด์ผ๋ก ๊ฒ์ฌํ๋ค.
2-1. x์์น๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
y์์น๊ฐ ๋ ์ ์ ์์์์ y์ +1์ ํด์ค ๊ณณ์ด "X"์ธ์ง ํ์ธํ๋ค.
2-2. y์์น๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
x์์น๊ฐ ๋ ์ ์ ์์์์ x์ +1์ ํด์ค ๊ณณ์ด "X"์ธ์ง ํ์ธํ๋ค.
2-3.x์ y๋ชจ๋ ๋ค๋ฅผ ๊ฒฝ์ฐ
x์์น๊ฐ ๋ ์ ์ ๊ณณ์ x์ +1์ ํด์ค ๊ณณ์ด "X"์ธ์ง ํ์ธํ๊ณ
x์์น๊ฐ ๋ ํฐ ๊ณณ์ x -1์ ํด์ค ๊ณณ์ด "X"์ธ์ง ํ์ธํ๋ค.
func getManhattanDistance(p1:Seat,p2:Seat) -> Int {
return abs(p1.0-p2.0) + abs(p1.1-p2.1)
}
func isKeepDistance(p1:Seat,p2:Seat,xMap:inout [[String]]) -> Bool {
let minX = p1.0 < p2.0 ? p1 : p2
let minY = p1.1 < p2.1 ? p1 : p2
if p1.0 == p2.0 {
return xMap[minY.1+1][p1.0] == "X"
}else if p1.1 == p2.1 {
return xMap[p1.1][minX.0+1] == "X"
}else {
if p1.0 < p2.0 {
return xMap[p1.1][p1.0+1] == "X" && xMap[p2.1][p2.0-1] == "X"
}else {
return xMap[p2.1][p2.0+1] == "X" && xMap[p1.1][p1.0-1] == "X"
}
}
}
3. ๋ชจ๋ ๋๊ธฐ์ค์ ๊ฒ์ฌํ๊ณ ์กฐ๊ฑด์ ๋ง๊ฒ 0๊ณผ 1์ ๋ฃ์ ๋ค ๋ฐํํ๋ค.
func solution(_ places:[[String]]) -> [Int] {
var answer:[Int] = []
for place in places {
var check:Bool = false
var applicants:[Seat] = []
var xMap:[[String]] = []
getSeatLocation(places: place,xMap: &xMap,applicants: &applicants)
outer:while !applicants.isEmpty {
let first = applicants.removeFirst()
for applicant in applicants {
if getManhattanDistance(p1: first, p2: applicant) <= 2 {
if !isKeepDistance(p1: first, p2: applicant,xMap: &xMap) {
answer.append(0)
check = true
break outer
}
}
}
}
if !check {
answer.append(1)
}
}
return answer
}
Source Code
728x90
๋ฐ์ํ
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] 2020 KAKAO BLIND RECRUITMENT ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์นํ๊ธฐ (0) | 2021.08.13 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ์ํด๋ฆฌ ์ฑ๋ฆฐ์ง 2์ฃผ์ฐจ ์ํธ ํ๊ฐ (0) | 2021.08.10 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ์ํด๋ฆฌ ์ฑ๋ฆฐ์ง 1์ฃผ์ฐจ ๋ถ์กฑํ ๊ธ์ก ๊ณ์ฐํ๊ธฐ (0) | 2021.08.07 |
[Swift] 2018 KAKAO BLIND RECRUITMENT [1์ฐจ] ์ ํ๋ฒ์ค (0) | 2021.08.06 |
[Swift] 2020 KAKAO INTERNSHIP ๋ณด์ ์ผํ (0) | 2021.08.04 |
๋๊ธ