Skip to content

Commit 607fe0a

Browse files
committed
feat: add new lc problems
1 parent 17d5ca7 commit 607fe0a

File tree

26 files changed

+5869
-4564
lines changed

26 files changed

+5869
-4564
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [2239. 找到最接近 0 的数字](https://leetcode-cn.com/problems/find-closest-number-to-zero)
2+
3+
[English Version](/solution/2200-2299/2239.Find%20Closest%20Number%20to%20Zero/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;,请你返回 <code>nums</code>&nbsp;中最 <strong>接近</strong>&nbsp;<code>0</code>&nbsp;的数字。如果有多个答案,请你返回它们中的 <strong>最大值</strong>&nbsp;。</p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre><b>输入:</b>nums = [-4,-2,1,4,8]
16+
<b>输出:</b>1
17+
<strong>解释:</strong>
18+
-4 到 0 的距离为 |-4| = 4 。
19+
-2 到 0 的距离为 |-2| = 2 。
20+
1 到 0 的距离为 |1| = 1 。
21+
4 到 0 的距离为 |4| = 4 。
22+
8 到 0 的距离为 |8| = 8 。
23+
所以,数组中距离 0 最近的数字为 1 。
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<pre><b>输入:</b>nums = [2,-1,1]
29+
<b>输出:</b>1
30+
<b>解释:</b>1 和 -1 都是距离 0 最近的数字,所以返回较大值 1 。
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= n &lt;= 1000</code></li>
39+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
40+
</ul>
41+
42+
## 解法
43+
44+
<!-- 这里可写通用的实现逻辑 -->
45+
46+
<!-- tabs:start -->
47+
48+
### **Python3**
49+
50+
<!-- 这里可写当前语言的特殊实现逻辑 -->
51+
52+
```python
53+
54+
```
55+
56+
### **Java**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```java
61+
62+
```
63+
64+
### **TypeScript**
65+
66+
```ts
67+
68+
```
69+
70+
### **...**
71+
72+
```
73+
74+
```
75+
76+
<!-- tabs:end -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [2239. Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero)
2+
3+
[中文文档](/solution/2200-2299/2239.Find%20Closest%20Number%20to%20Zero/README.md)
4+
5+
## Description
6+
7+
<p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the number with the value <strong>closest</strong> to </em><code>0</code><em> in </em><code>nums</code>. If there are multiple answers, return <em>the number with the <strong>largest</strong> value</em>.</p>
8+
<p>&nbsp;</p>
9+
<p><strong>Example 1:</strong></p>
10+
11+
<pre>
12+
<strong>Input:</strong> nums = [-4,-2,1,4,8]
13+
<strong>Output:</strong> 1
14+
<strong>Explanation:</strong>
15+
The distance from -4 to 0 is |-4| = 4.
16+
The distance from -2 to 0 is |-2| = 2.
17+
The distance from 1 to 0 is |1| = 1.
18+
The distance from 4 to 0 is |4| = 4.
19+
The distance from 8 to 0 is |8| = 8.
20+
Thus, the closest number to 0 in the array is 1.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [2,-1,1]
27+
<strong>Output:</strong> 1
28+
<strong>Explanation:</strong> 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n &lt;= 1000</code></li>
36+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
37+
</ul>
38+
39+
## Solutions
40+
41+
<!-- tabs:start -->
42+
43+
### **Python3**
44+
45+
```python
46+
47+
```
48+
49+
### **Java**
50+
51+
```java
52+
53+
```
54+
55+
### **TypeScript**
56+
57+
```ts
58+
59+
```
60+
61+
### **...**
62+
63+
```
64+
65+
```
66+
67+
<!-- tabs:end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2240. 买钢笔和铅笔的方案数](https://leetcode-cn.com/problems/number-of-ways-to-buy-pens-and-pencils)
2+
3+
[English Version](/solution/2200-2299/2240.Number%20of%20Ways%20to%20Buy%20Pens%20and%20Pencils/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数&nbsp;<code>total</code>&nbsp;,表示你拥有的总钱数。同时给你两个整数&nbsp;<code>cost1</code> 和&nbsp;<code>cost2</code>&nbsp;,分别表示一支钢笔和一支铅笔的价格。你可以花费你部分或者全部的钱,去买任意数目的两种笔。</p>
10+
11+
<p>请你返回购买钢笔和铅笔的&nbsp;<strong>不同方案数目</strong>&nbsp;。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><b>输入:</b>total = 20, cost1 = 10, cost2 = 5
18+
<b>输出:</b>9
19+
<b>解释:</b>一支钢笔的价格为 10 ,一支铅笔的价格为 5 。
20+
- 如果你买 0 支钢笔,那么你可以买 0 ,1 ,2 ,3 或者 4 支铅笔。
21+
- 如果你买 1 支钢笔,那么你可以买 0 ,1 或者 2 支铅笔。
22+
- 如果你买 2 支钢笔,那么你没法买任何铅笔。
23+
所以买钢笔和铅笔的总方案数为 5 + 3 + 1 = 9 种。
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<pre><b>输入:</b>total = 5, cost1 = 10, cost2 = 10
29+
<b>输出:</b>1
30+
<b>解释:</b>钢笔和铅笔的价格都为 10 ,都比拥有的钱数多,所以你没法购买任何文具。所以只有 1 种方案:买 0 支钢笔和 0 支铅笔。
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= total, cost1, cost2 &lt;= 10<sup>6</sup></code></li>
39+
</ul>
40+
41+
## 解法
42+
43+
<!-- 这里可写通用的实现逻辑 -->
44+
45+
<!-- tabs:start -->
46+
47+
### **Python3**
48+
49+
<!-- 这里可写当前语言的特殊实现逻辑 -->
50+
51+
```python
52+
53+
```
54+
55+
### **Java**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```java
60+
61+
```
62+
63+
### **TypeScript**
64+
65+
```ts
66+
67+
```
68+
69+
### **...**
70+
71+
```
72+
73+
```
74+
75+
<!-- tabs:end -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [2240. Number of Ways to Buy Pens and Pencils](https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils)
2+
3+
[中文文档](/solution/2200-2299/2240.Number%20of%20Ways%20to%20Buy%20Pens%20and%20Pencils/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer <code>total</code> indicating the amount of money you have. You are also given two integers <code>cost1</code> and <code>cost2</code> indicating the price of a pen and pencil respectively. You can spend <strong>part or all</strong> of your money to buy multiple quantities (or none) of each kind of writing utensil.</p>
8+
9+
<p>Return <em>the <strong>number of distinct ways</strong> you can buy some number of pens and pencils.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> total = 20, cost1 = 10, cost2 = 5
16+
<strong>Output:</strong> 9
17+
<strong>Explanation:</strong> The price of a pen is 10 and the price of a pencil is 5.
18+
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
19+
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
20+
- If you buy 2 pens, you cannot buy any pencils.
21+
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
22+
</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> total = 5, cost1 = 10, cost2 = 10
28+
<strong>Output:</strong> 1
29+
<strong>Explanation:</strong> The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= total, cost1, cost2 &lt;= 10<sup>6</sup></code></li>
37+
</ul>
38+
39+
## Solutions
40+
41+
<!-- tabs:start -->
42+
43+
### **Python3**
44+
45+
```python
46+
47+
```
48+
49+
### **Java**
50+
51+
```java
52+
53+
```
54+
55+
### **TypeScript**
56+
57+
```ts
58+
59+
```
60+
61+
### **...**
62+
63+
```
64+
65+
```
66+
67+
<!-- tabs:end -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2241. 设计一个 ATM 机器](https://leetcode-cn.com/problems/design-an-atm-machine)
2+
3+
[English Version](/solution/2200-2299/2241.Design%20an%20ATM%20Machine/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>一个 ATM 机器,存有&nbsp;<code>5</code>&nbsp;种面值的钞票:<code>20</code>&nbsp;,<code>50</code>&nbsp;,<code>100</code>&nbsp;,<code>200</code>&nbsp;&nbsp;<code>500</code>&nbsp;美元。初始时,ATM 机是空的。用户可以用它存或者取任意数目的钱。</p>
10+
11+
<p>取款时,机器会优先取 <b>较大</b>&nbsp;数额的钱。</p>
12+
13+
<ul>
14+
<li>比方说,你想取&nbsp;<code>$300</code>&nbsp;,并且机器里有&nbsp;<code>2</code>&nbsp;张 <code>$50</code>&nbsp;的钞票,<code>1</code>&nbsp;张&nbsp;<code>$100</code>&nbsp;的钞票和<code>1</code>&nbsp;张&nbsp;<code>$200</code>&nbsp;的钞票,那么机器会取出&nbsp;<code>$100</code> 和&nbsp;<code>$200</code>&nbsp;的钞票。</li>
15+
<li>但是,如果你想取&nbsp;<code>$600</code>&nbsp;,机器里有&nbsp;<code>3</code>&nbsp;张&nbsp;<code>$200</code>&nbsp;的钞票和<code>1</code>&nbsp;张&nbsp;<code>$500</code>&nbsp;的钞票,那么取款请求会被拒绝,因为机器会先取出&nbsp;<code>$500</code>&nbsp;的钞票,然后无法取出剩余的&nbsp;<code>$100</code>&nbsp;。注意,因为有&nbsp;<code>$500</code>&nbsp;钞票的存在,机器&nbsp;<strong>不能</strong>&nbsp;取&nbsp;<code>$200</code>&nbsp;的钞票。</li>
16+
</ul>
17+
18+
<p>请你实现 ATM 类:</p>
19+
20+
<ul>
21+
<li><code>ATM()</code>&nbsp;初始化 ATM 对象。</li>
22+
<li><code>void deposit(int[] banknotesCount)</code>&nbsp;分别存入&nbsp;<code>$20</code>&nbsp;,<code>$50</code>,<code>$100</code>,<code>$200</code>&nbsp;和&nbsp;<code>$500</code>&nbsp;钞票的数目。</li>
23+
<li><code>int[] withdraw(int amount)</code>&nbsp;返回一个长度为&nbsp;<code>5</code>&nbsp;的数组,分别表示&nbsp;<code>$20</code>&nbsp;,<code>$50</code>,<code>$100</code>&nbsp;,<code>$200</code>&nbsp;和&nbsp;<code>$500</code>&nbsp;钞票的数目,并且更新 ATM 机里取款后钞票的剩余数量。如果无法取出指定数额的钱,请返回&nbsp;<code>[-1]</code>&nbsp;(这种情况下 <strong>不</strong>&nbsp;取出任何钞票)。</li>
24+
</ul>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong>示例 1:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>
32+
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"]
33+
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
34+
<strong>输出:</strong>
35+
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]
36+
37+
<strong>解释:</strong>
38+
ATM atm = new ATM();
39+
atm.deposit([0,0,1,2,1]); // 存入 1 张 $100 ,2 张 $200 和 1 张 $500 的钞票。
40+
atm.withdraw(600); // 返回 [0,0,1,0,1] 。机器返回 1 张 $100 和 1 张 $500 的钞票。机器里剩余钞票的数量为 [0,0,0,2,0] 。
41+
atm.deposit([0,1,0,1,1]); // 存入 1 张 $50 ,1 张 $200 和 1 张 $500 的钞票。
42+
// 机器中剩余钞票数量为 [0,1,0,3,1] 。
43+
atm.withdraw(600); // 返回 [-1] 。机器会尝试取出 $500 的钞票,然后无法得到剩余的 $100 ,所以取款请求会被拒绝。
44+
// 由于请求被拒绝,机器中钞票的数量不会发生改变。
45+
atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的钞票和 1 张 $500 的钞票。</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>banknotesCount.length == 5</code></li>
53+
<li><code>0 &lt;= banknotesCount[i] &lt;= 10<sup>9</sup></code></li>
54+
<li><code>1 &lt;= amount &lt;= 10<sup>9</sup></code></li>
55+
<li><strong>总共</strong>&nbsp;最多有&nbsp;<code>5000</code>&nbsp;次&nbsp;<code>withdraw</code> 和&nbsp;<code>deposit</code>&nbsp;的调用。</li>
56+
<li><span style="">函数 </span><code>withdraw</code> 和&nbsp;<code>deposit</code>&nbsp;至少各有 <strong>一次&nbsp;</strong>调用。</li>
57+
</ul>
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **TypeScript**
82+
83+
```ts
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->

0 commit comments

Comments
 (0)