coding test

[파이썬, Java] 인사고과

잔망루피 2023. 4. 12. 19:26
반응형

🚩 나의 풀이

import java.util.*;

class Solution {
    public int solution(int[][] scores) {
        int answer = 1;
        int s1 = scores[0][0];
        int s2 = scores[0][1];
        int wanho_score = s1 + s2;
        int tmp = 0;
        
        Arrays.sort(scores, new Comparator<int[]>(){
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] < o2[0])
                    return 1;
                else if (o1[0] == o2[0]) {
                    if (o1[1] > o2[1])
                        return 1;
                    else
                        return -1;
                }
                else return -1;
            }
        });

        for(int i = 0; i < scores.length; i++){
            if (s1 < scores[i][0] && s2 < scores[i][1]){
                return -1;
            }
            if (tmp <= scores[i][1]){
                if (wanho_score < scores[i][0] + scores[i][1]){
                    answer += 1;
                }
                tmp = scores[i][1];
            }
        }
        return answer;
    }
}

정렬을 위해서 Comparator 사용

int 배열의 첫 번째 열은 내림차순, 두 번째 열은 오름차순 정렬

tmp 변수는 첫 번째 열의 최대값

if (tmp <= scores[i][1])은 인센티브를 받지 못하는 사원을 필터링

 

 

# 실패
def solution(scores):
    answer = 0
    dic = {}
    first = sorted(scores, key = lambda x : x[0])[-1]
    second = sorted(scores, key = lambda x : x[1])[-1]
    
    for idx, score in enumerate(scores) :
        f, s = score
        if score == first or score == second :
            dic[idx] = sum(score)
            continue
        if f < first[0] and s < first[1] :
            if idx == 0 :
                return -1
            continue
        if s < second[1] and f < second[0] :
            if idx == 0 :
                return -1
        dic[idx] = sum(score)
        
    sorted_dic = sorted(dic, key=lambda x : dic[x], reverse = True)
    
    return sorted_dic.index(0) + 1       # 석차

반은 맞고 반은 틀렸다. 🥲🥲🥲🥲

first는 근무 태도 점수가 가장 높은 사람

second는 동료 평가 점수가 가장 높은 사람

딕셔너리를 정렬해서 인덱스 0의 위치를 구했다.

 

🥞 다른 사람 풀이

# https://sundryy.tistory.com/109
def solution(scores):
    answer = 1
    
    target = scores[0]
    target_score = sum(scores[0])
    scores.sort(key=lambda x : (-x[0], x[1]))
    
    threshold = 0
    for score in scores :
        if target[0] < score[0] and target[1] < score[1] :
            return -1
        if threshold <= score[1] :
            if target_score < score[0] + score[1] :
                answer += 1 
            threshold = score[1]
    return answer

O(N)

근무 태도 점수는 내림차순, 동료 평가 점수는 오름차순 정렬

테케의 경우 scores는 [[3, 2], [3, 2], [2, 1], [2, 2], [1, 4]]로 정렬된다.

근무 태도, 동료 평가 점수 둘다 내림차순 정렬해야하는 줄...😂

인센티브를 못 받는 사람은 거르려고 동료 평가 점수는 오름차순

 

 

 

문제 출처 👇

https://school.programmers.co.kr/learn/courses/30/lessons/152995

 

프로그래머스

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

programmers.co.kr

 

반응형

'coding test' 카테고리의 다른 글

[파이썬, Java] 쿠키 구입  (0) 2023.04.12
[파이썬, Java] 호텔 방 배정  (0) 2023.04.11
[파이썬] 부대복귀  (0) 2023.04.11
[파이썬] 호텔 대실  (0) 2023.04.10
[파이썬] 과제 진행하기  (0) 2023.04.05