aboutsummaryrefslogtreecommitdiff
path: root/calculate_money_bank.c
blob: c74abf0f67920482189f8a41299146f90b2944d7 (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
27
28
29
30
31
32
33
// 1716. Calculate Money in Leetcode Bank
#include "leetcode.h"

/*
 * hercy wants to save money for his first car. he puts money in the leetcode
 * bank every day. he starts by putting in $1 on monday, the first day. every
 * day from tuesday to sunday he will put in 1 more dollar than the day before.
 * on every subsequent monday, he will put in 1 more than the previous monday.
 * given 'n', return the total amount of money he will have in the leetcode bank
 * at the end of the n'th day.
 */

int totalMoney(int n) {
  int begin = 1, end = 7;
  int sum = 28, total = 0;
  while (n - 7) {
    total += sum;
    sum -= begin++;
    sum += ++end;
    n -= 7;
  }
  for (int i = 0; i < n; ++i) {
    total += begin;
    ++begin;
  }
  return total;
}

int main() {
  printf("%d\n", totalMoney(4));  // expect: 10
  printf("%d\n", totalMoney(10)); // expect: 37
  printf("%d\n", totalMoney(20)); // expect: 96
}