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

 

Lv2 문제다!

구현문제인데 단순하게 생각하면 안되는 문제인 것 같다. 

 

풀이 이해 

  • 재생된 시간을 분으로 변경한다(12:00 ~ 12: 05를 5분으로 변경)
  • 각 노래마다 재생된 시간만큼 길이를 설정한다 
    • 재생된 시간 == 노래 길이 : 노래 한번 반복
    • 재생된 시간 < 노래 길이 : 노래 길이 일부만 재생됨
    • 재생된 시간 > 노래 길이 : 노래 길이 배수만큼 반복되고, 남은 시간은 노래 일부만 재생 
  • 노래 멜로디 중에 m을 포함하는 노래의 제목을 반환한다 (checkSubArray() 함수)
  • m을 포함하는 노래가 2개 이상인 경우 노래 재생시간 순으로한다 
    • 노래 재생시간이 같으면 먼저 재생된 노래를 반환한다 

 

주의할 점 

  • 음정에 #을 포함하는 경우를 잘 생각해야한다. 
  • m이 ABC이고, 노래 음정이 ABC#라면 ABC가 포함되는데, 일치하는 음정이 아니다.
  • 그래서 #이 있는 경우 이것까지 같이 담아야한다
    • 각 음정을 배열의 한 원소로 만드는 함수 getMusicArr()를 이용한다 (m과 각 노래들에 대해 변경해야하므로 함수로 변경하는 것이 용이하다)
    • getMusicArr에서 time == 0이라면 temp의 길이가 0이므로 분기문으로 따로 구현한다 
  • 해당하지 않는 경우는 None이 아닌 문자열 "(None)"을 반환해야한다 

 

- 정답 풀이 :

참고한 블로그

def getMusicArr(melody, time):
    temp = []
    for c in melody:
        if c == '#':
            temp[-1] += '#'
        else:
            temp.append(c)  
        
    if time == 0:
        return temp
            
    if time // len(temp) == 0:
        temp = temp[:time]
    else:
        temp = temp * (time // len(temp)) + temp[: time % len(temp)]
            
    return temp 

def checkSubArray(sub, arr):
    length = len(sub)
    for i in range(len(arr)):
        if sub == arr[i : i + length]:
            return True
    return False
    
def solution(m, musicinfos):
    answer = "(None)"
    answerTime = 0
    
    musics = []
    for i in range(len(musicinfos)):
        start, end, title, melody = musicinfos[i].split(',')
        # 재생 시간 구하기 
        s1, s2 = int(start[0 : 2]), int(start[3 : 5])
        e1, e2 = int(end[0 : 2]), int(end[3 : 5])
        hour, minute = e1 - s1, e2 - s2
        time = hour * 60 + minute
        # 음정 분리하기                 
        musicArr = getMusicArr(melody, time)
        m = getMusicArr(m, 0)
    	# m에 해당하는 문자열을 musicArr가 포함하는지 확인
        if checkSubArray(m, musicArr):
            if answerTime < time:
                answer = title
                answerTime = time
    return answer

 

 

 

- 시도해본 풀이 : 

50 퍼센트까지 맞았던 문제다

 

시간에 따른 멜로디를 확보한 뒤, m을 포함하는 걸 모두 담아서 조건에 맞춰서 반환하려했다 

def solution(m, musicinfos):
    
    musics = []
    for i in range(len(musicinfos)):
        start, end, title, melody = musicinfos[i].split(',')
        
        n = len(melody)
        s1, s2 = int(start[0 : 2]), int(start[3 : 5])
        e1, e2 = int(end[0 : 2]), int(end[3 : 5])
        hour, minute = e1 - s1, e2 - s2
        time = hour * 60 + minute
        
        temp = ''
        if time // n == 0:
            temp = melody[:time]
        else:
            temp = melody * (time // n ) + melody[: time % n]
            
        musics.append([title, temp])
        
    answer = []
    length = len(m)
    for i in range(len(musics)):
        if m in musics[i][1] and m + '#' not in musics[i][1]:           
            # 재생 시간, 재생 순서, 곡 제목 
            answer.append([length, i, musics[i][0]])

    if len(answer) == 0:
        return "(None)"
    elif len(answer) == 1:
        return answer[0][2]
    else :     
        result = []
        answer.sort(key = lambda x : x[0], reverse = True)
        temp = answer[0][0]
        for i in range(len(answer)):
            if answer[i][0] == temp:
                result.append([answer[i][1], answer[i][2]])
                
        result.sort()
        return result[0][1]

 

 

+ Recent posts