[1일 3알고] (해시/스택,큐) 베스트앨범,기능개발, 프린터

2400·2022년 3월 30일
0

베스트 앨범

1차 답안


def solution(genres, plays):
    dic = {}
    answer = []
    
    for i,genre in enumerate(genres):
        if genre in dic:
            dic[genre] = dic[genre] + plays[i] # 장르별 총 플레이 횟수 계산
        else:
            dic[genre] = plays[i]
    
    sorted_genre = sorted(list(dic.items()), 
                              reverse=True, 
                              key=lambda item: item[1])
    
    d = {}
    for elem in sorted_genre:
        if elem[0] in d:
            d[elem[0]].append(elem[1])
        else:
            d[elem[0]] = elem[1]
            
    sorted_genre = d
    # 플레이 수 기준 장르 순서 확인했음.
    # 이제 장르별, 1등, 2등 해쉬맵을 만들 것
    dic_1st_value = {}
    dic_1st_index = {}
    dic_2nd_value = {}
    dic_2nd_index = {}
    
    for i, genre in enumerate(genres):
        if genre in dic_1st_value: # 갱신 케이스
        
            # 2위도 존재하는 경우
            if genre in dic_2nd_value: 
                if plays[i] > dic_1st_value[genre]: # 신규 1위 등장
                    dic_2nd_value[genre] = dic_1st_value[genre] # 기존 1위는 2위로
                    dic_2nd_index[genre] = dic_1st_index[genre]

                    dic_1st_value[genre] = plays[i] # 1위 갱신
                    dic_1st_index[genre] = i

                elif plays[i] == dic_1st_value[genre]: # 공동 1위지만 id가 낮은 경우
                    dic_2nd_value[genre] = dic_1st_value[genre]
                    dic_2nd_index[genre] = i # 2위 갱신

                elif plays[i] > dic_2nd_value[genre]: # 신규 2위 등장
                    dic_2nd_value[genre] = plays[i] # 2위 갱신
                    dic_2nd_index[genre] = i
                    
            # 1위에는 존재하지만 2위는 없는 경우        
            elif genre not in dic_2nd_value: 
            
                # 1위와 비교 후 더 높은 경우
                if plays[i] > dic_1st_value[genre]: # 신규 1위 등장
                    dic_2nd_value[genre] = dic_1st_value[genre] # 기존 1위는 2위로
                    dic_2nd_index[genre] = dic_1st_index[genre]

                    dic_1st_value[genre] = plays[i] # 1위 갱신
                    dic_1st_index[genre] = i

                elif plays[i] == dic_1st_value[genre]: # 공동 1위지만 id가 낮은 경우
                    dic_2nd_value[genre] = dic_1st_value[genre]
                    dic_2nd_index[genre] = i # 2위 갱신

                else: # 1위보다 낮기 때문에 2위로 등극
                    dic_2nd_value[genre] = plays[i]
                    dic_2nd_index[genre] = i # 2위 갱신
                
        elif genre not in dic_1st_value: # 신규 케이스
            dic_1st_value[genre] = plays[i] # 1위 갱신
            dic_1st_index[genre] = i
            
    for genre in sorted_genre.keys():
        try:
            answer.append(dic_1st_index[genre]) # 1위
            answer.append(dic_2nd_index[genre]) # 2위
        except:
            pass # 위 try 진행하면서 1위는 기록되었음
            
    return answer

아무리 생각해도 dict를 활용하지 못한 답안 같다;;

기능개발

  1. 남은 일의 양 / 속도 를 계산하고 나서 올림을 통해 남은 일수를 계산
  2. 남은 일수에서 이전 프로세스 가운데 가장 오래 걸리는 일수(temp_high)를 구한다
  3. temp_high 가 올라가는 시점이 쌓아놓은(k) 프로젝트 수를 answer에 추가하고
  4. 마지막으로 마지막 프로젝트에 도달하면 이때까지 쌓아놓은 프로젝트 수를 answer에 추가한다.
import math

def solution(progresses, speeds):
    
    left_pg = []
    left_days = []
    
    i = 0
    for progress, speed in (zip(progresses,speeds)):
        
        left_pg.append(100-progress)
        left_days.append(math.ceil(left_pg[i]/speed))
        i = i+1
        
    answer = []
    k = 0
    temp_high = left_days[0]
    print(left_days)
    for i, left_day in enumerate(left_days):
        if left_day > temp_high:
            answer.append(k)
            k = 1
            temp_high = left_day # update
            
            if i==len(left_days)-1:
                answer.append(k)
            
        elif left_day <= temp_high:
            k = k+1
            
            if i==len(left_days)-1:
                answer.append(k)

    return answer

프린터

  1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다.
  2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에 넣습니다.
  3. 그렇지 않으면 J를 인쇄합니다.

자꾸 pandas df 를 상상하며 문제를 푼다;;

def solution(priorities, location):
    
    
    count = 0
    new_prior = []
    
    for i, priority in (enumerate(priorities)):
        new_prior.append([i,priority])
    
    flag = 0
    while flag ==0:
        # print(count, new_prior)
        temp = []
        temp.append(new_prior[0]) # 인쇄 예정 목록에 추가
        new_prior.remove(temp[0]) # 대기목록에서는 삭제
        

        # 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하는지
        temp_high = 0
        for i in range(len(new_prior)):
            if new_prior[i][1] >= temp_high:
                temp_high = new_prior[i][1]
            
            
            
        # 더 큰놈이 있다면
        if temp[0][1] < temp_high:
            new_prior.append(temp[0]) # 기존에 뽑아놨던 놈을 맨 뒤로 보냄
        # 더 큰놈이 없다면
        else : 
            count += 1 # 한 놈 빼내고 
            # 만약 출력 예정인 놈의 인덱스랑 로케이션의 숫자랑 일치한다면
            if temp[0][0] == location:
                flag = 1

    answer = count   

    return answer
profile
공부용 혹은 정리용 혹은 개인저장용

0개의 댓글