File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+
3
+
4
+
5
+ class Solution {
6
+ public:
7
+
8
+ // Approach 1
9
+ //T.c - O(n)
10
+
11
+ int totalMoney(int n) {
12
+ int total = 0;
13
+ int monday =1;
14
+ while(n>0){
15
+ int money = monday;
16
+ for(int day=1; day<=min(n,7); day++){
17
+ total += money; //1
18
+ money++; //2,3
19
+ }
20
+ n -= 7;
21
+ monday++;
22
+ }
23
+ return total;
24
+
25
+ // Approach 2
26
+ // T.C - O(1)
27
+ //math Approach A.P series
28
+ int terms = n/7;
29
+ int first = 28;
30
+ int last = 28 +(terms -1)*7; // A.P formula
31
+ int result = terms *(first + last)/2; // sum of nth term in A.P
32
+
33
+ //first week remaining days
34
+ int start_money = 1+ terms;
35
+ for( int day = 1; day <=(n%7); day++){
36
+ result += start_money;
37
+ start_money++;
38
+ }
39
+ return result;
40
+ }
41
+ }
42
+
43
+ return result;
44
+
45
+ }
46
+ };
You can’t perform that action at this time.
0 commit comments