Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-01-06 13:44:56
1833. Maximum Ice Cream Bars
给你一个阵列costs表示每天冰淇淋的价钱,我们有coins个硬币,求出每天最多买一个
冰淇淋最多可以买几个冰淇淋。
Example :
Input: costs = [1,3,2,4,1], coins = 7
Output: 4
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total
price of 1 + 3 + 2 + 1 = 7.
Input: costs = [10,6,8,7,7,8], coins = 5
Output: 0
Explanation: The boy cannot afford any of the ice cream bars.
法一 排序
思路:
1.很直观的想法就是从价钱低的天数开始买冰淇淋直到自己没钱。
2.我们先排序价钱,再遍历一次价钱一边减去花费一边统计数量,直到钱不够为止。
3.时间复杂度O(nlogn + n)
Java Code:
作者: Jaka (Jaka)   2023-01-06 13:47:00
大师
作者: sustainer123 (caster)   2023-01-06 13:50:00
大师
作者: SecondRun (雨夜琴声)   2023-01-06 13:54:00
大师
作者: Che31128 (justjoke)   2023-01-06 13:57:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com