본문 바로가기

코딩테스트

[프로그래머스] 카드 뭉치

문제 설명

문제 풀이 : python

def solution(cards1, cards2, goal):
    i = 0; j= 0
    for n in range(len(goal)):
        if i<len(cards1) and cards1[i] == goal[n]:
            i += 1
            n += 1
        elif j<len(cards2) and cards2[j] == goal[n]:
            j += 1
            n += 1
        else:
            return "No"
    return "Yes"

다른 사람의 풀이

def solution(cards1, cards2, goal):
    for g in goal:
        if len(cards1) > 0 and g == cards1[0]:
            cards1.pop(0)       
        elif len(cards2) >0 and g == cards2[0]:
            cards2.pop(0)
        else:
            return "No"
    return "Yes"

pop으로 깔끔하게 해결.

문제풀이 : java

class Solution {
    public String solution(String[] cards1, String[] cards2, String[] goal) {
        int index1 = 0, index2 = 0;
        for (String g : goal) {
            if (index1 < cards1.length && g.equals(cards1[index1])) {
                index1++;
            }
            else if (index2 < cards2.length && g.equals(cards2[index2])) {
                index2++;
            }
            else
                return "No";
        }
        return "Yes";
    }
}

'코딩테스트' 카테고리의 다른 글

[프로그래머스] 바탕화면 정리 python, java  (0) 2024.05.22
[이코테] Chap04 구현  (0) 2024.04.09
[이코테] 그리디 알고리즘  (0) 2024.04.08
[Python] 공원 산책  (0) 2024.03.12
[Python] 가장 가까운 같은 글자  (0) 2024.03.07