File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments