aboutsummaryrefslogtreecommitdiff
path: root/water_bottles.c
blob: 20a0a2d8f51f4e660bd8e7b9053da412237b841d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 1518. Water Bottles
#include "leetcode.h"

/*
 * there are 'numBottles' water bottles that are initially full of water. you
 * can exchange 'numExchange' empty water botls from the market with one full
 * water bottle. the operation of drinking a full water bottle turns into an
 * empty bottle. given the two integers 'numBottles' and 'numExchange', return
 * the maximum number of water bottles you can drink
 */

int numWaterBottles(int numBottles, int numExchange) {
  int ans = numBottles;
  while (numBottles >= numExchange) {
    int rem = numBottles % numExchange;
    numBottles /= numExchange;
    ans += numBottles;
    numBottles += rem;
  }
  return ans;
}

int main() {
  printf("%d\n", numWaterBottles(9, 3));  // expect: 13
  printf("%d\n", numWaterBottles(15, 4)); // expect: 19
}