Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
@import url(http://www.cppblog.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

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理