CS/Solutions
[ํ๋ก๊ทธ๋๋จธ์ค] ์ง์ฌ๊ฐํ ๋์ด ๊ตฌํ๊ธฐ (JavaScript ๋ฌธ์ ํ์ด)
์ค๋น
2023. 12. 21. 16:03
๋ฐ์ํ
๐ ๋ฌธ์ ์ค๋ช
2์ฐจ์ ์ขํ ํ๋ฉด์ ๋ณ์ด ์ถ๊ณผ ํํํ ์ง์ฌ๊ฐํ์ด ์์ต๋๋ค. ์ง์ฌ๊ฐํ ๋ค ๊ผญ์ง์ ์ ์ขํ [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
๊ฐ ๋ด๊ฒจ์๋ ๋ฐฐ์ด dots
๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ง์ฌ๊ฐํ์ ๋์ด๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด๋ณด์ธ์.
๐ ์ ํ์ฌํญ
dots
์ ๊ธธ์ด = 4dots
์ ์์์ ๊ธธ์ด = 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 |
๐ป ๋์ ํ์ด
- ๊ฐ๋ก ๊ธธ์ด ๊ตฌํ๊ธฐ: x ์ขํ๋ค ์ค ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ์ฐจ์ด๋ฅผ ๊ตฌํฉ๋๋ค.
- ์ธ๋ก ๊ธธ์ด ๊ตฌํ๊ธฐ: y ์ขํ๋ค ์ค ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ์ฐจ์ด๋ฅผ ๊ตฌํฉ๋๋ค.
- ๋์ด ๊ณ์ฐ: ๊ฐ๋ก ๊ธธ์ด์ ์ธ๋ก ๊ธธ์ด๋ฅผ ๊ณฑํฉ๋๋ค.
function solution(dots) {
const xCoords = dots.map(dot => dot[0]);
const yCoords = dots.map(dot => dot[1]);
const width = Math.max(...xCoords) - Math.min(...xCoords);
const height = Math.max(...yCoords) - Math.min(...yCoords);
return width * height;
}
๋ฐ์ํ