Heeto
article thumbnail
링크 : https://school.programmers.co.kr/learn/courses/30/lessons/150368
 

프로그래머스

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

programmers.co.kr

 

코드


from itertools import product

def getPrice(emoticons,ratio,userRate):
    return sum([(emoticons[i]*(100-ratio[i])//100) for i in range(len(emoticons)) if ratio[i] >= userRate])

def solution(users, emoticons):
    answer = []
    for ratio in product([10,20,30,40],repeat = len(emoticons)):
        membership,total = 0,0
        for user in users:
            if (res:=getPrice(emoticons,ratio,user[0])) >= user[1]:
                membership += 1
                continue
            total += res
        answer.append([membership,total])
    return sorted(answer,reverse = True)[0]

 

풀이


문제를 보고 고민해보았다면 아마도 완전탐색을 고려해보았을 것이다.

이모티콘의 개수는 7개, 할인률은 4개이기 때문에 모든 경우를 고려해도 4**7 == 만어쩌구 밖에 되지 않기 때문에 모든 조합을 만들어서 모든 경우를 따져보는 완전탐색을 사용해야 한다.

 

나는 itertools의 product라는 메서드를 사용했다.

product 메서드는 repeat이라는 옵션을 주어 repeat에 주어지는 값만큼 원소의 개수를 중복조합으로 생성한다.

 

따라서 할인율을 이모티콘의 개수만큼 중복조합으로 생성하고 유저마다 해당 할인율 조합으로 멤버십가입 또는 구매를 결정해준다.

모든 경우를 answer에 저장하고 역정렬 후 가장 첫 원소를 리턴해주면 된다.

profile

Heeto

@Heeto

🐧 블로그를 옮기는 과정에서 문법과 구성에 조금씩 오류가 있을 수 있습니다. 틀린 부분이 있다면 언제든지 알려주세요. 🐧