ํ์ด ๐๐ปโ๏ธ
function solution(sizes) {
const maxSizes = Math.max(...sizes.flat());
let secondMaxSizes = 0;
for (el of sizes) {
if (el[0] > el[1]) {
if (el[1] > secondMaxSizes) {
secondMaxSizes = el[1];
}
} else {
if (el[0] > secondMaxSizes) {
secondMaxSizes = el[0];
}
}
}
return maxSizes * secondMaxSizes;
}
- ๋ถํ์ํ if ๋ฌธ์ด ๋ง์๋ณด์ธ๋ค..
ํผ๋๋ฐฑ
function solution(sizes) {
const maxSize = Math.max(...sizes.flat());
let secondMaxSize = 0;
for (const el of sizes) {
secondMaxSizes =
el[0] > el[1]
? Math.max(secondMaxSizes, el[1])
: Math.max(secondMaxSizes, el[0]);
}
return maxSizes * secondMaxSizes;
}
- for of ๋ฌธ์ ๋ณ์ ํค์๋ ์ค์ ํด์ฃผ๊ธฐ.
- ๋จ์ ์ซ์๊ฐ ๋ณ์๋ช ๋จ์ํ์ผ๋ก ๋ณ๊ฒฝ
- ์ผํญ์ฐ์ฐ์์ Math.max ๋ก ์ค์ฒฉ if๋ฌธ ๊ฐ์
๋ค๋ฅธ ํ์ด
function solution(sizes) {
const sortedSizes = sizes.map(([width, height]) =>
width < height ? [height, width] : [width, height]
);
let [maxWidth, maxHeight] = [0, 0];
return sortedSizes.reduce((_, cur) => {
const [width, height] = cur;
maxWidth = Math.max(width, maxWidth);
maxHeight = Math.max(height, maxHeight);
return maxWidth * maxHeight;
}, 0);
}
- map์ ํตํด ์์๋ฅผ ๋ฏธ๋ฆฌ ์ ๋ ฌํ๊ณ reduce ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋์ถํด๋ธ ํ์ด๐
'์๊ณ ๋ฆฌ์ฆ & ๋ฌธ์ ํ์ด > Programmers-1' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ชจ์๊ณ ์ฌ (0) | 2022.10.09 |
---|---|
ํฐ์ผ๋ชฌ (0) | 2022.10.09 |
์ด์ํ ๋ฌธ์ ๋ง๋ค๊ธฐ (0) | 2022.10.01 |
๊ฐ์ ์ซ์๋ ์ซ์ด (0) | 2022.09.30 |
์ต๋๊ณต์ฝ์์ ์ต๋๊ณต๋ฐฐ์ (0) | 2022.09.28 |