eaz_coding

[Programmers] 방금그곡 (Python, Java) 본문

eaz_algorithm

[Programmers] 방금그곡 (Python, Java)

eaz_silver 2024. 6. 3. 14:30

문제

요약

노래의 일부만 듣고 방금 들은 곡인지 확인하고 싶다.

"시작시간,끝시간,제목,코드" 가 들어있는 배열이 주어지고, 코드는 C, C#, D, D#, E, F, F#, G, G#, A, A#, B가 주어진다.

(문제 조건에 B#이 없는데 테케에 있음.)

노래는 처음부터 시작하고 재생시간이 코드보다 길 경우 처음부터 다시 반복된다.

 

문제

https://school.programmers.co.kr/learn/courses/30/lessons/17683

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 


풀이

Python

def solution(m, musicinfos):
    answer, at = "", 0
    codes = {"C#":"1","D#":"2","F#":"3","G#":"4", "A#":"5", "B#":"6"}
    
    for i, v in codes.items():
        m = m.replace(i, v)
    
    for info in musicinfos:
        start, end, name, code = info.split(",")
        sh, sm = map(int, start.split(":"))
        eh, em = map(int, end.split(":"))
        
        time = (eh-sh)*60 + em-sm
            
        for i, v in codes.items():
            code = code.replace(i, v)
        
        if time > len(code):
            tmp = code * (time//len(code)) + code[:time%len(code)]
        else:
            tmp = code[:time]
        
        if m in tmp:
            if at < time:
                answer = name
                at = time          

    return answer if len(answer) else "(None)"

 

Java

새로 배운 것

1. 다른 메소드에서 변수에 접근이 가능하게 하기 위해서는 메소드 바깥에 전역변수로 선언한 뒤, 값을 메소드 내에서 할당해준다.

2. 문자열을 여러번 쓸 때, 파이썬은 *를 사용하지만, 자바는 repeat를 사용한다.

 

자바의 Map 쓰는 법 잊지말기!

import java.util.*;

class Solution {
    static Map<String, String> dict = new HashMap<String, String>();
    
    public String change(String str){
        for (String k : dict.keySet()){
            str = str.replace(k, dict.get(k));
        }    
        return str;
    }
    
    public int getTime(String time) {
        Integer[] HM = Arrays.stream(time.split(":"))
                                  .map(Integer::parseInt)
                                  .toArray(Integer[]::new);
        
        return HM[0]*60 + HM[1];
    }
    
    public String solution(String m, String[] musicinfos) {
        String answer = "";
        int at = 0;
        
        dict.put("C#", "1");
        dict.put("D#", "2");
        dict.put("F#", "3");
        dict.put("G#", "4");
        dict.put("A#", "5");
        dict.put("B#", "6");
        
        m = change(m);
        
        for (String info : musicinfos) {
            String[] lst = info.split(",");
            int time = getTime(lst[1]) - getTime(lst[0]);
            
            String code = change(lst[3]);
            String tmp;
            int l = code.length();

            if (time > l){
                tmp = code.repeat(time/l) + code.substring(0, time%l);
            } else {
                tmp = code.substring(0, time);                    
            }

            if (tmp.contains(m)){
                if (time > at) {
                    answer = lst[2];
                    at = time;
                }    
            }
        }
        
        if (answer.length() > 0){
            return answer;
        } else {
            return "(None)";
        }
    }
}