본문 바로가기

프로그래머스 - n + 1 카드게임 본문

알고리즘

프로그래머스 - n + 1 카드게임

Seongjun_You 2024. 1. 10. 22:01

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

 

프로그래머스

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

programmers.co.kr

 

2024 KAKAO WINTER INTERNSHIP 문제

 

1. 나의 덱에 있는 요소들로 라운드 진출 가능한 경우

2. 나의 덱 하나와 coin 하나 써서 라운드 진출 가능한 경우

3. coin 두 개 써서 라운드 진출 가능한 경우

4. 모두 통과 못하면 최대 라운드이다.

 

라운드마다 두장의 카드는  바로 temp_card에 넣어두고

필요할 때 coin을 쓰고 temp_card에서 빼가기로 했다.

 

stage값이 최대치 이상으로 넘어가는 경우가 있는데

이것 때문에 테스트케이스 18번 통과를 못했다.

그래서 조건문 넣어서 통과해 주었다.

 

from collections import deque
def solution(coin, cards):
    target = len(cards) + 1
    my_card = cards[:len(cards)//3]
    cards = deque(cards[len(cards)//3:])
    temp_card = []
    stage = 0

    while True:
        if len(cards) >= 2:
            temp_card.append(cards.popleft())
            temp_card.append(cards.popleft())
        stage += 1
        continue_flag = 0
        #1
        for i in my_card:
            if target - i in my_card:
                my_card.remove(i)
                my_card.remove(target - i)
                continue_flag = 1
                break
        #2
        if continue_flag == 0 and coin > 0:
            for i in temp_card:
                if target - i in my_card:
                    temp_card.remove(i)
                    my_card.remove(target - i)
                    continue_flag = 1
                    coin -= 1
                    break
        #3
        if continue_flag == 0 and coin > 1:
            for i in temp_card:
                if target - i in temp_card:
                    temp_card.remove(i)
                    temp_card.remove(target - i)
                    continue_flag = 1
                    coin -= 2
                    break

        if continue_flag:
            continue
        break

    if stage > (((target-1) - ((target-1)//3))//2)+1 :
        stage = (((target-1) - ((target-1)//3))//2)+1
    return stage

Comments