Skip to content

Commit a48425d

Browse files
authored
Create calculate-money-in-leetcode-bank.py
1 parent f31099d commit a48425d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(1)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def totalMoney(self, n):
6+
"""
7+
:type n: int
8+
:rtype: int
9+
"""
10+
def arithmetic_sequence_sum(a, d, n):
11+
return (2*a + (n-1)*d) * n //2
12+
13+
cost, day = 1, 7
14+
first_week_cost = arithmetic_sequence_sum(cost, cost, day)
15+
week, remain_day = divmod(n, day)
16+
return arithmetic_sequence_sum(first_week_cost, cost*day, week) + \
17+
arithmetic_sequence_sum(cost*(week+1), cost, remain_day)

0 commit comments

Comments
 (0)