Skip to content

Commit d1f1400

Browse files
Create 1716-calculate-money-in-leetcode-bank.java
1 parent 1330ad2 commit d1f1400

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*-------------------------------
2+
Time complexity: O(1)
3+
Space Complexity: O(1)
4+
-------------------------------*/
5+
class Solution {
6+
public int totalMoney(int n) {
7+
int weeks = n / 7;;
8+
int low = 28;
9+
int high = 28 + 7 * (weeks - 1);
10+
int res = (weeks * (low + high) / 2);
11+
12+
int monday = weeks + 1;
13+
for(int i = 0; i < n % 7; i++)
14+
res += i + monday;
15+
16+
return res;
17+
}
18+
}
19+
20+
/*-------------------------------
21+
Time complexity: O(n)
22+
Space Complexity: O(1)
23+
-------------------------------*/
24+
class Solution {
25+
public int totalMoney(int n) {
26+
int day = 0;
27+
int deposit = 1;
28+
int res = 0;
29+
30+
while(day < n){
31+
res += deposit;
32+
deposit += 1;
33+
day += 1;
34+
35+
if(day % 7 == 0)
36+
deposit = 1 + day/7;
37+
}
38+
return res;
39+
}
40+
}
41+

0 commit comments

Comments
 (0)