๋ฌธ์ ๋งํฌ
์ ๊ทผ ๋ฐฉ๋ฒ
์ฒ์์๋ ๋ชจ๋ ํต๊ณผ์(completion)๋ฅผ ์ํํ์ฌ ์ ์ฒด ์ฐธ๊ฐ์(participant)์์ ์ ์ธํ์ฌ
๋ง์ง๋ง ๋จ์ ์ฐธ๊ฐ์๋ฅผ ์ ๋ต์ผ๋ก ๊ตฌํ๋ค.
ํ์ง๋ง ํจ์จ์ฑ์์ ๋ฏธํต๊ณผ๊ฐ ๋์๋๋ฐ, ๋ฐฐ์ด ์ํ๋ฅผ forEach, indexOf, splice ์ธ๋ฒ์ ์งํํด์ ๊ทธ๋ฐ๊ฑฐ๊ฐ๋ค.
๋ค์ ๋ฐฉ๋ฒ์ผ๋ก๋ completion, participant๋ฅผ ์ ๋ ฌํด๋ณด๋
๋ฏธํต๊ณผ์ ํ๋ช ๋นผ๊ณ ๋ ๊ฐ์ ๋ฐฐ์ด์ด ๋ง๋ค์ด์ก๋ค.
๋๋ฌธ์ ๋์ ๋น๊ตํด์ ๋์ ์์๊ฐ ๋ฌ๋ผ์ง๋ answer์ ๋์ ํ๊ณ break; ํ์ฌ ์ ๋ต์ ๊ตฌํ๋ค.
(์ฒ์์๋ break;๋ฅผ ํ์ง์์ ํต๊ณผํ์ง ๋ชปํ์)
ํ๋ฆฐ ํ์ด (ํจ์จ์ฑ ํ ์คํธ ๋ฏธํต๊ณผ)
function solution(participant, completion) {
completion.forEach((runner)=> {
participantIdx = participant.indexOf(runner)
participant.splice(participantIdx,1)
})
return participant.join("");
}
๋ง์ ํ์ด
function solution(participant, completion) {
var answer = "";
participant.sort();
completion.sort();
for(let i = 0; i < participant.length; i++){
if(participant[i] !== completion[i]){
answer = participant[i];
break;
}
}
return answer;
}
'์ฝ๋ฉํ ์คํธ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript] 3์ง๋ฒ ๋ค์ง๊ธฐ (68935) (0) | 2022.05.04 |
---|---|
[Javascript] ์ฝ์์ ๊ฐ์์ ๋ง์ (77884) (0) | 2022.05.04 |
[Javascript] ํ๋ ฌ ํ ๋๋ฆฌ ํ์ ํ๊ธฐ (77485) (0) | 2022.05.02 |
[Javascript] ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ (12973) (0) | 2022.05.02 |
[Javascript] ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธ (81302) (0) | 2022.05.01 |