1518. Water Bottles

LeetCode easy original: C# #csharp #easy #leetcode
Der Aufgabentext wird für die gewählte Sprache aus dem Russischen übersetzt. Code bleibt unverändert.

Есть numBottles бутылок с водой, которые изначально наполнены водой. Вы можете обменять numExchange пустых бутылок на одну полную бутылку воды на рынке.

Операция питья полной бутылки воды превращает её в пустую бутылку.

given два целых числа numBottles и numExchange. return максимальное количество бутылок с водой, которые вы можете выпить.

Beispiel:

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.

C# Lösung

zugeordnet/original
public class Solution {
    public int NumWaterBottles(int numBottles, int numExchange) {
        int consumedBottles = 0;
        while (numBottles >= numExchange) {
            consumedBottles += numExchange;
            numBottles -= numExchange;
            numBottles++;
        }
        return consumedBottles + numBottles;
    }
}

C++ Lösung

Auto-Entwurf, vor dem Einreichen prüfen
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int NumWaterBottles(int numBottles, int numExchange) {
        int consumedBottles = 0;
        while (numBottles >= numExchange) {
            consumedBottles += numExchange;
            numBottles -= numExchange;
            numBottles++;
        }
        return consumedBottles + numBottles;
    }
}

Java Lösung

zugeordnet/original
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int consumedBottles = 0;

        while (numBottles >= numExchange) {
            consumedBottles += numExchange;
            numBottles -= numExchange;
            numBottles++;
        }

        return consumedBottles + numBottles;
    }
}

JavaScript Lösung

zugeordnet/original
var numWaterBottles = function(numBottles, numExchange) {
    let consumedBottles = 0;

    while (numBottles >= numExchange) {
        consumedBottles += numExchange;
        numBottles -= numExchange;
        numBottles++;
    }

    return consumedBottles + numBottles;
};

Go Lösung

zugeordnet/original
func numWaterBottles(numBottles int, numExchange int) int {
    consumedBottles := 0

    for numBottles >= numExchange {
        consumedBottles += numExchange
        numBottles -= numExchange
        numBottles++
    }

    return consumedBottles + numBottles
}

Algorithm

1⃣Инициализируйте переменную ответа consumedBottles значением 0.

2⃣Продолжайте выполнять следующие действия, пока количество numBottles больше или равно numExchange:

— Выпейте numExchange количество полных бутылок, т.е. добавьте numExchange к consumedBottles.

— Уменьшите numExchange от доступных полных бутылок numBottles.

— Обменяйте пустые бутылки на одну полную бутылку, т.е. увеличьте numBottles на одну.

3⃣return consumedBottles + numBottles.

😎

Stellen zu dieser Aufgabe

aktive Stellen with overlapping task tags are angezeigt.

Alle Stellen
Es gibt noch keine aktiven Stellen.