当前位置:网站首页>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

边栏推荐
- 初级软件测试必问面试题
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- Talking about MySQL index
- Sentinel production environment practice (I)
- Concurrency control of performance tuning methodology
- How to develop and introduce applet plug-ins
- How to view Apache log4j 2 remote code execution vulnerability?
- Database recovery strategy
- Overview of concurrency control
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
猜你喜欢

Implementation technology of recovery

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程

Huawei fast game failed to call the login interface, and returned error code -1

Yolov5 training custom data set (pycharm ultra detailed version)

Ad637 notes d'utilisation

数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖

PIP install beatifulsoup4 installation failed

AD637使用笔记

Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned

Reptile practice
随机推荐
Advantages of robot framework
Codeforces 12D Ball 树形阵列模拟3排序元素
Tips for using SecureCRT
database mirroring
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
The real situation of programmers
Common interview questions of redis factory
Multiplexing of Oracle control files
Robot framework setting variables
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Image editor for their AutoLayout environment
Installation of VMware Workstation
Form artifact
How to add new fields to mongodb with code (all)
笔记本电脑蓝牙怎么用来连接耳机
Reptile practice
他们主动布局(autolayout)环境的图像编辑器
Business learning of mall order module