728x90
๐ 21 - ๋ฑ์ ๊ตฌํ๊ธฐ
์ด ๋ฌธ์ ๋ boj7568 - ๋ฉ์น์ ๋น์ทํ ๋ฌธ์ ๋ค. ๋ฑ์ ๊ตฌํ๊ธฐ ๋ฌธ์ ๊ฐ ์ฝ๋ค๋ฉด ๋ฉ์น ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์. ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ๋ bruteForce
๋ฐฉ๋ฒ์ผ๋ก ํ๋ฉด ๋๋๋ฐ, ๋ชจ๋ ๊ฐ์ 1
๋ก ๋น ๋ฐฐ์ด์ ์ด๊ธฐํ ์ํฌ ๋ Array.from
์ ์ด์ฉํ๋ค.
let n = 5;
let arr = [87, 89, 92, 100, 76];
console.log(solution(n, arr));
// ๋์์ฝ๋
function solution(n, arr) {
let ans = [];
for (let i = 0; i < n; i++) {
let rank = 1;
for (let j = 0; j < n; j++) {
if (arr[i] < arr[j]) rank++;
}
ans.push(rank);
}
console.log(ans)
}
// ๊ฐ์ฌ๋ ์ฝ๋
let n = 5;
let scores = [100, 88, 76, 88, 76];
console.log(solution(n, scores));
function solution(n, scores) {
let answers = Array.from({ length: n }, () => 1);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (scores[j] > scores[i]) {
answers[i]++;
}
}
}
return answers;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 23 - ๋ด์ฐ๋ฆฌ (0) | 2021.08.17 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 22 - ๊ฒฉ์ํ ์ต๋ํฉ (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 20 - ์ ์ ๊ณ์ฐ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 19 - ๊ฐ์ ๋ฐ์ ๋ณด (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 18 - ๋ณด์ด๋ ํ์ (0) | 2021.08.13 |
๋๊ธ