-
[TIL] Algorithm 9Develope/Algorithm 2020. 7. 22. 20:34
◎ 프로그래머스 - 완주하지 못한 선수
◎ 문제
코딩테스트 연습 - 완주하지 못한 선수
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수��
programmers.co.kr
◎ 정답
function solution(participant, completion) { participant.sort(); completion.sort(); for (let i = 0; i < participant.length; i++) { if (participant[i] !== completion[i]) { return participant[i]; } } }
◎ 해설
- participant와 competion의 배열의 값을 어떻게 비교할까 고민을 많이 했었다.
sort() 함수를 생각하기 전 까지 많은 시간을 투자했는데 sort() 함수로 정렬을 하고
for문으로 participant[i] 와 competion[i] 가 같지 않으면 participant[i]을 return 시키니까 해결되었다.
level 1이고 쉬울 줄 알았는데... 시간을 좀 투자하였다.
'Develope > Algorithm' 카테고리의 다른 글
[TIL] Algorithm 11 (0) 2020.07.27 [TIL] Algorithm 10 (0) 2020.07.27 [TIL] Algorithm 8 (0) 2020.07.22 [TIL] Algorithm 7 (0) 2020.07.22 [TIL] Algorithm 6 (0) 2020.07.20