๋ฐ์ํ
์ง์ฌ๊ฐํ ๋์ด ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช
2์ฐจ์ ์ขํ ํ๋ฉด์ ๋ณ์ด ์ถ๊ณผ ํํํ ์ง์ฌ๊ฐํ์ด ์์ต๋๋ค. ์ง์ฌ๊ฐํ ๋ค ๊ผญ์ง์ ์ ์ขํ [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]๊ฐ ๋ด๊ฒจ์๋ ๋ฐฐ์ด dots๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ง์ฌ๊ฐํ์ ๋์ด๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด ๋ณด์ธ์.
์ ํ ์ฌํญ
- dots์ ๊ธธ์ด = 4
- dots์ ์์์ ๊ธธ์ด = 2
- -256 < dots[i]์ ์์ < 256
- ์๋ชป๋ ์ ๋ ฅ์ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
dots | result |
[[1, 1], [2, 1], [2, 2], [1, 2]] | 1 |
[[-1, -1], [1, 1], [1, -1], [-1, 1]] | 4 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
- ์ขํ [[1, 1], [2, 1], [2, 2], [1, 2]] ๋ฅผ ๊ผญ์ง์ ์ผ๋ก ๊ฐ๋ ์ง์ฌ๊ฐํ์ ๊ฐ๋ก, ์ธ๋ก ๊ธธ์ด๋ ๊ฐ๊ฐ 1, 1์ด๋ฏ๋ก ์ง์ฌ๊ฐํ์ ๋์ด๋ 1 x 1 = 1์ ๋๋ค.
์ ์ถ๋ ฅ ์ #2
- ์ขํ [[-1, -1], [1, 1], [1, -1], [-1, 1]]๋ฅผ ๊ผญ์ง์ ์ผ๋ก ๊ฐ๋ ์ง์ฌ๊ฐํ์ ๊ฐ๋ก, ์ธ๋ก ๊ธธ์ด๋ ๊ฐ๊ฐ 2, 2์ด๋ฏ๋ก ์ง์ฌ๊ฐํ์ ๋์ด๋ 2 x 2 = 4์ ๋๋ค.
์ ์ถ
import Foundation
func solution(_ dots:[[Int]]) -> Int {
var x = dots[0][0] - dots[1][0] != 0 ? dots[0][0] - dots[1][0] : (dots[0][0] - dots[2][0] != 0 ? dots[0][0] - dots[2][0] : dots[0][0] - dots[3][0])
var y = dots[0][1] - dots[1][1] != 0 ? dots[0][1] - dots[1][1] : (dots[0][1] - dots[2][1] != 0 ? dots[0][1] - dots[2][1] : dots[0][1] - dots[3][1])
return abs(x)*abs(y)
}
x1 - xn์ด 0์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํด ๋ณ์ x์ ์ ์ฅ (n = 2, 3, 4)
y1 - yn์ด 0์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํด ๋ณ์ y์ ์ ์ฅ (n = 2, 3, 4)
x์ y์ ์ ๋๊ฐ์ ๊ณฑํ์ฌ ๋ฐํํ๋ค
๋ค๋ฅธ ํ์ด
import Foundation
func solution(_ dots:[[Int]]) -> Int {
let dots = dots.sorted(by: { ($0[0], $0[1]) < ($1[0], $1[1]) })
return (dots.last![0] - dots.first![0]) * (dots.last![1] - dots.first![1])
}
dots๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ ํ dots ๋ฐฐ์ด์์ ๋ง์ง๋ง ๊ฐ์์ ์ฒซ ๋ฒ์งธ ๊ฐ์ ๋นผ์ ๊ณฑํ์ฌ ๋ฐํํ๋ค
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ต๋๊ฐ ๋ง๋ค๊ธฐ (2) (0) | 2023.01.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์บ๋ฆญํฐ์ ์ขํ (0) | 2023.01.02 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ๋ฐฐ์ด ์์์ ๊ธธ์ด (0) | 2022.12.30 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ปจํธ๋กค ์ ํธ (0) | 2022.12.30 |
[ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์์ธ์๋ถํด (0) | 2022.12.30 |