코딩테스트-알고리즘/프로그래머스

[프로그래머스/Lv.1] 카드 뭉치

닉네임생각즁 2023. 12. 26. 23:19

 

https://school.programmers.co.kr/learn/courses/30/lessons/159994

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.*;

class Solution {
    public String solution(String[] cards1, String[] cards2, String[] goal) {
        ArrayList<String> c1List = new ArrayList<>(Arrays.asList(cards1));
        ArrayList<String> c2List = new ArrayList<>(Arrays.asList(cards2));
        
        for(int i=0; i<goal.length; i++) {
            if(c1List.size()>0 && goal[i].equals(c1List.get(0))) { 
                c1List.remove(0);
                if (i==goal.length-1) break;
                
            } else if(c2List.size()>0 && goal[i].equals(c2List.get(0))) { 
                c2List.remove(0);
                if (i==goal.length-1) break;
                
            } else return "No";
        }        
        
        return "Yes";
    }
}

 

cards1 or cards2 가장 앞에 와있는 문자와 goal배열을 순서대로 비교하면서 같은게 있다면 제거해주고 또 가장 앞에 있는 문자와 비교해주는 방식을 계속 반복하기 위해 cards1과 cards2를 리스트에 넣어줘서 이용했다

큐를 빨리 익혀서 큐로도 풀어보고싶다