文巾解题 1833. 雪糕的最大数量
生活随笔
收集整理的這篇文章主要介紹了
文巾解题 1833. 雪糕的最大数量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 題目描述
2 解題思路
2.1 排序+貪心
我們把雪糕的定價從低到高進行排序。然后從低價開始取,直到當前取出來的價格比coins大為止。
class Solution:def maxIceCream(self, costs: List[int], coins: int) -> int:costs.sort() #雪糕定價排序now_weight=0 #當前取出來的雪糕的總價格count=0 #已經取出來的雪糕的數量for i in costs:now_weight+=iif(now_weight<=coins):count+=1return(count)2.2 記數排序+貪心
我們建立一個字典,記錄每個價格的雪糕的數量
然后也是按照價格從低到高取雪糕
class Solution:def maxIceCream(self, costs: List[int], coins: int) -> int:unique_list=list(set(costs))unique_list.sort() #不同的雪糕的價格,從低到高排序dic={}for i in costs:if(i not in dic):dic[i]=1else:dic[i]+=1 #不同價格雪糕的數量 now_weight=0 #當前取出來的雪糕的總價格count=0 #當前取出來的雪糕的數量for i in unique_list:if(now_weight+i*dic[i]<=coins):count+=dic[i]now_weight=now_weight+i*dic[i]else:count+=(coins-now_weight)//inow_weight+=(coins-now_weight)//i * ireturn(count)總結
以上是生活随笔為你收集整理的文巾解题 1833. 雪糕的最大数量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生物计算:SIR模型笔记
- 下一篇: 文巾解题 197. 上升的温度