Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 deletions articles/calculate-money-in-leetcode-bank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
## 1. Simulation

::tabs-start

```python
class Solution:
def totalMoney(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
public class Solution {
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;
}
}
```

```cpp
class Solution {
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
class Solution {
/**
* @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
class Solution:
def totalMoney(self, n: int) -> int:
weeks = n // 7
low = 28
high = 28 + 7 * (weeks - 1)
res = weeks * (low + high) // 2

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

return res
```

```java
public class Solution {
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;
}
}
```

```cpp
class Solution {
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
class Solution {
/**
* @param {number} n
* @return {number}
*/
totalMoney(n) {
const weeks = Math.floor(n / 7);
const low = 28;
const high = 28 + 7 * (weeks - 1);
let res = weeks * (low + high) / 2;

const monday = 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
class Solution:
def totalMoney(self, n: int) -> int:
SUM = lambda x: (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
public class Solution {
public int totalMoney(int n) {
int weeks = n / 7;
int res = SUM(weeks - 1) * 7 + weeks * SUM(7);
res += SUM(n % 7) + weeks * (n % 7);
return res;
}

private int SUM(int n) {
return (n * (n + 1)) / 2;
}
}
```

```cpp
class Solution {
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
class Solution {
/**
* @param {number} n
* @return {number}
*/
totalMoney(n) {
const SUM = x => (x * (x + 1)) / 2;

const weeks = 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