알고리즘
프로그래머스 카드뭉치 자바스크립트
minprogramming
2023. 8. 20. 10:25
<문제>
https://school.programmers.co.kr/learn/courses/30/lessons/159994#
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
<나의 풀이>
//cards1에서 원소를 하나뽑는다.
//cards2에서 원소를 하나뽑는다.
//만약에 cards1에서 원소가 goal에서의 원소와 같을 때 다음 과정을 수행한다.
//goal의 원소의 값을 하나 제거한다.
//cards2와는 비교하지 않는다.
//만약에 cards2에서 원소가 goal에서의 원소와 같을 때 다음 과정을 수행한다.
//goal의 원소의 값을 하나 제거한다.
//cards1와는 비교하지 않는다.
function solution(cards1, cards2, goal) {
let card1Idx = 0;
let card2Idx = 0;
let goalIdx = 0;
while(goalIdx < goal.length) {
if (cards1[card1Idx] === goal[goalIdx]) {
card1Idx += 1;
goalIdx += 1
}
else if (cards2[card2Idx] === goal[goalIdx]) {
card2Idx += 1;
goalIdx += 1;
}
else {
return "No";
}
}
return "Yes";
}
<다른 사람의 풀이>
function solution(cards1, cards2, goal) {
for(const s of goal) {
if(cards1[0] == s) {
cards1.shift();
} else if(cards2[0] == s) {
cards2.shift();
} else {
return "No"
}
}
return "Yes";
}
<참고자료>
shift()
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
Array.prototype.shift() - JavaScript | MDN
shift() 메서드는 배열에서 첫 번째 요소를 제거하고, 제거된 요소를 반환합니다. 이 메서드는 배열의 길이를 변하게 합니다.
developer.mozilla.org