-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path25_Calculate_Money_in_Leetcode_Bank.cpp
More file actions
47 lines (32 loc) · 1.24 KB
/
Copy path25_Calculate_Money_in_Leetcode_Bank.cpp
File metadata and controls
47 lines (32 loc) · 1.24 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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
class Solution
{
public:
int triSum(int n)
{
return (n * (n + 1)) >> 1;
}
int totalMoney(int days)
{
int nWeeks = days / 7;
int rDays = days % 7;
return triSum(days) - 42 * triSum(nWeeks - 1) - 6 * nWeeks * rDays;
}
};