์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 31 |
- ๋ฐฑ์ค 2108 nodejs
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- js ์ค์ฝํ
- ์นด์นด์ค ์ฝํ
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- suspense ๋์์๋ฆฌ
- ์ค์ฝํ
- emotion RSC
- emtion app router
- ์ต์ ๋์ฒด์ด๋
- app router emotion
- ์ฌ์ฉ์ฑ ๊ฐ์
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- suspense ๋น๋๊ธฐ
- js
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- suspense react-query
- ๋ฐฑ์ค 1339๋ฒ nodejs
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- js ๋ฌธ์์ด ์์ถ
- next13 emotion
- suspense ๋ณ๋ชฉํ์
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- ๊ตฌ๋ฆํค
- ๋ฐฑ์ค 1339๋ฒ js
- Today
- Total
Lennon FE
[๋ฐฑ์ค 1062๋ฒ] ๊ฐ๋ฅด์นจ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) ๋ณธ๋ฌธ
[๋ฐฑ์ค 1062๋ฒ] ๊ฐ๋ฅด์นจ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon 2022. 2. 11. 19:34https://www.acmicpc.net/problem/1062
const fs = require('fs');
let input = fs.readFileSync('../input.txt').toString().trim().split('\n');
const [N, K] = input.shift().split(' ').map(Number);
const words = ['a', 'n', 't', 'i', 'c'];
input = input.map((v) => v.slice(4, v.length - 4));
const combinations = function* (elements, selectNumber) {
for (let i = 0; i < elements.length; i++) {
if (selectNumber === 1) {
yield [elements[i]];
} else {
const fixed = elements[i];
const rest = elements.slice(i + 1);
for (const a of combinations(rest, selectNumber - 1)) {
yield [fixed, ...a];
}
}
}
};
const filterWords = (arr) => {
return arr.map((v) => {
const word = [...new Set(v.split(''))].filter((ele) => !words.includes(ele));
return word;
});
};
if (K < 5) {
console.log(0);
} else if (K === 5) {
const filterWord = filterWords(input);
console.log(filterWord.filter((v) => v.length === 0).length);
} else {
const filterWord = filterWords(input);
const flatWord = [...new Set(filterWord.flat())];
let answer = 0;
if (K > [...flatWord, ...words].length) {
console.log(N);
return;
}
for (let ele of combinations(flatWord, K - 5)) {
const str = [...words, ...ele];
let count = 0;
Loop: for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
if (!str.includes(input[i][j])) {
continue Loop;
}
}
count++;
}
answer = Math.max(answer, count);
}
console.log(answer);
}
์ฌ์ด ๋ฌธ์ ์ด์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ๋ก ํ๋ฉด ๊ณ์ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ ์ ์๋ ๋ฐฉ๋ฒ์ ๋ญ๊ฐ ์์๊น ํ๋ค๊ฐ
https://kscodebase.tistory.com/520
์ด ๋ถ์ ๋ธ๋ก๊ทธ์ ์กฐํฉ ์ฝ๋๋ฅผ ๊ฐ์ ธ์์ ์ผ๋๋ ๋ฐ๋ก ํต๊ณผ๊ฐ ๋๋ค. ์ฝ๋๋ฅผ ์ค๋ช ํ์๋ฉด
Function combinations
์กฐํฉ์ฝ๋์ด๋ฉฐ, ์กฐํฉ ๊ฒฐ๊ณผ๊ฐ objํํ๋ก ๋ฐํ๋๋ค.
Function filterWords
words = ['a', 'n', 't', 'i', 'c'] ๋ฅผ ์ ์ธํ๊ณ ๋จ๋ ๋ฌธ์๋ฅผ ๋ฆฌํดํด์ฃผ๋ ํจ์์ด๋ค.
ex) [ 'antihj' ] => ['hj']
1. ์ฃผ์ด์ง input์์ ์ค๋ณต๋๋ ๊ฐ์ธ ์๋ 4๊ฐ๋ฅผ ์๋ผ์ ๋ฐฐ์ดํํ๋ก ๋ฆฌํดํด์ค๋ค.
ex) [ 'antarctica', 'antahellotica', 'antacartica' ] => [ 'rc', 'hello', 'car' ]
2. ๊ธฐ๋ณธ์ ์ผ๋ก K๊ฐ 5๊ฐ ์ด์์ด์ด์ผ ๋ง์กฑํ๋ ๋จ์ด์ ๊ฐ์๊ฐ ์ต์ 0๊ฐ๋ผ๋ ๋๋ฏ๋ก K๊ฐ 5๋ณด๋ค ์์ผ๋ฉด ๊ทธ๋ฅ 0์ ์ฝ์๋ก ์ถ๋ ฅํด์ค๋ค.
3. K๊ฐ 5๊ฐ๋ฉด filterWordsํจ์๋ฅผ ์ด์ฉํด words์ ํฌํจ๋์ง ์๋ ์๋ ๋ฌธ์๋ง ํํฐ๋งํด์ฃผ๊ณ , ๊ธธ์ด๊ฐ 0์ธ input๊ฐ์ ๊ฐ์๋ฅผ ์ฝ์๋ก ์ถ๋ ฅํ๋ค.
=> ๊ธธ์ด๊ฐ 0์ด๋ผ๋ ๊ฑด ๋ชจ๋ words์์ ํฌํจ๋์ด์๋ ๋ฌธ์์ด๋ฏ๋ก ๋ฐฐ์ธ ์ ์๋ ๋ฌธ์์ด๋ค.
ex) [ 'c', 'hello', 'car' ] => [ [], ['h', 'e', 'l', 'o'], ['r']] => ์ ๋ต: 1๊ฐ
4. K๊ฐ 5 ์ด๊ณผ์ด๋ฉด ๋ง์ฐฌ๊ฐ์ง๋ก filterWordsํจ์๋ฅผ ์ด์ฉํด ๋ฌธ์๋ง ํํฐ๋งํด์ฃผ๊ณ , ํด๋น ๋ฌธ์๊ฐ ์ค๋ณต๋์ง ์๋๋ก ์๋ก์ด ๋ฐฐ์ด flatWord์ ์ด๊ธฐํ์์ผ์ค๋ค.
ex) [ 'rc', 'hello', 'car' ] => [ [ 'r' ], [ 'h', 'e', 'l', 'o' ], [ 'r' ] ] => [ 'r', 'h', 'e', 'l', 'o' ]
5. ๋ง์ฝ flatWord์ words๋ฅผ ํฉ์น ๊ธธ์ด๊ฐ K๋ณด๋ค ์๋ค๋ฉด ๋ชจ๋ ์ธ์ด๋ฅผ ๋ฐฐ์ธ ์ ์๋ค๋ ๋ป์ด๋ฏ๋ก ์ ๋ ฅ๊ฐ N์ ์ฝ์๋ก ์ฐ์ด์ค๋ค.
ex) [ 'r', 'h', 'e', 'l', 'o', ...words ].length < K ๋ชจ๋ ์ธ์ด๋ฅผ ๋ฐฐ์ธ ์ ์์!
6. ์๋๋ผ๋ฉด ์กฐํฉ ๊ฒฐ๊ณผ๋ฅผ ์ํํ๋ฉฐ input๊ฐ์์ ๋ง์กฑํ๋ ์ธ์ด์ ๊ฐ์์ ์ต๋๊ฐ์ ๊ตฌํด์ฃผ๊ณ ์ฝ์๋ก ์ฐ์ด์ค๋ค.
์ด ๋ฌธ์ ์ ํต์ฌ์ K๊ฐ 5์ผ๋ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๋ง๋์ ๋ฌ๋ฆฐ ๊ฒ ๊ฐ๋ค. ํ์ง ์์ผ๋ฉด ๋ฌธ์ ๋ ๊ทธ๋ฅ ํ๋ ค๋ฒ๋ฆฐ๋ค.
๋ํ ์๋ฐ์คํฌ๋ฆฝํธ ์กฐํฉ ํจ์์ธ ์๋ ํจ์๋ฅผ ์ฐ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ก ๋ฌธ์ ๊ฐ ํ๋ฆฌ์ง ์๋๋ค.
const getCombinations = function (arr, selectNum) {
const results = [];
if (selectNum === 1) return arr.map((value) => [value]);
arr.forEach((fixed, index) => {
const rest = arr.slice(index + 1);
const combinations = getCombinations(rest, selectNum - 1);
const attached = combinations.map((combination) => [fixed, ...combination]);
results.push(...attached);
});
return results;
};
'๐ฅ Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 1966๋ฒ] ํ๋ฆฐํฐ ํ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.02.15 |
---|---|
[๋ฐฑ์ค 5430๋ฒ] AC - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.02.13 |
[๋ฐฑ์ค 3085๋ฒ] ์ฌํ๊ฒ์ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.02.06 |
[๋ฐฑ์ค 6603๋ฒ] ๋ก๋ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.02.06 |
[๋ฐฑ์ค 1920๋ฒ] ์ ์ฐพ๊ธฐ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs) (0) | 2022.02.03 |