当前位置:网站首页>Leetcode 1518. wine change
Leetcode 1518. wine change
2022-07-28 12:45:00 【.DoubleBean.】
leetcode 1518. Wine exchange
Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.
The operation of drinking a full water bottle turns it into an empty bottle.
Return the maximum number of water bottles you can drink.
Example 1:
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
Example 2:
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.
Example 3:
Input: numBottles = 5, numExchange = 5
Output: 6
Example 4:
Input: numBottles = 2, numExchange = 3
Output: 2
Constraints:
·1 <= numBottles <= 100
·2 <= numExchange <= 100
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/water-bottles
One
To the maximum extent , Greedily , Go to exchange wine , How many bottles of wine are there , Finish it first , After drinking, take the empty bottle as much as possible to exchange wine , Then drink all the wine you change ( That is to say sum += change), Last update numBottles, By this time numBottles, At this time, the number of empty bottles in your hand has just been drunk change A few drinks , In addition, at the beginning, it was not enough to change the wine, resulting in the remaining empty bottles , That's the remainder .
With example2 For example :
First put the in your hand 15 Drink a bottle of wine , Then there is 15 Empty bottles , Convertible 15 / 4 = 3 A bottle of wine , Drink these three bottles again , At this time, I drank 15+3=18 bottle , The number of empty bottles in your hand is 15 % 4 + 3 = 6 Empty bottles , Because it is larger than the exchange amount 4, So into the cycle , Convertible 6 / 4 = 1 A bottle of wine , The remaining 6 % 4 + 1 = 3 Empty bottles , Less than 4, Exit loop . A total of sum = 15 + 3 + 1 = 19 bottle
// Greedy strategy
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int sum = numBottles; // After drinking the drink in your hand
while(numBottles >= numExchange){
int change = numBottles / numExchange;
sum += change;
numBottles = change + numBottles % numExchange;
}
return sum;
}
};
Two
take numBottles = 15, numExchange = 4 To explain :
At first, you can exchange 15 / 4 = 3 A bottle of wine , At this time, you have to drink 3 * 4 = 12 Only when you have a bottle of wine can you have an empty bottle to exchange , After the exchange, there is 15 - 3 * 4 + 3 = 6 A bottle of wine , As a parameter, it can be substituted into the function for recursion .
// Using recursion
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
if(numBottles < numExchange){
return numBottles;
}
int change = numBottles / numExchange;
int sum = change * numExchange;
sum += numWaterBottles(numBottles % numExchange + change, numExchange);
return sum;
}
};
3、 ... and
① At the beginning, let's put the numBottles Drink up the bottle of wine , be left over numBottles Empty bottles .
② Because it's taking numExchange Change an empty wine bottle 1 Bottle of new wine , Therefore, the number of empty bottles equivalent to drinking an extra bottle of wine is numExchange - 1
( It's understandable : take numExchange Exchange an empty bottle for a bottle of wine , Drink up this bottle of wine . So at this point , Add this empty bottle just finished , At present, the number of empty bottles on hand is less than at the beginning numExchange - 1, But I went whoring to drink . )
Until the remaining empty bottles cannot be exchanged for new wine , How many bottles can you drink at most , That's what's required n, Then there are :
numBottles - n(numExchange - 1) < numExchange
Move items to get :
At the same time, we have to pay special attention to the premise here is numBottles > numExchange.
The code is as follows :
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
numBottles += numBottles >= numExchange ? (numBottles - numExchange) / (numExchange - 1) + 1 : 0;
return numBottles;
}
};
边栏推荐
- leetcode:数组
- 牛客网二叉树题解
- How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
- 上位机和三菱FN2x通信实例
- Uninstall Navicat: genuine MySQL official client, really fragrant!
- 05 pyechars 基本图表(示例代码+效果图)
- 苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
- stm32 回环结构接收串口数据并处理
- New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
- GMT安装与使用
猜你喜欢

Distributed session solution

FlexPro软件:生产、研究和开发中的测量数据分析

卸载 Navicat:正版 MySQL 官方客户端,真香!
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou

VS1003调试例程

Newly released, the domestic ide developed by Alibaba is completely open source

Library automatic reservation script

西门子对接Leuze BPS_304i 笔记

Hc-05 Bluetooth module debugging slave mode and master mode experience

HC-05蓝牙模块调试从模式和主模式经历
随机推荐
Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
上位机和三菱FN2x通信实例
云原生—运行时环境
Functions and pointers in 08 go language
合并表格行---三层for循环遍历数据
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
Unity 安装 Device Simulator
Pits encountered in MSP430 development (to be continued)
[apue] 文件中的空洞
C for循环内定义int i变量出现的重定义问题
遭受痛苦和创伤后的四种本真姿态 齐泽克
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
新零售电商O2O模式解析
非标自动化设备企业如何借助ERP系统,做好产品质量管理?
【一知半解】零值拷贝
C# static的用法详解
新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间
Ten prohibitions for men and women in love
Why do enterprises need the ability of enterprise knowledge management?
Brief discussion on open source OS distribution