Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 카드 뭉치 #120

Closed
hwangJi-dev opened this issue Feb 27, 2023 · 0 comments
Closed

[Algorithm] 카드 뭉치 #120

hwangJi-dev opened this issue Feb 27, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Feb 27, 2023

💬 문제

카드 뭉치


💬 Idea

  1. cards1, cards2 stack을 만든다.
  2. cards1, cards2의 원소를 차례로 target1,2로 뽑아둔 후 goal을 돌면서 해당 target에 일치할 경우 일치하는 카드 뭉치의 원소를 뽑고 다음 target으로 만들어준다
    • 이 때 해당 target과 g가 일치하지 않는다면 카드 뭉치에 없는 카드로 만들어졌거나, 카드 순서가 일치하지 않는다는 것이므로 “No”를 리턴한 후 종료한다.

💬 풀이

func solution(cards1:[String], cards2:[String], goal:[String]) -> String {
    var cards1 = cards1.reversed().map({ String($0) })
    var cards2 = cards2.reversed().map({ String($0) })
    var target1 = cards1.popLast()
    var target2 = cards2.popLast()
    
    for g in goal {
        if g == target1 {
            target1 = cards1.popLast()
        } else if g == target2 {
            target2 = cards2.popLast()
        } else {
            return "No"
        }
    }
    
    return "Yes"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant