-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy path1716. Calculate Money in Leetcode bank.cpp
More file actions
46 lines (36 loc) · 901 Bytes
/
1716. Calculate Money in Leetcode bank.cpp
File metadata and controls
46 lines (36 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
// Approach 1
//T.c - O(n)
int totalMoney(int n) {
int total = 0;
int monday =1;
while(n>0){
int money = monday;
for(int day=1; day<=min(n,7); day++){
total += money; //1
money++; //2,3
}
n -= 7;
monday++;
}
return total;
// Approach 2
// T.C - O(1)
//math Approach A.P series
int terms = n/7;
int first = 28;
int last = 28 +(terms -1)*7; // A.P formula
int result = terms *(first + last)/2; // sum of nth term in A.P
//first week remaining days
int start_money = 1+ terms;
for( int day = 1; day <=(n%7); day++){
result += start_money;
start_money++;
}
return result;
}
}
return result;
}
};