Skip to content
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
13 changes: 7 additions & 6 deletions solution/2000-2099/2043.Simple Bank System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ bank.withdraw(10, 50); // 返回 false ,交易无效,因为账户 10 并

### 方法一:模拟

根据题意,我们可以使用一个数组 `balance` 来模拟银行账户的余额,数组下标从 0 开始,数组的值表示账户的余额
我们可以使用一个数组 $\textit{balance}$ 来存储每个账户的余额。对于每个操作,我们只需要根据题目要求进行相应的判断和更新即可

- 初始化时,我们将 `balance` 数组赋给成员变量 `this.balance`,并将 `balance` 的长度赋给成员变量 `this.n`。
- `transfer` 函数中,如果 `account1` 或 `account2` 大于 `n` 或 `balance[account1 - 1]` 小于 `money`,则返回 `false`,否则,将 `balance[account1 - 1]` 减去 `money`,将 `balance[account2 - 1]` 加上 `money`,并返回 `true`。
- `deposit` 函数中,如果 `account` 大于 `n`,则返回 `false`,否则,将 `balance[account - 1]` 加上 `money`,并返回 `true`。
- `withdraw` 函数中,如果 `account` 大于 `n` 或 `balance[account - 1]` 小于 `money`,则返回 `false`,否则,将 `balance[account - 1]` 减去 `money`,并返回 `true`。
对于 $\textit{transfer}$ 操作,我们需要检查账户编号是否合法以及账户余额是否足够,如果满足条件则进行转账操作。

以上操作的时间复杂度均为 $O(1)$,空间复杂度为 $O(n)$。其中,$n$ 为 `balance` 的长度。
对于 $\textit{deposit}$ 操作,我们只需要检查账户编号是否合法,然后进行存款操作。

对于 $\textit{withdraw}$ 操作,我们需要检查账户编号是否合法以及账户余额是否足够,如果满足条件则进行取款操作。

以上每个操作的时间复杂度均为 $O(1)$,因此整体时间复杂度为 $O(q)$,其中 $q$ 是操作次数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
13 changes: 7 additions & 6 deletions solution/2000-2099/2043.Simple Bank System/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ bank.withdraw(10, 50); // return false, it is invalid because account 10 does

### Solution 1: Simulation

According to the problem description, we can use an array `balance` to simulate the balance of bank accounts. The array index starts from 0, and the value of the array represents the balance of the account.
We can use an array $\textit{balance}$ to store the balance of each account. For each operation, we simply perform the required checks and updates according to the problem statement.

- During initialization, we assign the `balance` array to the member variable `this.balance`, and assign the length of `balance` to the member variable `this.n`.
- In the `transfer` function, if `account1` or `account2` is greater than `n` or `balance[account1 - 1]` is less than `money`, return `false`. Otherwise, subtract `money` from `balance[account1 - 1]`, add `money` to `balance[account2 - 1]`, and return `true`.
- In the `deposit` function, if `account` is greater than `n`, return `false`. Otherwise, add `money` to `balance[account - 1]`, and return `true`.
- In the `withdraw` function, if `account` is greater than `n` or `balance[account - 1]` is less than `money`, return `false`. Otherwise, subtract `money` from `balance[account - 1]`, and return `true`.
For the $\textit{transfer}$ operation, we need to check whether the account numbers are valid and whether the balance is sufficient. If the conditions are met, we perform the transfer.

The time complexity of the above operations is $O(1)$, and the space complexity is $O(n)$. Here, $n$ is the length of `balance`.
For the $\textit{deposit}$ operation, we only need to check whether the account number is valid, and then perform the deposit.

For the $\textit{withdraw}$ operation, we need to check whether the account number is valid and whether the balance is sufficient. If the conditions are met, we perform the withdrawal.

Each operation has a time complexity of $O(1)$, so the overall time complexity is $O(q)$, where $q$ is the number of operations. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tags:
<strong>输出:</strong>22
<strong>解释:</strong>
22 是一个数值平衡数,因为:
- 数字 2 出现 2 次
- 数字 2 出现 2 次
这也是严格大于 1 的最小数值平衡数。
</pre>

Expand All @@ -47,7 +47,7 @@ tags:
<strong>解释:</strong>
1333 是一个数值平衡数,因为:
- 数字 1 出现 1 次。
- 数字 3 出现 3 次。
- 数字 3 出现 3 次。
这也是严格大于 1000 的最小数值平衡数。
注意,1022 不能作为本输入的答案,因为数字 0 的出现次数超过了 0 。</pre>

Expand All @@ -59,7 +59,7 @@ tags:
<strong>解释:</strong>
3133 是一个数值平衡数,因为:
- 数字 1 出现 1 次。
- 数字 3 出现 3 次。
- 数字 3 出现 3 次。
这也是严格大于 3000 的最小数值平衡数。
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ tags:
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 22
<strong>Explanation:</strong>
<strong>Explanation:</strong>
22 is numerically balanced since:
- The digit 2 occurs 2 times.
- The digit 2 occurs 2 times.
It is also the smallest numerically balanced number strictly greater than 1.
</pre>

Expand All @@ -43,10 +43,10 @@ It is also the smallest numerically balanced number strictly greater than 1.
<pre>
<strong>Input:</strong> n = 1000
<strong>Output:</strong> 1333
<strong>Explanation:</strong>
<strong>Explanation:</strong>
1333 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times.
- The digit 3 occurs 3 times.
It is also the smallest numerically balanced number strictly greater than 1000.
Note that 1022 cannot be the answer because 0 appeared more than 0 times.
</pre>
Expand All @@ -56,7 +56,7 @@ Note that 1022 cannot be the answer because 0 appeared more than 0 times.
<pre>
<strong>Input:</strong> n = 3000
<strong>Output:</strong> 3133
<strong>Explanation:</strong>
<strong>Explanation:</strong>
3133 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

<pre>
+------------------+---------+
| Column Name | Type |
| Column Name | Type |
+------------------+---------+
| event_id | int |
| user_id | int |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

<pre>
+------------------+---------+
| Column Name | Type |
| Column Name | Type |
+------------------+---------+
| event_id | int |
| user_id | int |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Smallest%20Missing%20Multiple%20of%20K/README.md
rating: 1227
source: 第 472 场周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Smallest%20Missing%20Multiple%20of%20K/README_EN.md
rating: 1227
source: Weekly Contest 472 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3719.Longest Balanced Subarray I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3719.Longest%20Balanced%20Subarray%20I/README.md
rating: 1467
source: 第 472 场周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3719.Longest%20Balanced%20Subarray%20I/README_EN.md
rating: 1467
source: Weekly Contest 472 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3720.Lexicographically%20Smallest%20Permutation%20Greater%20Than%20Target/README.md
rating: 1958
source: 第 472 场周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3720.Lexicographically%20Smallest%20Permutation%20Greater%20Than%20Target/README_EN.md
rating: 1958
source: Weekly Contest 472 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3721.Longest%20Balanced%20Subarray%20II/README.md
rating: 2723
source: 第 472 场周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3721.Longest%20Balanced%20Subarray%20II/README_EN.md
rating: 2723
source: Weekly Contest 472 Q4
---

<!-- problem:start -->
Expand Down
Loading