코딩테스트/프로그래머스

[프로그래머스] 인사고과

박소민 2025. 7. 26. 22:23
인사고과

 

  • 첫번째 풀이 - fail
    • 이 코드면 완호가 [2,2]고 나머지가 [4,1],[3,3] 이었을 때,
    • 첫번째 값인 [4,1]은 완호를 거르지 못하지만 [3,3]은 완호보다 모두 크므로 완호를 제외시킬 수 있음
      • → sort하고 첫번째값이 무조건 완호보다 둘 다 크지 않을 수 있기 때문에 오류
def solution(scores):
    answer = 1
    if len(scores)==1:
        return answer
    
    wanho=scores[0]
    wanho_val=sum(wanho)
    new_scores=sorted(scores[1:],reverse=True)
    
    max_a,max_b=new_scores[0]
    if max_a>wanho[0] and max_b>wanho[1]:
        return 0
    
    for a,b in new_scores:
        if max_a>a and max_b>b:
            continue
        if a+b>wanho_val:
            answer+=1
    
    return answer

 

  • 두번째 풀이 - success 
    • key=lambda x:(-x[0],x[1]) 로 정렬하면 
      • 어차피 (a,b) 에서 뒷값들은 a가 앞에 a보다 작거나 같을 수밖에 없음
      • 그러므로 b가 더 큰값이 있으면 그걸로 비교값을 바꿈
    • 걸러지는 값이어도 완호와의 대소비교는 되어야하기 때문에
      • 완호가 걸러지는지가 가장 먼저 체크되어야함
def solution(scores):
    answer = 1
    if len(scores)==1:
        return answer
    
    wanho=scores[0]
    wanho_val=sum(wanho)
    
    new_scores=scores[1:]
    new_scores.sort(key=lambda x:(-x[0],x[1]))
    max_a,max_b=new_scores[0]
    
    for a,b in new_scores:
        # 걸러지는 값이어도 완호와의 대소비교는 되어야하기 때문에 가장 먼저 체크
        if a>wanho[0] and b>wanho[1]:
            return -1
        if max_a>a and max_b>b:
            continue
        
        if a+b>wanho_val:
            answer+=1
            
        if b>max_b:
            max_a=a
            max_b=b

    
    return answer