Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฐฉ๊ธˆ ๊ทธ๊ณก (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฐฉ๊ธˆ ๊ทธ๊ณก (js)

Lennon 2021. 10. 28. 17:14
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/17683?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [3์ฐจ] ๋ฐฉ๊ธˆ๊ทธ๊ณก

๋ฐฉ๊ธˆ๊ทธ๊ณก ๋ผ๋””์˜ค๋ฅผ ์ž์ฃผ ๋“ฃ๋Š” ๋„ค์˜ค๋Š” ๋ผ๋””์˜ค์—์„œ ๋ฐฉ๊ธˆ ๋‚˜์™”๋˜ ์Œ์•…์ด ๋ฌด์Šจ ์Œ์•…์ธ์ง€ ๊ถ๊ธˆํ•ด์งˆ ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ๋„ค์˜ค๋Š” ๋‹ค์Œ ํฌํ„ธ์˜ '๋ฐฉ๊ธˆ๊ทธ๊ณก' ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ณค ํ•œ๋‹ค. ๋ฐฉ๊ธˆ๊ทธ๊ณก์—์„œ๋Š” TV,

programmers.co.kr

 

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
๋ฐ˜์‘ํ˜•
Comments