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

Commit 8d05723

Browse files
committed
feat: add solutions to lc problem: No.1716
1 parent 6445c81 commit 8d05723

File tree

7 files changed

+140
-29
lines changed

7 files changed

+140
-29
lines changed

‎solution/1700-1799/1716.Calculate Money in Leetcode Bank/README.md‎

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,35 @@ tags:
6161

6262
<!-- solution:start -->
6363

64-
### 方法一
64+
### 方法一:数学
65+
66+
根据题目描述,每周的存款情况如下:
67+
68+
```
69+
第 1 周:1, 2, 3, 4, 5, 6, 7
70+
第 2 周:2, 3, 4, 5, 6, 7, 8
71+
第 3 周:3, 4, 5, 6, 7, 8, 9
72+
...
73+
第 k 周:k, k+1, k+2, k+3, k+4, k+5, k+6
74+
```
75+
76+
存款天数为 $n,ドル那么完整的周数为 $k = \lfloor n / 7 \rfloor,ドル剩余天数为 $b = n \mod 7$。
77+
78+
完整的 $k$ 周的存款总额,可以通过等差数列求和公式计算:
79+
80+
$$
81+
S_1 = \frac{k}{2} \times (28 + 28 + 7 \times (k - 1))
82+
$$
83+
84+
剩余的 $b$ 天的存款总额,同样可以通过等差数列求和公式计算:
85+
86+
$$
87+
S_2 = \frac{b}{2} \times (k + 1 + k + 1 + b - 1)
88+
$$
89+
90+
最终的总存款金额为 $S = S_1 + S_2$。
91+
92+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
6593

6694
<!-- tabs:start -->
6795

@@ -70,17 +98,21 @@ tags:
7098
```python
7199
class Solution:
72100
def totalMoney(self, n: int) -> int:
73-
a, b = divmod(n, 7)
74-
return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2
101+
k, b = divmod(n, 7)
102+
s1 = (28 + 28 + 7 * (k - 1)) * k // 2
103+
s2 = (k + 1 + k + 1 + b - 1) * b // 2
104+
return s1 + s2
75105
```
76106

77107
#### Java
78108

