在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You have some apples, where Return the maximum number of apples you can put in the basket.
Example 1: Input: arr = [100,200,150,1000] Output: 4 Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450. Example 2: Input: arr = [900,950,800,1000,700,800] Output: 5 Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
Constraints:
楼下水果店正在促销,你打算买些苹果, 你有一个购物袋,最多可以装
示例 1: 输入:arr = [100,200,150,1000] 输出:4 解释:所有 4 个苹果都可以装进去,因为它们的重量之和为 1450。 示例 2: 输入:arr = [900,950,800,1000,700,800] 输出:5 解释:6 个苹果的总重量超过了 5000,所以我们只能从中任选 5 个。
提示:
52ms 1 class Solution { 2 func maxNumberOfApples(_ arr: [Int]) -> Int { 3 var arr = arr 4 arr.sort() 5 var cnt:Int = 0 6 var w:Int = 5000 7 for a in arr 8 { 9 if w >= a 10 { 11 w -= a 12 cnt += 1 13 } 14 else 15 { 16 break 17 } 18 } 19 return cnt 20 } 21 }
|
请发表评论