IT/알고리즘 문제

프로그래머스 - 완주하지 못한 선수

금마s 2020. 11. 12. 16:32

# python 3

def solution(participant, completion):
    answer = ''
    participant.sort()
    completion.sort()
    for i in range(0, len(completion)):
        if participant[i] != completion[i]:
            answer = participant[i]
            break
            
    if answer == '':
        answer = participant[len(completion)]
    return answer

 

# JAVA

import java.util.Arrays;
class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        Arrays.sort(participant);
        Arrays.sort(completion);
        
        for(int i=0; i<completion.length; i++){
            String tmp = participant[i];
            if(!tmp.equals(completion[i])){
                answer = tmp;
                break;
            }
        }
        
        if(answer == ""){
            return participant[participant.length-1];
        } else {
            return answer;
        }
    }
}

 

 

 

 

728x90