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 thenthday.
The money deposited each week forms an arithmetic progression, and each day within a week also follows a pattern. We can compute the total by summing complete weeks and then adding the remaining days.
classSolution {
public:int totalMoney(int n) {
int weeks = n /7, days = n %7;
int ans = weeks *28+7* weeks * (weeks -1) /2;
for (int i =1; i <= days; ++i) {
ans += weeks + i;
}
return ans;
}
};
classSolution {
publicinttotalMoney(int n) {
int weeks = n / 7, days = n % 7;
int ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2;
for (int i = 1; i <= days; ++i) {
ans += weeks + i;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funtotalMoney(n: Int): Int {
val weeks = n / 7val days = n % 7var ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2for (i in1..days) {
ans += weeks + i
}
return ans
}
}
1
2
3
4
5
6
7
classSolution:
deftotalMoney(self, n: int) -> int:
weeks, days = divmod(n, 7)
ans = weeks *28+7* weeks * (weeks -1) //2for i in range(1, days +1):
ans += weeks + i
return ans
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfntotal_money(n: i32) -> i32 {
let weeks = n /7;
let days = n %7;
letmut ans = weeks *28+7* weeks * (weeks -1) /2;
for i in1..=days {
ans += weeks + i;
}
ans
}
}