본문 바로가기
코딩테스트 파이썬/파이썬 프로그래머스 2단계

할인행사

by 세용용용용 2023. 7. 8.

나의 코드

import copy
def solution(want, number, discount):
    answer = 0
   
    #일단 딕셔너리를 만들어주자
    dict = {}
    for i in range(len(want)):
        dict[want[i]] = number[i]
   
    index = 0
    #10일치 만큼만 반복문
    while index <= len(discount)-10:

        #deepcopy를 안쓰게되면 참조하게 되서 dict도 같이 수정됨
        new_dict = copy.deepcopy(dict)

        #10일치 만큼 for문 돌리면서 dict값 뺴주기
        for i in discount[index:index+10]:
            if i not in new_dict:
                break
            new_dict[i]-=1
            if new_dict[i] == 0:
                del new_dict[i]
       
        #최종적으로 원하는 것을 다 구입했을때 answer증감
        if len(new_dict) == 0:
            answer+=1
       
        #또 다음날부터 10일차 를 구하기 위해 index 증감
        index+=1

    print(answer)
    return answer
solution(["banana", "apple", "rice", "pork", "pot"],
         [3, 2, 2, 2, 1],
         ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"])

 

'코딩테스트 파이썬 > 파이썬 프로그래머스 2단계' 카테고리의 다른 글

프로세스  (0) 2023.07.08
기능개발  (0) 2023.07.08
튜플  (0) 2023.07.07
의상  (0) 2023.07.07
괄호 변환  (0) 2023.06.13