본문 바로가기

개발과/이 문제 알고리쯤?

완주하지 못한 선수 : java

//완주하지 못한 선수
//1. 참가자에도 있고 완주자에도 있는 사람들을 비교해서 빼준다. 참가자 배열에 남은 사람이 완주x : equals()
//2. 큰쪽에서 작은 쪽을 비교해야 큰쪽에 남는 사람이 있다.


import java.util.Arrays;

class Solution {
public String solution(String[] participant, String[] completion) {
Arrays.sort(participant);
Arrays.sort(completion);
//Arrays.sort(); 오름차순으로 배열을 정리하는
// Arrays.sort(Arrays, Collections.reverseOrder()); 내림차순으로 배열을 정리하는
int i; // int i를 밖으로 뺀 이유는 무엇인가...? 안에 넣었을 때는 안되고 빼면 되는 이유는? : 동명이인이 있다면 출력의 오류가 생긴다!
for (i = 0; i < completion.length; i++) {
if (!participant[i].equals(completion[i])) {
return participant[i];
}
}return participant[i];
}
}
//이 문제는 다시 한번 파봐야 겠다...검색 및 숙달이 더 필요함...for 문으로 돌려서 해보자!
//for 를 통한 풀이
class Solution{
public String solution(String[] participant, String[] completion) {
String answer = "";

for (int i = 0; i < completion.length; i++) {
String comp = completion[i];
for (int j = 0; j < participant.length; j++) {
String part = participant[j];
if (comp.equals(part)) { // 작은 쪽에서 큰쪽을 비교하면 남는 것이 있다.
participant[j] = "";
break; // comp, part 차이점을 찾았고 더 이상 반복 할 필요가 없다.
}
}
}
for (String s : participant) { //향상된 for
if (!s.equals("")) {
answer=s;
}
}
return answer;
}
}
//시간 초과라고 나온다... 이유가 뭘까?

https://github.com/bbakzi/coding_prac.git

 

GitHub - bbakzi/coding_prac

Contribute to bbakzi/coding_prac development by creating an account on GitHub.

github.com

제 git hub 주소 입니다아~ㅎㅎㅎ