๐ฅ Algorithm/Programmers
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฐฉ๊ธ ๊ทธ๊ณก (js)
Lennon
2021. 10. 28. 17:14
728x90
๋ฐ์ํ
https://programmers.co.kr/learn/courses/30/lessons/17683?language=javascript
function solution(m, musicinfos) {
let melody;
let answer = [];
m = conversionMelody(m);
for(let i = 0; i < musicinfos.length; i++){
let v = musicinfos[i];
v = v.split(",");
v[3] = conversionMelody(v[3]);
const time = (v[1].slice(0,2)-v[0].slice(0,2))*60 + (v[1].slice(3)-v[0].slice(3))
const repeatCount = time/v[3].length;
const remainder = time % v[3].length;
if(repeatCount<1){
melody = v[3].slice(0,time);
}
else{
melody = v[3].repeat(repeatCount)+v[3].slice(0,time);
}
if(melody.includes(m)){
answer.push({value: v[2], time: time});
}
}
answer.sort((a,b) => b.time-a.time);
return answer.length===0 ? "(None)" : answer[0].value
}
function conversionMelody(m){
return m = m.replace(/C#/gi, 'c')
.replace(/D#/gi, 'd')
.replace(/F#/gi, 'f')
.replace(/G#/gi, 'g')
.replace(/A#/gi, 'a')
.replace(/E#/gi, 'e');
}
๋์ค์ ์ฒ๋ฆฌํ๊ธฐ ๊ท์ฐฎ์ #์ฒ๋ฆฌ๋ฅผ ๋ฉ๋ก๋์ ๋ณธ์ธ์ ๋ฉ๋ก๋์ ๋จผ์ ํด์ฃผ๊ณ
์๊ฐ์ ๊ตฌํ ํ ๋ฉ๋ก๋๋ฅผ ๊ทธ๋งํผ ๋ฐ๋ณตํด์ค๋ค.
๋ฐ๋ณตํ ๋ฉ๋ก๋์ ๋ณธ์ธ์ ๋ฉ๋ก๋๊ฐ includes ๋์ด์์ผ๋ฉด ๋ฐฐ์ด์ ๊ฐ์ฒด๋ก ์ ๋ชฉ๊ณผ ์๊ฐ์ ํจ๊ป ๋ฃ๋๋ค.
์ฌ๋ฌ๊ฐ๊ฐ ์์ผ๋ฉด ์๊ฐ์ด ๊ฐ์ฅ ํฐ ๋ฉ๋ก๋๋ฅผ ๋ฆฌํด / ํด๋น๋๋ ๊ฒ ์์ผ๋ฉด "(None)" ๋ฆฌํด ์ด๋ฏ๋ก
์๊ฐ ์์ผ๋ก ์ ๋ ฌ ํ ๋ฆฌํดํด์ฃผ๋ฉด ๋๋ค!
๋ฌธ์ ํ ๋ ์๊ตฌ์ฌํญ์ ์ ๋ฆฌํ๊ณ ํ๋ฉด ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ์๊ฐ์ ์ ์ฝํ ์ ์๋ค.
๋ฌธ์ ๋ฅผ ๊ผผ๊ผผํ๊ฒ ์ฝ๊ณ ํ์!
728x90
๋ฐ์ํ