Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit332199e

Browse files
authored
Sri Hari: Batch-5/Neetcode-ALL/Added-articles (neetcode-gh#3815)
* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles
1 parentd908bc0 commit332199e

20 files changed

+6302
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
##1. Simulation
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
deftotalMoney(self,n:int) ->int:
8+
day, deposit=0,1
9+
res=0
10+
11+
while day< n:
12+
res+= deposit
13+
deposit+=1
14+
day+=1
15+
16+
if day%7==0:
17+
deposit=1+ day//7
18+
19+
return res
20+
```
21+
22+
```java
23+
publicclassSolution {
24+
publicinttotalMoney(intn) {
25+
int day=0, deposit=1, res=0;
26+
27+
while (day< n) {
28+
res+= deposit;
29+
deposit++;
30+
day++;
31+
32+
if (day%7==0) {
33+
deposit=1+ day/7;
34+
}
35+
}
36+
37+
return res;
38+
}
39+
}
40+
```
41+
42+
```cpp
43+
classSolution {
44+
public:
45+
int totalMoney(int n) {
46+
int day = 0, deposit = 1, res = 0;
47+
48+
while (day < n) {
49+
res += deposit;
50+
deposit++;
51+
day++;
52+
53+
if (day % 7 == 0) {
54+
deposit = 1 + day / 7;
55+
}
56+
}
57+
58+
return res;
59+
}
60+
};
61+
```
62+
63+
```javascript
64+
classSolution {
65+
/**
66+
*@param{number}n
67+
*@return{number}
68+
*/
69+
totalMoney(n) {
70+
let day=0, deposit=1, res=0;
71+
72+
while (day< n) {
73+
res+= deposit;
74+
deposit++;
75+
day++;
76+
77+
if (day%7===0) {
78+
deposit=1+Math.floor(day/7);
79+
}
80+
}
81+
82+
return res;
83+
}
84+
}
85+
```
86+
87+
::tabs-end
88+
89+
###Time & Space Complexity
90+
91+
* Time complexity: $O(n)$
92+
* Space complexity: $O(1)$
93+
94+
---
95+
96+
##2. Math
97+
98+
::tabs-start
99+
100+
```python
101+
classSolution:
102+
deftotalMoney(self,n:int) ->int:
103+
weeks= n//7
104+
low=28
105+
high=28+7* (weeks-1)
106+
res= weeks* (low+ high)//2
107+
108+
monday= weeks+1
109+
for iinrange(n%7):
110+
res+= i+ monday
111+
112+
return res
113+
```
114+
115+
```java
116+
publicclassSolution {
117+
publicinttotalMoney(intn) {
118+
int weeks= n/7;
119+
int low=28;
120+
int high=28+7* (weeks-1);
121+
int res= weeks* (low+ high)/2;
122+
123+
int monday= weeks+1;
124+
for (int i=0; i< n%7; i++) {
125+
res+= i+ monday;
126+
}
127+
128+
return res;
129+
}
130+
}
131+
```
132+
133+
```cpp
134+
classSolution {
135+
public:
136+
int totalMoney(int n) {
137+
int weeks = n / 7;
138+
int low = 28;
139+
int high = 28 + 7 * (weeks - 1);
140+
int res = weeks * (low + high) / 2;
141+
142+
int monday = weeks + 1;
143+
for (int i = 0; i < n % 7; i++) {
144+
res += i + monday;
145+
}
146+
147+
return res;
148+
}
149+
};
150+
```
151+
152+
```javascript
153+
classSolution {
154+
/**
155+
*@param{number}n
156+
*@return{number}
157+
*/
158+
totalMoney(n) {
159+
constweeks=Math.floor(n/7);
160+
constlow=28;
161+
consthigh=28+7* (weeks-1);
162+
let res= weeks* (low+ high)/2;
163+
164+
constmonday= weeks+1;
165+
for (let i=0; i< n%7; i++) {
166+
res+= i+ monday;
167+
}
168+
169+
return res;
170+
}
171+
}
172+
```
173+
174+
::tabs-end
175+
176+
###Time & Space Complexity
177+
178+
* Time complexity: $O(1)$
179+
* Space complexity: $O(1)$
180+
181+
---
182+
183+
##3. Math (Optimal)
184+
185+
::tabs-start
186+
187+
```python
188+
classSolution:
189+
deftotalMoney(self,n:int) ->int:
190+
SUM=lambdax: (x* (x+1))>>1
191+
weeks= n//7
192+
res= SUM(weeks-1)*7+ weeks* SUM(7)
193+
res+= SUM(n%7)+ weeks* (n%7)
194+
return res
195+
```
196+
197+
```java
198+
publicclassSolution {
199+
publicinttotalMoney(intn) {
200+
int weeks= n/7;
201+
int res=SUM(weeks-1)*7+ weeks*SUM(7);
202+
res+=SUM(n%7)+ weeks* (n%7);
203+
return res;
204+
}
205+
206+
privateintSUM(intn) {
207+
return (n* (n+1))/2;
208+
}
209+
}
210+
```
211+
212+
```cpp
213+
classSolution {
214+
public:
215+
int totalMoney(int n) {
216+
auto SUM =[](int x) { return (x * (x + 1)) / 2; };
217+
218+
int weeks = n / 7;
219+
int res = SUM(weeks - 1) * 7 + weeks * SUM(7);
220+
res += SUM(n % 7) + weeks * (n % 7);
221+
return res;
222+
}
223+
};
224+
```
225+
226+
```javascript
227+
classSolution {
228+
/**
229+
*@param{number}n
230+
*@return{number}
231+
*/
232+
totalMoney(n) {
233+
constSUM=x=> (x* (x+1))/2;
234+
235+
constweeks=Math.floor(n/7);
236+
let res=SUM(weeks-1)*7+ weeks*SUM(7);
237+
res+=SUM(n%7)+ weeks* (n%7);
238+
return res;
239+
}
240+
}
241+
```
242+
243+
::tabs-end
244+
245+
###Time & Space Complexity
246+
247+
* Time complexity: $O(1)$
248+
* Space complexity: $O(1)$

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp