[LeetCode] 1716. Calculate Money in Leetcode Bank
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 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 nth day.
Example 1:
Input: n = 4
Output: 10
Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
Example 2:
Input: n = 10
Output: 37
Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
Example 3:
Input: n = 20
Output: 96
Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
Constraints:
1 <= n <= 1000
计算力扣银行的钱。
Hercy 想要为购买第一辆车存钱。他 每天 都往力扣银行里存钱。
最开始,他在周一的时候存入 1 块钱。从周二到周日,他每天都比前一天多存入 1 块钱。在接下来每一个周一,他都会比前一个周一
多存入 1 块钱。
给你 n ,请你返回在第 n 天结束的时候他在力扣银行总共存了多少块钱。
思路
这是一道数学题。因为存钱是按照星期来存的,所以我们一开始用 n 除以 7,看看一共有几个星期;如果有余数,则再进行计算。对于一整个星期,计算方式就是等差数列。
复杂度
时间O(round)
空间O(1)
代码
Java实现
class Solution {
public int totalMoney(int n) {
int res = 0;
int round = n / 7;
int remainder = n % 7;
int start = 1;
int end = 7;
for (int i = 0; i < round; i++) {
res += helper(start++, end++);
}
// last round
start = 1 + round;
end = remainder + round;
for (int i = start; i <= end; i++) {
res += i;
}
return res;
}
// first item, last item, round
private int helper(int start, int end) {
return (start + end) * (end - start + 1) / 2;
}
}