๋ฐ์ํ
๐ 4-3 MaxCounter
๋ด ํ์ด 1 (์ค๋ต)
function solution(N, A) {
let ans = new Array(N).fill(0);
A.forEach((a) => {
if (a <= N) {
ans[a - 1] += 1;
}
});
return ans;
}
A๋ฅผ ๋๋ฉฐ idx๋ฅผ ๋์ ๊ฐ์ ๋ง๋๋ฉด ๊ทธ ์ค์ ๊ฐ์ฅ ํฐ ๊ฐ์๋ก ๋ฐฐ์ด๊ฐ์ ๋ชจ๋ ๋ฐ๊ฟ์ฃผ๋ ๊ฒ์ธ๋ฐ
๋จ์ง idx๋ง ํ๋์ค ์๊ณ ์ด๋ ๊ฒ ํ์ดํ๋ค.
๋ดํ์ด 2 (77%)
function solution(N, A) {
let ans = new Array(N).fill(0);
let maxNum = 0;
A.forEach((a) => {
let idx = a - 1;
if (a <= N) {
ans[idx] += 1;
maxNum = Math.max(maxNum, ans[idx]);
} else ans.fill(maxNum);
});
return ans;
}
๊ฐ์ ๋ฃ์ด์ค ๋๋ง๋ค maxNum์ ๊ตฌํ๊ณ
๊ฐ์ด idx๋ฅผ ๋์ผ๋ฉด maxNum์ผ๋ก ์ฑ์์ค๋ค.
->2๊ฐ์ง testcase๊ฐ ์ค๋ต์ด ๋์๋ค.
๋ดํ์ด 3 (100%)
function solution(N, A) {
let ans = new Array(N).fill(0);
let maxNum = 0;
let last = 0;
A.forEach((a) => {
let idx = a - 1;
if (a <= N) {
if(ans[idx] < last){
ans[idx] = last;
}
ans[idx] +=1;
if(maxNum < ans[idx]){
maxNum = ans[idx]
}
} else last = maxNum;
});
ans.forEach((a,idx) =>{
if(a < last){
ans[idx] = last
}
})
return ans;
}
Math.max ํ๋ ๋ถ๋ถ์ ์ง์ฐ๊ธฐ ์ํด
last ๋ผ๋ ๋ณ์๋ฅผ ์ถ๊ฐํด A๋ฅผ ๋๋ฉฐ ๊ฐ ๊ฐ๊ณผ last๋ฅผ ๋น๊ตํ๋ค.
๐ฅณ ๋ฌธ์ ๋ฅผ ์ฐจ๋ถํ๊ฒ ์ฝ๊ณ Math ํจ์๋ ์ต์ํ ํ ์ ์๋๋ก...!
'๐ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 64 _ minimumPathSum (4) | 2021.11.23 |
---|---|
[LeetCode] 746 Min Cost Climbing Stairs-javascript (0) | 2021.11.20 |
[codility] JS Lv4-1 ForgRiverOne (0) | 2021.09.18 |
[codility] JS Lv3-3 TapeEquilibrium (0) | 2021.09.17 |
[์ด์ฝ๋ค] ๊ทธ๋ฆฌ๋-๊ธฐ์ถ๋ฌธ์ (2) | 2021.08.31 |
๋๊ธ