Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- suspense ๋์์๋ฆฌ
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- ์ต์ ๋์ฒด์ด๋
- suspense ๋น๋๊ธฐ
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- suspense react-query
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- ๊ตฌ๋ฆํค
- ์ค์ฝํ
- ์ฌ์ฉ์ฑ ๊ฐ์
- js ์ค์ฝํ
- ๋ฐฑ์ค 1339๋ฒ nodejs
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- emtion app router
- js ๋ฌธ์์ด ์์ถ
- ๋ฐฑ์ค 1339๋ฒ js
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- emotion RSC
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- next13 emotion
- app router emotion
- suspense ๋ณ๋ชฉํ์
- ์นด์นด์ค ์ฝํ
- js
- ๋ฐฑ์ค 2108 nodejs
Archives
- Today
- Total
Lennon FE
[๋ฐฑ์ค 1744๋ฒ] ์ ๋ฌถ๊ธฐ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) ๋ณธ๋ฌธ
๐ฅ Algorithm/Baekjoon
[๋ฐฑ์ค 1744๋ฒ] ์ ๋ฌถ๊ธฐ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon 2022. 2. 18. 21:25728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/1744
const fs = require('fs');
const [n, ...input] = fs.readFileSync('../input.txt').toString().trim().split('\n').map(Number);
const negative = [];
const positive = [];
let zero = 0;
let answer = 0;
input.forEach((v) => {
if (v < 0) {
negative.push(v);
} else if (v > 1) {
positive.push(v);
} else if (v === 0) {
zero++;
} else {
answer++;
}
});
negative.sort((a, b) => b - a);
positive.sort((a, b) => a - b);
while (positive.length) {
if (positive.length === 1) {
answer += positive.pop();
break;
}
const value = positive.pop();
const value2 = positive.pop();
answer += value * value2;
if (positive.length === 1) {
answer += positive.pop();
break;
}
}
while (negative.length) {
if (negative.length === 1) {
if (zero !== 0) {
zero--;
break;
} else {
answer += negative.pop();
break;
}
}
if (negative.length % 2 === 1 && zero !== 0) {
negative.shift();
zero--;
}
const value = negative.pop();
const value2 = negative.pop();
answer += value * value2;
if (negative.length === 1) {
answer += negative.pop();
break;
}
}
console.log(answer);
1์ ์์์ ๊ณฑํ๋ฉด ๋ํ๋ ๊ฐ์ด ์์์ง๋ฏ๋ก ex) 1, 11 => ๊ณฑํ๋ฉด 11 or ๋ํ๋ฉด 12 ๊ทธ๋ฅ answer์ 1์ ๋ํด์ค๋ค.
์์ ์์๋ฅผ ๋ฐ๋ก ๋ฐฐ์ด์ ์ ์ฅํด sort๋ก ์ ๋ ฌํ ํ ํ์, ์ง์ ๊ฐ์๋ฅผ ์๊ฐํ๋ฉฐ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌ์ฑํ๋ค.
๋ง์ฝ 0์ด ์์ ๋๋ ์์๊ฐ ํ์์ผ ๋ ์์์ค์ ๊ฐ์ฅ ํฐ ๊ฐ์ด๋ ๊ณฑํด์ค์ผ ํฉ์ด ์ต๋์ด๋ค.
๊ทธ๋ฆฌ๊ณ , ์์, ์์๊ฐ 1๊ฐ์ฉ๋ง ์์ ๋๋ฅผ ์๊ฐํด ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌ์ํ๋ฉด ์ฝ๊ฒ ํ ์ ์๋ค.
728x90
๋ฐ์ํ
'๐ฅ Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments