728x90
๐ 20 - ์ ์ ๊ณ์ฐ
๋ฌธ์ ์ ์์ ๊ธธ์ง๋ง ์กฐ๊ธ๋ง ์๊ฐํ๋ฉด ๊ธ๋ฐฉ ํ ์ ์๋ค. ์ฌ๊ธฐ์ ์ค์ํ ์ ์ ๊ฐ์ฐ์
์ฒ๋ฆฌ์ธ๋ฐ, ์ฐ์์ผ๋ก ๋ง์ ๊ฒฝ์ฐ์๋ง +1
์ฉ ์ฆ๊ฐํ๊ฒ ์์ฑํด์ผํ๋ค.
let n = 10;
let scores = [1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
console.log(solution(n, scores));
// ์ผํญ์ฐ์ฐ์
function solution(n, scores) {
let extraPoint = 0;
let cnt = 0;
for (let score of scores) {
score === 1 ? (extraPoint++, (cnt += extraPoint)) : (extraPoint = 0);
}
return cnt;
}
// if - else
function solution(n, scores) {
let extraPoint = 0;
let cnt = 0;
for (let score of scores) {
if (score === 1) {
extraPoint++;
cnt += extraPoint;
} else extraPoint = 0;
}
return cnt;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 22 - ๊ฒฉ์ํ ์ต๋ํฉ (0) | 2021.08.17 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 21 - ๋ฑ์ ๊ตฌํ๊ธฐ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 19 - ๊ฐ์ ๋ฐ์ ๋ณด (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 18 - ๋ณด์ด๋ ํ์ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 17 - ํฐ ์ ์ถ๋ ฅํ๊ธฐ (0) | 2021.08.13 |
๋๊ธ