Python/Programmers

[프로그래머스] 광물 캐기 LV2

Magin 2024. 2. 7. 14:32
728x90

문제)

 

프로그래머스

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

programmers.co.kr

알고리즘)

1. 총 곡갱이 수광물을 5개씩 분할(b_split 함수) #추가적으로 총 곡갱이 수로 수확가능한 광물까지 가지치기

2. 수확 가능범위 안에 있는 광물등급 갯수 확인

3. 내림차순으로 피로도가 가장 요구되는 구역을 다이아 -> 철 -> 돌 곡갱이 순으로 캐기

4. 총 피로도 누적합 출력

코드)

def solution(picks, minerals):
    result = 0
    total_p = sum(picks)
    bowl = b_split(minerals, total_p)
    bill = []
    
    for q in bowl:
        a, b, c = q.count('diamond'), q.count('iron'), q.count('stone')
        bill.append([a, b, c])
    
    bill.sort(key=lambda x:(x[0], x[1], x[2]), reverse=True)
    
    if bill[picks[0]:] != None:
        for d in bill[:picks[0]]:
            result += sum(d)
        for i in bill[picks[0]:]:
            if picks[1] !=0:
                picks[1] -= 1
                result += i[0]*5 + i[1] + i[2]
                continue
                
            if picks[2] != 0:
                picks[2] -= 1
                result += i[0]*25 + i[1]*5 + i[2] 
    return result

def b_split(minerals, total_p): #전체 광물을 5개씩 나누기 
    bowl = [minerals[i:i+5] for i in range(0, len(minerals), 5)]
    bowl = bowl[:total_p]
    return bowl
728x90