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
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- ์ค์ฝํ
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- suspense react-query
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- ๋ฐฑ์ค 2108 nodejs
- ์ฌ์ฉ์ฑ ๊ฐ์
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๋ฐฑ์ค 1339๋ฒ nodejs
- js ์ค์ฝํ
- ๋ฐฑ์ค 1339๋ฒ js
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- js ๋ฌธ์์ด ์์ถ
- emotion RSC
- ๊ตฌ๋ฆํค
- js
- next13 emotion
- app router emotion
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- suspense ๋น๋๊ธฐ
- emtion app router
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- suspense ๋ณ๋ชฉํ์
- suspense ๋์์๋ฆฌ
- ์ต์ ๋์ฒด์ด๋
- ์นด์นด์ค ์ฝํ
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
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