[코딩테스트] 신입사원

시나브로·2021년 12월 12일
0

코딩테스트

목록 보기
34/34
post-thumbnail

문제


신입사원 문제 바로가기



풀이

지원자들을 첫번째 기준(서류 순위)에 맞춰 정렬을 진행한 뒤, 순차 반복을 통해 이전 합격자와 두번째 기준(면접 순위)을 비교하며 합격자 선별

  • 탈락자는 기준에서 제외



제출 코드(JAVA)


코드 제출

package 백준.sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

//https://www.acmicpc.net/problem/1946
public class 신입사원_1946 {

//  다른 사원들과 비교하여 둘 다 하위일 때, 탈락
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int testCaseCnt = Integer.parseInt(br.readLine());
        
        PriorityQueue<int[]> applicantScoreQueue = new PriorityQueue<>((o1, o2) -> o1[0] - o2[0]);

        int[] scoreArray;


        for(int i = 0; i < testCaseCnt; i++) {
            int successfulApplicantCnt = 0;
            int applicantCnt = Integer.parseInt(br.readLine());
            for (int i1 = 0; i1 < applicantCnt; i1++) {
                StringTokenizer st = new StringTokenizer(br.readLine());
                scoreArray = new int[2];
                scoreArray[0] = Integer.parseInt(st.nextToken());
                scoreArray[1] = Integer.parseInt(st.nextToken());
                applicantScoreQueue.add(scoreArray);
            }
            successfulApplicantCnt = solution(applicantScoreQueue);
            System.out.println(successfulApplicantCnt);
        }
    }

    public static int solution(PriorityQueue<int[]> applicantScoreQueue){
        int successfulApplicantCnt = 1;
        int[] previousScoreArray;
        previousScoreArray = applicantScoreQueue.poll();

        while (!applicantScoreQueue.isEmpty()) {
            int[] currentScoreArray = applicantScoreQueue.poll();
            if (currentScoreArray[1] < previousScoreArray[1]) {
                successfulApplicantCnt++;
                previousScoreArray = currentScoreArray;
            }
        }

        return successfulApplicantCnt;
    }
}

profile
Be More!

0개의 댓글