diff options
Diffstat (limited to 'calculate_money_bank.c')
-rw-r--r-- | calculate_money_bank.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/calculate_money_bank.c b/calculate_money_bank.c new file mode 100644 index 0000000..c74abf0 --- /dev/null +++ b/calculate_money_bank.c @@ -0,0 +1,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 +} |