💻 개인공부 💻/알고리즘

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

공대생 배기웅 2020. 3. 10. 23:35
반응형

https://programmers.co.kr/learn/courses/30/lessons/42576

문제 출처 입니다.

 

코딩테스트 연습 - 완주하지 못한 선수 | 프로그래머스

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요. 제한사항 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다. completion의 길이는 partic

programmers.co.kr

완주하지 못한 선수 문제

 

문제를 풀기 전 간단한 개념 몇 개 만 공부해보도록 하겠습니다.

 

1. Arrays.sort()메소드

 

Arrays.sort()는 난잡하게 나열되어 있는 배열을 어떤 기준에 맞게 "정렬"하게 만들어 주는 메소드 입니다.

 

예를 한 번 보도록 하겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public class ArraySolution {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        int [] num= {12,45,7,23,61,34};
        Arrays.sort(num);
        
        for(int i=0; i<num.length; i++) {
            System.out.println(num[i]);
        }
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs이

<실행결과>

이 코드 소스를 보면 Arrays.sort메소드에 num이라는 배열을 넣음으로서 "정렬"하여 주었습니다.

그렇다면 숫자만 배열을 시킬 수 있을까요?

아래의 소스도 한번 보겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
 
public class ArraySolution{
    public static void main(String [] args) {
        String [] people = {"candy""banana""apple""dragon""frog""elephant"};
        Arrays.sort(people);
        
        for(int i=0; i<people.length; i++) {
            System.out.println(people[i]);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<실행결과>

 

 

이 소스코드는 알파벳 순으로 정렬을 하였습니다.

 

다음 예시도 보겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
 
public class ArraySolution{
    public static void main(String [] args) {
        String [] people = {"저의""블로그""구독과""좋아요""많은사랑""부탁드리겠습니당"};
        Arrays.sort(people);
        
        for(int i=0; i<people.length; i++) {
            System.out.println(people[i]);
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<실행결과>

이런 식으로 한글도 정렬이 됩니다.

 

 

2. equals() 메소드

 

다음은 equals()메소드 입니다. equals()는 단어의 이름에서 알 수 있다시피 내용이 같은지를 검사하는 메소드 입니

예제를 하나 들어보도록 하겠습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package March_10;
 
public class EqualSolution {
    
    static boolean Correction(String a, String b) {
        if(a.equals(b)) 
            return true;
        return false;
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a="구독";
        String b="사랑";
//=================================================        
        String c="좋아요";
        String d="좋아요";
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<실행결과>

이런 식으로 내용이 같은지를 판별해 줍니다. 

 

 

 

<완주하지 못한 선수 문제 풀이>

 

문제 해결을 위한 사고의 흐름을 분석해 봅시다.

 

1. 우선 어떤 선수가 완주하지 못하였는지 확인을 하기 위해서는 participant배열과 completion배열을 비교 해야 할 것 같습니다.

 

2. 비교라... 효율적으로 비교를 하기 위해선 어떻게 하는 것이 좋을까요?

 

3. participant 배열과 completion배열을 처음부터 차례대로 비교를 해보면 될 것 같군요.

 

4. 그렇다면 각각의 배열을 기준에 맞추어 "정렬"하고, 처음부터 비교를 해보면 될 것 같습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
 
class Solution{
    public String solution(String [] participant, String []completion){
        Arrays.sort(completion);
        int i;
        for(i=0; i<completion.length; i++){
            if(!participant[i].equals(completion[i])){
                return participant[i];
            }
        }
        return participant[i];
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
728x90
반응형