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 d3d948a

Browse files
✨feat: Add 1713
1 parent ceeb4f0 commit d3d948a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
| [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 |
5151
| [1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/) | 简单 | 🤩🤩🤩🤩 |
5252
| [1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 |
53+
| [1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/) | 简单 | 🤩🤩🤩🤩 |
5354
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
5455
| [1734. 解码异或后的排列](https://leetcode-cn.com/problems/decode-xored-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-permutation/solution/gong-shui-san-xie-note-bie-pian-li-yong-zeh6o/) | 中等 | 🤩🤩🤩🤩 |
5556
| [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
9090
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 |
9191
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
92+
| [1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/) | 简单 | 🤩🤩🤩🤩 |
9293
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
9394
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩 |
9495
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/)** ,难度为 **简单**
4+
5+
Tag : 「数学」、「模拟」
6+
7+
8+
9+
Hercy 想要为购买第一辆车存钱。他 **每天** 都往力扣银行里存钱。
10+
11+
最开始,他在周一的时候存入 `1` 块钱。从周二到周日,他每天都比前一天多存入 `1` 块钱。在接下来每一个周一,他都会比 前一个周一 多存入 `1` 块钱。
12+
13+
给你 `n` ,请你返回在第 `n` 天结束的时候他在力扣银行总共存了多少块钱。
14+
15+
示例 1:
16+
```
17+
输入:n = 4
18+
19+
输出:10
20+
21+
解释:第 4 天后,总额为 1 +たす 2 +たす 3 +たす 4 = 10 。
22+
```
23+
示例 2:
24+
```
25+
输入:n = 10
26+
27+
输出:37
28+
29+
解释:第 10 天后,总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37 。注意到第二个星期一,Hercy 存入 2 块钱。
30+
```
31+
示例 3:
32+
```
33+
输入:n = 20
34+
35+
输出:96
36+
37+
解释:第 20 天后,总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96 。
38+
```
39+
40+
提示:
41+
* 1ドル <= n <= 1000$
42+
43+
---
44+
45+
### 模拟
46+
47+
根据题意进行模拟即可,分两步进行计算。
48+
49+
先计算完整周:共 $a = \left \lfloor \frac{n}{7} \right \rfloor$ 周,第一周起始日金额为 1ドル,ドル每周的起始日的金额递增 1ドル,ドル周内金额可使用「等差数列求和」公式直接求解。
50+
51+
然后再计算最后一周(若有)的金额:共 $b = n \pmod 7$ 天,使用紧接的起始日金额继续进行累加即可。
52+
53+
代码:
54+
```Java
55+
class Solution {
56+
public int totalMoney(int n) {
57+
int a = n / 7, b = n % 7;
58+
int ans = 0, k = 1;
59+
while (a-- > 0) {
60+
ans += (k + (k + 6)) * 7 / 2;
61+
k++;
62+
}
63+
while (b-- > 0) ans += k++;
64+
return ans;
65+
}
66+
}
67+
```
68+
* 时间复杂度:$O(a + b)$
69+
* 空间复杂度:$O(1)$
70+
71+
---
72+
73+
### (优化)模拟
74+
75+
更近一步,每个完整周的起始日金额相比上周起始日金额多 1ドル,ドル同时周内每日金额递增 1ドル,ドル因此相邻完整周的金额之和也满足「等差」性质,可直接使用「等差数列求和」公式 $O(1)$ 求解完整周部分的金额之和;周后一周(若有)的金额也是同理。
76+
77+
代码:
78+
```Java
79+
class Solution {
80+
public int totalMoney(int n) {
81+
int a = n / 7, b = n % 7;
82+
int a1 = (1 + 7) * 7 / 2, an = (a + (a + 6)) * 7 / 2;
83+
int s1 = (a1 + an) * a / 2;
84+
a++;
85+
int s2 = (a + (a + b - 1)) * b / 2;
86+
return s1 + s2;
87+
}
88+
}
89+
```
90+
* 时间复杂度:$O(1)$
91+
* 空间复杂度:$O(1)$
92+
93+
---
94+
95+
### 最后
96+
97+
这是我们「刷穿 LeetCode」系列文章的第 `No.1716` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
98+
99+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
100+
101+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
102+
103+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
104+

0 commit comments

Comments
(0)

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