[LeetCode]1833. Maximum Ice Cream Bars (Medium) Python-2023.01.06
Posted on 2023-01-06 20:31 Uriel 閱讀(58) 評論(0) 編輯 收藏 引用 所屬分類: 貪心 、閑來無事重切Leet Code 、大水題@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
給出每個ice cream的價格costs[i],一共有coins錢,問最多買多少ice cream,貪心水題,直接按costs排序從價格由低到高買
1 #1833
2
3 class Solution(object):
4 def maxIceCream(self, costs, coins):
5 """
6 :type costs: List[int]
7 :type coins: int
8 :rtype: int
9 """
10 costs.sort()
11 ans = 0
12 for i in costs:
13 coins -= i
14 if coins < 0:
15 break
16 ans += 1
17 return ans
2
3 class Solution(object):
4 def maxIceCream(self, costs, coins):
5 """
6 :type costs: List[int]
7 :type coins: int
8 :rtype: int
9 """
10 costs.sort()
11 ans = 0
12 for i in costs:
13 coins -= i
14 if coins < 0:
15 break
16 ans += 1
17 return ans