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

Bong2·2024년 5월 21일
0

알고리즘

목록 보기
25/63

문제 - 인사고과


완호는 scores[0]의 점수를 가지고 있다.

문제 접근

  1. 인센티브를 받지 못하는 대상을 제외시킨다.
    • 근무태도 점수는 내림차순, 동료평가 점수는 오름차순으로 정렬
    • 정렬된 맨 처음값을 기준으로 제외대상을 탐색
    • 제외대상에 완호가 있는 경우 -1을 출력
  2. 인센티브를 받는 대상을 근무태도와 동료평가 점수의 합산으로 내림차순 정렬
  3. 완호보다 높은 점수를 가지고 있는 인원을 센 뒤 결과를 출력
import java.util.*;

class Solution {
    public int solution(int[][] scores) {
        int answer = 1;
        int wanho[] = scores[0];
        
        //인센티브 받을 수 없는 대상 제외
        ArrayList<int []> ary = new ArrayList<>();
        Arrays.sort(scores, (x,y) -> {
            if(x[0] == y[0])
                return x[1]-y[1];
            return y[0] - x[0];
        } );
        int maxScore = scores[0][1];
        ary.add(scores[0]);
        for(int i =1; i< scores.length;i++)
        {
            if(scores[i][1] < maxScore)
            {
                if(scores[i][0] == wanho[0] && scores[i][1] == wanho[1])
                    return -1;
            }
            else{
                ary.add(scores[i]);
                maxScore = scores[i][1];
            }
        }
        
        //두 점수의 합산으로 내림차순 정렬
        Collections.sort(ary,new Comparator<int []>(){
            @Override
            public int compare(int a[],int b[])
            {
                int as = a[0]+a[1];
                int bs = b[0]+b[1];
                
                return bs - as;
            }
        });
        
        int sum = wanho[0] + wanho[1];
        
        for(int i=0;i<ary.size();i++)
        {
            int temp = ary.get(i)[0] + ary.get(i)[1];
            if(temp > sum)
                answer++;
            else 
                break;
        }
        
        return answer;
    }
    
}
profile
자바 백엔드 개발자로 성장하자

0개의 댓글