동캄의 코딩도장

프로그래머스 level2 [할인 행사] 파이썬 본문

코테/프로그래머스

프로그래머스 level2 [할인 행사] 파이썬

동 캄 2023. 4. 5. 17:37

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

 

프로그래머스

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

programmers.co.kr

# 프로그래머스 level2 할인 행사
def solution(want, number, discount):
    answer = 0
    dic=dict()
    l=[]
    
    
    for i in range(len(want)): #초기 값 세팅
        dic[want[i]]=number[i]
    
    
    for i in range(10): # 첫날~10일차까지 할인품목 체크 
        inStuff=discount[i]
        if inStuff in want:
            dic[inStuff]-=1
        l.append(inStuff)
    
    for wantStuff in want: #모두 할인 받는지 체크
        if dic[wantStuff]>0:
            break
    else:
        answer+=1
    
    for i in range(10,len(discount)): # N일차~N+10일차까지 할인품목 체크
        outStuff=l.pop(0)
        if outStuff in want:
            dic[outStuff]+=1
        inStuff=discount[i]
        l.append(inStuff)
        if inStuff in want:
            dic[inStuff]-=1
        for wantStuff in want: #모두 할인 받는지 체크
            if dic[wantStuff]>0:
                break
        else:
            answer+=1
    return answer

딕셔너리를 이용하여 해결하였다.

want의 길이가 10이 넘지 않으므로, "for 물건 in want "구문을 사용하였다.