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

Sri Hari: Batch-5/Neetcode-ALL/Added-articles#3815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 2 commits intoneetcode-gh:mainfromSrihari2222:develop_articles1
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletionsarticles/calculate-money-in-leetcode-bank.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
##1. Simulation

::tabs-start

```python
classSolution:
deftotalMoney(self,n:int) ->int:
day, deposit=0,1
res=0

while day< n:
res+= deposit
deposit+=1
day+=1

if day%7==0:
deposit=1+ day//7

return res
```

```java
publicclassSolution {
publicinttotalMoney(intn) {
int day=0, deposit=1, res=0;

while (day< n) {
res+= deposit;
deposit++;
day++;

if (day%7==0) {
deposit=1+ day/7;
}
}

return res;
}
}
```

```cpp
classSolution {
public:
int totalMoney(int n) {
int day = 0, deposit = 1, res = 0;

while (day < n) {
res += deposit;
deposit++;
day++;

if (day % 7 == 0) {
deposit = 1 + day / 7;
}
}

return res;
}
};
```

```javascript
classSolution {
/**
*@param{number}n
*@return{number}
*/
totalMoney(n) {
let day=0, deposit=1, res=0;

while (day< n) {
res+= deposit;
deposit++;
day++;

if (day%7===0) {
deposit=1+Math.floor(day/7);
}
}

return res;
}
}
```

::tabs-end

###Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(1)$

---

##2. Math

::tabs-start

```python
classSolution:
deftotalMoney(self,n:int) ->int:
weeks= n//7
low=28
high=28+7* (weeks-1)
res= weeks* (low+ high)//2

monday= weeks+1
for iinrange(n%7):
res+= i+ monday

return res
```

```java
publicclassSolution {
publicinttotalMoney(intn) {
int weeks= n/7;
int low=28;
int high=28+7* (weeks-1);
int res= weeks* (low+ high)/2;

int monday= weeks+1;
for (int i=0; i< n%7; i++) {
res+= i+ monday;
}

return res;
}
}
```

```cpp
classSolution {
public:
int totalMoney(int n) {
int weeks = n / 7;
int low = 28;
int high = 28 + 7 * (weeks - 1);
int res = weeks * (low + high) / 2;

int monday = weeks + 1;
for (int i = 0; i < n % 7; i++) {
res += i + monday;
}

return res;
}
};
```

```javascript
classSolution {
/**
*@param{number}n
*@return{number}
*/
totalMoney(n) {
constweeks=Math.floor(n/7);
constlow=28;
consthigh=28+7* (weeks-1);
let res= weeks* (low+ high)/2;

constmonday= weeks+1;
for (let i=0; i< n%7; i++) {
res+= i+ monday;
}

return res;
}
}
```

::tabs-end

###Time & Space Complexity

* Time complexity: $O(1)$
* Space complexity: $O(1)$

---

##3. Math (Optimal)

::tabs-start

```python
classSolution:
deftotalMoney(self,n:int) ->int:
SUM=lambdax: (x* (x+1))>>1
weeks= n//7
res= SUM(weeks-1)*7+ weeks* SUM(7)
res+= SUM(n%7)+ weeks* (n%7)
return res
```

```java
publicclassSolution {
publicinttotalMoney(intn) {
int weeks= n/7;
int res=SUM(weeks-1)*7+ weeks*SUM(7);
res+=SUM(n%7)+ weeks* (n%7);
return res;
}

privateintSUM(intn) {
return (n* (n+1))/2;
}
}
```

```cpp
classSolution {
public:
int totalMoney(int n) {
auto SUM =[](int x) { return (x * (x + 1)) / 2; };

int weeks = n / 7;
int res = SUM(weeks - 1) * 7 + weeks * SUM(7);
res += SUM(n % 7) + weeks * (n % 7);
return res;
}
};
```

```javascript
classSolution {
/**
*@param{number}n
*@return{number}
*/
totalMoney(n) {
constSUM=x=> (x* (x+1))/2;

constweeks=Math.floor(n/7);
let res=SUM(weeks-1)*7+ weeks*SUM(7);
res+=SUM(n%7)+ weeks* (n%7);
return res;
}
}
```

::tabs-end

###Time & Space Complexity

* Time complexity: $O(1)$
* Space complexity: $O(1)$
Loading

[8]ページ先頭

©2009-2025 Movatter.jp