728x90
https://programmers.co.kr/learn/courses/30/lessons/42576
function solution(participant, completion) {
var answer = '';
// 배열은 일단 정렬을 해준다
let particiArr = participant.sort();
let compleArr = completion.sort();
// 전체 participant 배열을 돌면서 값이 없는걸 발견하면
for(let i=0; i<particiArr.length; ++i){
if(particiArr[i] !== compleArr[i]){
//그 값이 정답
answer = particiArr[i];
// 정답내보내기
return answer;
}
}
}
가장 기본적으로 풀어냈다.
정답코드는 해시를 이용해 푸는게 권장이기 때문에
해시로 푼걸 가져왔다.
function solution(participant, completion) {
const myMap = new Map();
for ( const participant of participants){
if(!myMap.get(participant)){
myMap.set(participant, 1);
}else{
myMap.set(participant, myMap.get(participant)+1);
}
}
for(const completion of completions){
if(myMap.get(completion)){
myMap.set(completion, myMap.get(completion)-1);
}
}
for(const participant of participants){
if(myMap.get(participant) && myMap.get(participant) >=1 ){
answer = participant;
}
}
}
728x90
'알고리즘' 카테고리의 다른 글
프로그래머스 Level 1 - 정수 제곱근 판별 (Javascript) (0) | 2022.05.15 |
---|---|
프로그래머스 Level 1 - 콜라츠 추측 (Javascript) (0) | 2022.05.15 |
프로그래머스 Level 1 - 행렬의 덧셈 (Javascript) (0) | 2022.05.14 |
프로그래머스 Level 1 - 2016년 (Javascript) (0) | 2022.05.14 |
(중요! map관련)프로그래머스 Level 1 - 나누어 떨어지는 숫자 배열 (Javascript) (0) | 2022.05.14 |