Skip to content

Commit 3f87c38

Browse files
committed
New Problem Solution -"Calculate Money in Leetcode Bank"
1 parent d1cf484 commit 3f87c38

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ LeetCode
6161
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
6262
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy|
6363
|1725|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/) | [C++](./algorithms/cpp/numberOfRectanglesThatCanFormTheLargestSquare/NumberOfRectanglesThatCanFormTheLargestSquare.cpp)|Easy|
64+
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/) | [C++](./algorithms/cpp/calculateMoneyInLeetcodeBank/CalculateMoneyInLeetcodeBank.cpp)|Easy|
6465
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/) | [C++](./algorithms/cpp/lexicographicallySmallestStringAfterApplyingOperations/LexicographicallySmallestStringAfterApplyingOperations.cpp)|Medium|
6566
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [C++](./algorithms/cpp/largestSubstringBetweenTwoEqualCharacters/LargestSubstringBetweenTwoEqualCharacters.cpp)|Easy|
6667
|1605|[Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Source : https://leetcode.com/problems/calculate-money-in-leetcode-bank/
2+
// Author : Hao Chen
3+
// Date : 2021-03-28
4+
5+
/*****************************************************************************************************
6+
*
7+
* Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
8+
*
9+
* He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put
10+
* in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the
11+
* previous Monday.
12+
*
13+
* Given n, return the total amount of money he will have in the Leetcode bank at the end of the n^th
14+
* day.
15+
*
16+
* Example 1:
17+
*
18+
* Input: n = 4
19+
* Output: 10
20+
* Explanation: After the 4^th day, the total is 1 + 2 + 3 + 4 = 10.
21+
*
22+
* Example 2:
23+
*
24+
* Input: n = 10
25+
* Output: 37
26+
* Explanation: After the 10^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37.
27+
* Notice that on the 2^nd Monday, Hercy only puts in $2.
28+
*
29+
* Example 3:
30+
*
31+
* Input: n = 20
32+
* Output: 96
33+
* Explanation: After the 20^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7
34+
* + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
35+
*
36+
* Constraints:
37+
*
38+
* 1 <= n <= 1000
39+
******************************************************************************************************/
40+
41+
class Solution {
42+
public:
43+
int totalMoney(int n) {
44+
int weeks = n / 7;
45+
int days = n % 7;
46+
47+
int m = 1 + 2 + 3 + 4 + 5 + 6 + 7;
48+
// we know
49+
// week1 = 0*7 + m
50+
// week2 = 1*7 + m
51+
// week3 = 2*7 + m
52+
// weekn = (n-1)*7 + m
53+
// So,
54+
// week1 + week2 + week3 + ....+ weekn
55+
// = n*m + 7*(0+1+2+..n-1)
56+
// = n*m + 7*(n-1)*n/2
57+
58+
int money = weeks*m + 7 * (weeks-1) * weeks / 2;
59+
60+
// for the rest days
61+
// every day needs to add previous `weeks * 1`, it is days* weeks
62+
// then add from 1 to days
63+
money += (days*weeks + days*(days+1)/2);
64+
65+
return money;
66+
}
67+
};

0 commit comments

Comments
 (0)