当前位置:网站首页>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;
}
};
边栏推荐
- LeetCode394 字符串解码
- STM32 loopback structure receives and processes serial port data
- 非标自动化设备企业如何借助ERP系统,做好产品质量管理?
- Using JSON in C language projects
- 恋爱男女十禁
- Custom paging tag 02 of JSP custom tag
- Some API interfaces purchased by Yiwu hope to bring you some help
- Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
- Why do enterprises need the ability of enterprise knowledge management?
- The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
猜你喜欢

奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东

Interface control telerik UI for WPF - how to use radspreadsheet to record or comment

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

云原生—运行时环境

Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property

LeetCode 42.接雨水

Basic use of JSON server

Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held
随机推荐
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
JSP自定义标签之自定义分页标签02
线性分类器(CCF20200901)
AVL树(平衡搜索树)
Uniapp 应用开机自启插件 Ba-Autoboot
How to build knowledge management system in enterprises and institutions
Redis实现分布式锁
DIY system home page, your personalized needs PRO system to meet!
1331. 数组序号转换 : 简单模拟题
FlexPro软件:生产、研究和开发中的测量数据分析
HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
Insufficient permission to pull server code through Jenkins and other precautions
What SaaS architecture design does a software architect need to know?
云原生—运行时环境
C for循环内定义int i变量出现的重定义问题
LeetCode 移除元素&移动零
IO流再回顾,深入理解序列化和反序列化
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
GMT installation and use
Holes in [apue] files