79109
```java
80110
class Solution {
81111
public int totalMoney(int n) {
82-
int a = n / 7, b = n % 7;
83-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
112+
int k = n / 7, b = n % 7;
113+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
114+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
115+
return s1 + s2;
84116
}
85117
}
86118
```
@@ -91,8 +123,10 @@ class Solution {
91123
class Solution {
92124
public:
93125
int totalMoney(int n) {
94-
int a = n / 7, b = n % 7;
95-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
126+
int k = n / 7, b = n % 7;
127+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
128+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
129+
return s1 + s2;
96130
}
97131
};
98132
```
@@ -101,8 +135,22 @@ public:
101135
102136
```go
103137
func totalMoney(n int) int {
104-
a, b := n/7, n%7
105-
return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2
138+
k, b := n/7, n%7
139+
s1 := (28 + 28 + 7*(k-1)) * k / 2
140+
s2 := (k + 1 + k + 1 + b - 1) * b / 2
141+
return s1 + s2
142+
}
143+
```
144+
145+
#### TypeScript
146+
147+
```ts
148+
function totalMoney(n: number): number {
149+
const k = (n / 7) | 0;
150+
const b = n % 7;
151+
const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0;
152+
const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0;
153+
return s1 + s2;
106154
}
107155
```
108156

‎solution/1700-1799/1716.Calculate Money in Leetcode Bank/README_EN.md‎

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,35 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Math
66+
67+
According to the problem description, the deposit situation for each week is as follows:
68+
69+
```bash
70+
Week 1: 1, 2, 3, 4, 5, 6, 7
71+
Week 2: 2, 3, 4, 5, 6, 7, 8
72+
Week 3: 3, 4, 5, 6, 7, 8, 9
73+
...
74+
Week k: k, k+1, k+2, k+3, k+4, k+5, k+6
75+
```
76+
77+
Given $n$ days of deposits, the number of complete weeks is $k = \lfloor n / 7 \rfloor,ドル and the remaining days is $b = n \mod 7$.
78+
79+
The total deposit for the complete $k$ weeks can be calculated using the arithmetic sequence sum formula:
80+
81+
$$
82+
S_1 = \frac{k}{2} \times (28 + 28 + 7 \times (k - 1))
83+
$$
84+
85+
The total deposit for the remaining $b$ days can also be calculated using the arithmetic sequence sum formula:
86+
87+
$$
88+
S_2 = \frac{b}{2} \times (k + 1 + k + 1 + b - 1)
89+
$$
90+
91+
The final total deposit amount is $S = S_1 + S_2$.
92+
93+
The time complexity is $O(1)$ and the space complexity is $O(1)$.
6694

6795
<!-- tabs:start -->
6896

@@ -71,17 +99,21 @@ tags:
7199
```python
72100
class Solution:
73101
def totalMoney(self, n: int) -> int:
74-
a, b = divmod(n, 7)
75-
return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2
102+
k, b = divmod(n, 7)
103+
s1 = (28 + 28 + 7 * (k - 1)) * k // 2
104+
s2 = (k + 1 + k + 1 + b - 1) * b // 2
105+
return s1 + s2
76106
```
77107

78108
#### Java
79109

80110
```java
81111
class Solution {
82112
public int totalMoney(int n) {
83-
int a = n / 7, b = n % 7;
84-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
113+
int k = n / 7, b = n % 7;
114+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
115+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
116+
return s1 + s2;
85117
}
86118
}
87119
```
@@ -92,8 +124,10 @@ class Solution {
92124
class Solution {
93125
public:
94126
int totalMoney(int n) {
95-
int a = n / 7, b = n % 7;
96-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
127+
int k = n / 7, b = n % 7;
128+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
129+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
130+
return s1 + s2;
97131
}
98132
};
99133
```
@@ -102,8 +136,22 @@ public:
102136
103137
```go
104138
func totalMoney(n int) int {
105-
a, b := n/7, n%7
106-
return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2
139+
k, b := n/7, n%7
140+
s1 := (28 + 28 + 7*(k-1)) * k / 2
141+
s2 := (k + 1 + k + 1 + b - 1) * b / 2
142+
return s1 + s2
143+
}
144+
```
145+
146+
#### TypeScript
147+
148+
```ts
149+
function totalMoney(n: number): number {
150+
const k = (n / 7) | 0;
151+
const b = n % 7;
152+
const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0;
153+
const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0;
154+
return s1 + s2;
107155
}
108156
```
109157

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Solution {
22
public:
33
int totalMoney(int n) {
4-
int a = n / 7, b = n % 7;
5-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
4+
int k = n / 7, b = n % 7;
5+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
6+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
7+
return s1 + s2;
68
}
7-
};
9+
};
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
func totalMoney(n int) int {
2-
a, b := n/7, n%7
3-
return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2
4-
}
2+
k, b := n/7, n%7
3+
s1 := (28 + 28 + 7*(k-1)) * k / 2
4+
s2 := (k + 1 + k + 1 + b - 1) * b / 2
5+
return s1 + s2
6+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Solution {
22
public int totalMoney(int n) {
3-
int a = n / 7, b = n % 7;
4-
return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2;
3+
int k = n / 7, b = n % 7;
4+
int s1 = (28 + 28 + 7 * (k - 1)) * k / 2;
5+
int s2 = (k + 1 + k + 1 + b - 1) * b / 2;
6+
return s1 + s2;
57
}
6-
}
8+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
22
def totalMoney(self, n: int) -> int:
3-
a, b = divmod(n, 7)
4-
return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2
3+
k, b = divmod(n, 7)
4+
s1 = (28 + 28 + 7 * (k - 1)) * k // 2
5+
s2 = (k + 1 + k + 1 + b - 1) * b // 2
6+
return s1 + s2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function totalMoney(n: number): number {
2+
const k = (n / 7) | 0;
3+
const b = n % 7;
4+
const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0;
5+
const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0;
6+
return s1 + s2;
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /