Skip to content

Commit f31099d

Browse files
authored
Create calculate-money-in-leetcode-bank.cpp
1 parent 5272c32 commit f31099d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int totalMoney(int n) {
7+
static const int cost = 1, day = 7;
8+
static const auto arithmetic_sequence_sum =
9+
[](int a, int d, int n) {
10+
return (2 * a + (n - 1) * d) * n / 2;
11+
};
12+
13+
int first_week_cost = arithmetic_sequence_sum(cost, cost, day);
14+
int week = n / day, remain_day = n % day;
15+
return arithmetic_sequence_sum(first_week_cost, cost * day, week) +
16+
arithmetic_sequence_sum(cost * (week + 1), cost, remain_day);
17+
}
18+
};

0 commit comments

Comments
 (0)