首页 > 其他分享 >leetcode-1518-easy

leetcode-1518-easy

时间:2023-07-20 20:44:59浏览次数:34  
标签:full bottles int water numExchange 1518 easy numBottles leetcode

Water Bottles

There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, 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.
Constraints:

1 <= numBottles <= 100
2 <= numExchange <= 100

思路一:遍历计算,用余数和除数相加

    public int numWaterBottles(int numBottles, int numExchange) {
        int ans = numBottles;

        while (numBottles >= numExchange) {
            int y = numBottles % numExchange;
            int x = numBottles / numExchange;
            ans += x;
            numBottles = y + x;
        }

        return ans;
    }

标签:full,bottles,int,water,numExchange,1518,easy,numBottles,leetcode
From: https://www.cnblogs.com/iyiluo/p/17569622.html

相关文章

  • [oeasy]python0073_进制转化_eval_evaluate_衡量_oct_octal_八进制
    进制转化回忆上次内容上次了解的是整型数字类变量integer前缀为i ​ 添加图片注释,不超过140字(可选) 整型变量和字符串变量不同整型变量是直接存储二进制形式的可以用int()函数将2进制形式的字符串转化为......
  • Codility / LeetCode的重要性与注意事项
    Codility/Leetcode不只会针对回答内容给出最终分数,也会一并记录解题的过程供面试官参考;相较于现场考试,Codility/Leetcode可以省下更多时间,也能让求职者在最熟悉的环境发挥实力。 进行测验前先查看Codility/LeetcodeFAQ,并完成demo题。可试着多做几题练习题,能全部做......
  • EasyCVR告警类型设置后首页需要刷新才能更新的问题优化
    EasyCVR视频融合平台基于云边端一体化架构,可支持多协议、多类型设备接入,包括:NVR、IPC、视频编码器、无人机、车载设备、智能手持终端、移动执法仪等。平台具有强大的数据接入、处理及分发能力,可在复杂的网络环境中,将分散的各类视频资源进行统一汇聚、整合、集中管理。关于平台的......
  • [LeetCode] 2268. Minimum Number of Keypresses
    Youhaveakeypadwith 9 buttons,numberedfrom 1 to 9,eachmappedtolowercaseEnglishletters.Youcanchoosewhichcharacterseachbuttonismatchedtoaslongas:All26lowercaseEnglishlettersaremappedto.Eachcharacterismappedtoby exact......
  • [LeetCode] 2486. Append Characters to String to Make Subsequence
    Youaregiventwostrings s and t consistingofonlylowercaseEnglishletters.Return theminimumnumberofcharactersthatneedtobeappendedtotheendof s sothat t becomesa subsequence of s.A subsequence isastringthatcanbederived......
  • LeetCode 1201. Ugly Number III 数学+二分答案
    Anuglynumberisapositiveintegerthatisdivisibleby\(a\),\(b\),or\(c\).Givenfourintegers\(n\),\(a\),\(b\),and\(c\),returnthe\(n\)thuglynumber.Solution考虑如何二分答案,假设一个函数\(f(num,a,b,c)\)会得到\([1,num]\)中uglynumb......
  • [刷题记录Day4]Leetcode链表专题
    No.1题目两两交换链表中的节点思路模拟类型题目两个节点前后交换,同时记住原来的下一个节点虚拟头节点代码public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode cur = dummyHead; while (cur.next != ......
  • LeetCode 875. Koko Eating Bananas 二分答案
    Kokolovestoeatbananas.Thereare\(n\)pilesofbananas,the\(i\)thpilehas\(piles[i]\)bananas.Theguardshavegoneandwillcomebackinhhours.Kokocandecideherbananas-per-houreatingspeedofk.Eachhour,shechoosessomepileofb......
  • [SUCTF 2019]EasySQL
    [SUCTF2019]EasySQL题目来源:buuctf题目类型:web涉及考点:SQL注入、堆叠注入1.题目给了一个输入框,先随便传点东西进去传入数字回显如下:传入flag回显如下:传入字符无回显没什么其他线索了,还是爆破一下看看过滤了哪些字符:返回长度为507的字符都是被过滤了的,包括uni......
  • LeetCode 1011. Capacity To Ship Packages Within D Days 二分答案
    Aconveyorbelthaspackagesthatmustbeshippedfromoneporttoanotherwithindaysdays.Theithpackageontheconveyorbelthasaweightof\(weights[i]\).Eachday,weloadtheshipwithpackagesontheconveyorbelt(intheordergivenby\(wei......