๋ฐ์ํ
ํํ
๋ฌธ์ ์ค๋ช
์ ๋ค ๊ฐ์ ์ขํ๋ฅผ ๋ด์ ์ด์ฐจ์ ๋ฐฐ์ด dots๊ฐ ๋ค์๊ณผ ๊ฐ์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค.
- [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
์ฃผ์ด์ง ๋ค ๊ฐ์ ์ ์ ๋ ๊ฐ์ฉ์ด์์ ๋, ๋ ์ง์ ์ด ํํ์ด ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฉด 1์ ์์ผ๋ฉด 0์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด ๋ณด์ธ์.
์ ํ ์ฌํญ
- 0 ≤ dots์ ์์ ≤ 100
- dots์ ๊ธธ์ด = 4
- dots์ ์์์ ๊ธธ์ด = 2
- dots์ ์์๋ [x, y] ํํ์ด๋ฉฐ x, y๋ ์ ์์ ๋๋ค.
- ์๋ก ๋ค๋ฅธ ๋ ๊ฐ ์ด์์ ์ ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.
- ๋ ์ง์ ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ(์ผ์นํ๋ ๊ฒฝ์ฐ)์๋ 1์ return ํด์ฃผ์ธ์.
- ์์์ ๋ ์ ์ ์ด์ ์ง์ ์ด x์ถ ๋๋ y์ถ๊ณผ ํํํ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
dots | result |
[[1, 4], [9, 2], [3, 8], [11, 6]] | 1 |
[[3, 5], [4, 1], [2, 4], [5, 10]] | 0 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
- ์ [1, 4], [3, 8]์ ์๊ณ [9, 2], [11, 6]๋ฅผ ์ด์ผ๋ฉด ๋ ์ ๋ถ์ ํํํฉ๋๋ค.
์ ์ถ๋ ฅ ์ #2
- ์ ์ ์ด๋ป๊ฒ ์ฐ๊ฒฐํด๋ ํํํ์ง ์์ต๋๋ค.
์ ์ถ
import Foundation
func slope(_ dot1: [Int], _ dot2: [Int]) -> Double{
return Double(dot2[1] - dot1[1]) / Double(dot2[0] - dot1[0])
}
func solution(_ dots:[[Int]]) -> Int {
if slope(dots[0], dots[1]) == slope(dots[2], dots[3]) {return 1}
if slope(dots[0], dots[2]) == slope(dots[1], dots[3]) {return 1}
if slope(dots[0], dots[3]) == slope(dots[1], dots[2]) {return 1}
return 0
}
์ง์ ์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ตฌํ๋ ํจ์ slope๋ฅผ ์์ฑํ๋ค.
๋ ์ง์ ์ ๊ธฐ์ธ๊ธฐ๊ฐ ์๋ก ๊ฐ๋ค๋ฉด ํํ์ด๋ฏ๋ก 1์ ๋ฐํ
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ ํ์์ ํ๋ณํ๊ธฐ (0) | 2023.01.04 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ๊ฒน์น๋ ์ ๋ถ์ ๊ธธ์ด (0) | 2023.01.03 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ ์ฃผ์ ์ซ์ 3 (0) | 2023.01.03 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ธ๊ณ์ด ์ฌ์ (0) | 2023.01.02 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ผ๊ฐํ์ ์์ฑ์กฐ๊ฑด (2) (0) | 2023.01.02 |