当前位置:网站首页>Leetcode simple question: the minimum cost of buying candy at a discount
Leetcode simple question: the minimum cost of buying candy at a discount
2022-07-05 22:02:00 【·Starry Sea】
subject
A shop is selling candy at a discount . Every time you buy Two candy , Shop meeting free Send a candy .
The only restriction on free candy is : Its price needs to be less than or equal to the price of two candies purchased Smaller value .
For example , All in all 4 A candy , The prices are 1 ,2 ,3 and 4 , A customer bought it for 2 and 3 Of candy , Then he can get the price for free 1 Of candy , But you can't get a price of 4 Of candy .
I'll give you a subscript from 0 The starting array of integers cost , among cost[i] It means the first one i The price of a candy , Please return to get all Candy Minimum The total cost .
Example 1:
Input :cost = [1,2,3]
Output :5
explain : Our purchase price is 2 and 3 Of candy , Then get the price for free 1 Of candy .
The total cost is 2 + 3 = 5 . This is the least expensive only programme .
Be careful , We can't buy at 1 and 3 Of candy , And get the price for free 2 Of candy .
This is because the price of free candy must be less than or equal to the purchase 2 The smaller value of the candy price .
Example 2:
Input :cost = [6,5,7,9,2,2]
Output :23
explain : The minimum total cost of buying candy is :
- The purchase price is 9 and 7 Of candy
- The free price is 6 Of candy
- The purchase price is 5 and 2 Of candy
- The free price is 2 The last candy of
therefore , The minimum total cost is 9 + 7 + 5 + 2 = 23 .
Example 3:
Input :cost = [5,5]
Output :10
explain : Because only 2 A candy , We need to buy them all , And there is no free candy .
So the total minimum cost is 5 + 5 = 10 .
Tips :
1 <= cost.length <= 100
1 <= cost[i] <= 100
source : Power button (LeetCode)
Their thinking
For a given cost, The maximum value and the second largest value will definitely not be given , Then if the third largest value exists, it will be given away , So you can cost Sort from large to small , Then take out the first three values for operation , The remaining subsequence is another round of operation .
class Solution:
def minimumCost(self, cost: List[int]) -> int:
s=0
cost.sort(reverse=True)
for i in range(len(cost)):
if (i+1)%3!=0: # The third largest value does not need to be included in the total price
s+=cost[i]
return s

边栏推荐
- Blocking of concurrency control
- Summary of El and JSTL precautions
- A long's perception
- 科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
- Sentinel production environment practice (I)
- Performance monitoring of database tuning solutions
- An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
- POJ 3237 tree (tree chain splitting)
- Advantages of robot framework
- Concurrency control of performance tuning methodology
猜你喜欢

Sentinel production environment practice (I)

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

华为快游戏调用登录接口失败,返回错误码 -1

"Chris Richardson microservices series" uses API gateway to build microservices

元宇宙中的三大“派系”

Type of fault

极狐公司官方澄清声明

Efficiency difference between row first and column first traversal of mat data types in opencv

Official clarification statement of Jihu company

Overview of concurrency control
随机推荐
Pointer parameter passing vs reference parameter passing vs value parameter passing
Scenario interview: ten questions and ten answers about distributed locks
Blocking protocol for concurrency control
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Tips for using SecureCRT
Official clarification statement of Jihu company
Evolution of large website architecture and knowledge system
datagrid直接编辑保存“设计缺陷”
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
database mirroring
A trip to Suzhou during the Dragon Boat Festival holiday
SecureCRT使用提示
Blocking of concurrency control
AD637使用笔记
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Ad637 notes d'utilisation
EBS Oracle 11g cloning steps (single node)
C language knowledge points link
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]