happy coding
[level 1] 과일 장수 본문
문제 설명
과일 장수가 사과 상자를 포장하고 있습니다. 사과는 상태에 따라 1점부터 k점까지의 점수로 분류하며, k점이 최상품의 사과이고 1점이 최하품의 사과입니다. 사과 한 상자의 가격은 다음과 같이 결정됩니다.
- 한 상자에 사과를 m개씩 담아 포장합니다.
- 상자에 담긴 사과 중 가장 낮은 점수가 p (1 ≤ p ≤ k)점인 경우, 사과 한 상자의 가격은 p * m 입니다.
과일 장수가 가능한 많은 사과를 팔았을 때, 얻을 수 있는 최대 이익을 계산하고자 합니다.(사과는 상자 단위로만 판매하며, 남는 사과는 버립니다)
예를 들어, k = 3, m = 4, 사과 7개의 점수가 [1, 2, 3, 1, 2, 3, 1]이라면, 다음과 같이 [2, 3, 2, 3]으로 구성된 사과 상자 1개를 만들어 판매하여 최대 이익을 얻을 수 있습니다.
- (최저 사과 점수) x (한 상자에 담긴 사과 개수) x (상자의 개수) = 2 x 4 x 1 = 8
사과의 최대 점수 k, 한 상자에 들어가는 사과의 수 m, 사과들의 점수 score가 주어졌을 때, 과일 장수가 얻을 수 있는 최대 이익을 return하는 solution 함수를 완성해주세요.
제한사항- 3 ≤ k ≤ 9
- 3 ≤ m ≤ 10
- 7 ≤ score의 길이 ≤ 1,000,000
- 1 ≤ score[i] ≤ k
- 이익이 발생하지 않는 경우에는 0을 return 해주세요.
내 풀이
다른 풀이
def solution(k, m, score):
"""
score 돌면서 dict에 key : count 저장
jj
"""
score_count = {}
for s in score:
try:
score_count[s] += 1
except:
score_count[s] = 1
score_count = dict(sorted(score_count.items(), key = lambda x:x[0], reverse=True))
box_count = 0 # 몫
rest_count = 0 # 나머지
result = 0
for s, count in score_count.items():
# rest_count가 있으면 count에 더해주기
if rest_count:
count += rest_count
box_count = count // m
rest_count = count % m
result += box_count * s * m
print(result)
return result
def solution(k, m, score):
answer = 0
a = []
score.sort()
while True:
if(len(a) == m):
answer += min(a) * m
a = []
if len(score) == 0:
return answer
a.append(score[-1])
score.pop()
return score
def solution(k, m, score):
return sum(sorted(score)[len(score)%m::m])*m
solution = lambda _, m, s: sum(sorted(s)[-m::-m]) * m
def solution(k, m, score):
answer = 0
score.sort(reverse=True)
apple_box = []
for i in range(0, len(score), m):
apple_box.append(score[i:i+m])
for apple in apple_box:
if len(apple) == m:
answer += min(apple) * m
return answer
'coding study > programmars' 카테고리의 다른 글
[level 1] 소수 찾기 (0) | 2024.07.02 |
---|---|
[level 1] 소수 만들기 (0) | 2024.07.01 |
[level 1] 푸드 파이트 대회 (0) | 2024.07.01 |
[level 1] 모의고사 (0) | 2024.07.01 |
[level 1] 기사단원의 무기 (0) | 2024.07.01 |
Comments