๐ŸŸฅ[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์‚ฌ๊ฐํ˜• ๋„“์ด ๊ตฌํ•˜๊ธฐ

Chobbyยท2022๋…„ 10์›” 20์ผ
2

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
132/349
post-custom-banner

๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด์— ๋ณ€์ด ์ถ•๊ณผ ํ‰ํ–‰ํ•œ ์ง์‚ฌ๊ฐํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ง์‚ฌ๊ฐํ˜• ๋„ค ๊ผญ์ง“์ ์˜ ์ขŒํ‘œ [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐฐ์—ด dots๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • dots์˜ ๊ธธ์ด = 4
  • dots์˜ ์›์†Œ์˜ ๊ธธ์ด = 2
  • -256 < dots[i]์˜ ์›์†Œ < 256
  • ์ž˜๋ชป๋œ ์ž…๋ ฅ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

dotsresult
[[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์ž…๋‹ˆ๋‹ค.

๐Ÿ’œ๋‚˜์˜ ํ’€์ด

function solution(dots) {
    // ๊ฐ€์žฅ ํฐ x์˜ ๊ฐ’ - ๊ฐ€์žฅ ์ž‘์€ x์˜ ๊ฐ’ = ๊ฐ€๋กœ ๊ธธ์ด
    const width = Math.max(...dots.map(a => a[0]))-Math.min(...dots.map(a => a[0]))
    // ๊ฐ€์žฅ ํฐ y์˜ ๊ฐ’ - ๊ฐ€์žฅ ์ž‘์€ y์˜ ๊ฐ’ = ์„ธ๋กœ ๊ธธ์ด
    const height = Math.max(...dots.map(a => a[1]))-Math.min(...dots.map(a => a[1]))
    // ๋„“์ด ๋ฐ˜ํ™˜
    return width*height
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€