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 a80b022

Browse files
committed
feat: add solutions to lc problems: No.2293~2296
* No.2293.Min Max Game * No.2294.Partition Array Such That Maximum Difference Is K * No.2295.Replace Elements in an Array * No.2296.Design a Text Editor
1 parent 3952589 commit a80b022

File tree

35 files changed

+6611
-4692
lines changed

35 files changed

+6611
-4692
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [2291. Maximum Profit From Trading Stocks](https://leetcode.cn/problems/maximum-profit-from-trading-stocks)
2+
3+
[English Version](/solution/2200-2299/2291.Maximum%20Profit%20From%20Trading%20Stocks/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given two <strong>0-indexed</strong> integer arrays of the same length <code>present</code> and <code>future</code> where <code>present[i]</code> is the current price of the <code>i<sup>th</sup></code> stock and <code>future[i]</code> is the price of the <code>i<sup>th</sup></code> stock a year in the future. You may buy each stock at most <strong>once</strong>. You are also given an integer <code>budget</code> representing the amount of money you currently have.</p>
10+
11+
<p>Return <em>the maximum amount of profit you can make.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> present = [5,4,6,2,3], future = [8,5,4,3,5], budget = 10
18+
<strong>Output:</strong> 6
19+
<strong>Explanation:</strong> One possible way to maximize your profit is to:
20+
Buy the 0<sup>th</sup>, 3<sup>rd</sup>, and 4<sup>th</sup> stocks for a total of 5 + 2 + 3 = 10.
21+
Next year, sell all three stocks for a total of 8 + 3 + 5 = 16.
22+
The profit you made is 16 - 10 = 6.
23+
It can be shown that the maximum profit you can make is 6.
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> present = [2,2,5], future = [3,4,10], budget = 6
30+
<strong>Output:</strong> 5
31+
<strong>Explanation:</strong> The only possible way to maximize your profit is to:
32+
Buy the 2<sup>nd</sup> stock, and make a profit of 10 - 5 = 5.
33+
It can be shown that the maximum profit you can make is 5.
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> present = [3,3,12], future = [0,3,15], budget = 10
40+
<strong>Output:</strong> 0
41+
<strong>Explanation:</strong> One possible way to maximize your profit is to:
42+
Buy the 1<sup>st</sup> stock, and make a profit of 3 - 3 = 0.
43+
It can be shown that the maximum profit you can make is 0.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>n == present.length == future.length</code></li>
51+
<li><code>1 &lt;= n &lt;= 1000</code></li>
52+
<li><code>0 &lt;= present[i], future[i] &lt;= 100</code></li>
53+
<li><code>0 &lt;= budget &lt;= 1000</code></li>
54+
</ul>
55+
56+
57+
## 解法
58+
59+
<!-- 这里可写通用的实现逻辑 -->
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
77+
```
78+
79+
### **TypeScript**
80+
81+
```ts
82+
83+
```
84+
85+
### **...**
86+
87+
```
88+
89+
```
90+
91+
<!-- tabs:end -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2291. Maximum Profit From Trading Stocks](https://leetcode.com/problems/maximum-profit-from-trading-stocks)
2+
3+
[中文文档](/solution/2200-2299/2291.Maximum%20Profit%20From%20Trading%20Stocks/README.md)
4+
5+
## Description
6+
7+
<p>You are given two <strong>0-indexed</strong> integer arrays of the same length <code>present</code> and <code>future</code> where <code>present[i]</code> is the current price of the <code>i<sup>th</sup></code> stock and <code>future[i]</code> is the price of the <code>i<sup>th</sup></code> stock a year in the future. You may buy each stock at most <strong>once</strong>. You are also given an integer <code>budget</code> representing the amount of money you currently have.</p>
8+
9+
<p>Return <em>the maximum amount of profit you can make.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> present = [5,4,6,2,3], future = [8,5,4,3,5], budget = 10
16+
<strong>Output:</strong> 6
17+
<strong>Explanation:</strong> One possible way to maximize your profit is to:
18+
Buy the 0<sup>th</sup>, 3<sup>rd</sup>, and 4<sup>th</sup> stocks for a total of 5 + 2 + 3 = 10.
19+
Next year, sell all three stocks for a total of 8 + 3 + 5 = 16.
20+
The profit you made is 16 - 10 = 6.
21+
It can be shown that the maximum profit you can make is 6.
22+
</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> present = [2,2,5], future = [3,4,10], budget = 6
28+
<strong>Output:</strong> 5
29+
<strong>Explanation:</strong> The only possible way to maximize your profit is to:
30+
Buy the 2<sup>nd</sup> stock, and make a profit of 10 - 5 = 5.
31+
It can be shown that the maximum profit you can make is 5.
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> present = [3,3,12], future = [0,3,15], budget = 10
38+
<strong>Output:</strong> 0
39+
<strong>Explanation:</strong> One possible way to maximize your profit is to:
40+
Buy the 1<sup>st</sup> stock, and make a profit of 3 - 3 = 0.
41+
It can be shown that the maximum profit you can make is 0.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>n == present.length == future.length</code></li>
49+
<li><code>1 &lt;= n &lt;= 1000</code></li>
50+
<li><code>0 &lt;= present[i], future[i] &lt;= 100</code></li>
51+
<li><code>0 &lt;= budget &lt;= 1000</code></li>
52+
</ul>
53+
54+
55+
## Solutions
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
```python
62+
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
69+
```
70+
71+
### **TypeScript**
72+
73+
```ts
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2292. Products With Three or More Orders in Two Consecutive Years](https://leetcode.cn/problems/products-with-three-or-more-orders-in-two-consecutive-years)
2+
3+
[English Version](/solution/2200-2299/2292.Products%20With%20Three%20or%20More%20Orders%20in%20Two%20Consecutive%20Years/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Orders</code></p>
10+
11+
<pre>
12+
+---------------+------+
13+
| Column Name | Type |
14+
+---------------+------+
15+
| order_id | int |
16+
| product_id | int |
17+
| quantity | int |
18+
| purchase_date | date |
19+
+---------------+------+
20+
order_id is the primary key for this table.
21+
Each row in this table contains the ID of an order, the id of the product purchased, the quantity, and the purchase date.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
26+
<p>Write an SQL query to report the IDs of all the products that were purchased three or more times in two consecutive years.</p>
27+
28+
<p>Return the result table in <strong>any order</strong>.</p>
29+
30+
<p>The query result format is in the following example.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Example 1:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong>
37+
Orders table:
38+
+----------+------------+----------+---------------+
39+
| order_id | product_id | quantity | purchase_date |
40+
+----------+------------+----------+---------------+
41+
| 1 | 1 | 7 | 2020年03月16日 |
42+
| 2 | 1 | 4 | 2020年12月02日 |
43+
| 3 | 1 | 7 | 2020年05月10日 |
44+
| 4 | 1 | 6 | 2021年12月23日 |
45+
| 5 | 1 | 5 | 2021年05月21日 |
46+
| 6 | 1 | 6 | 2021年10月11日 |
47+
| 7 | 2 | 6 | 2022年10月11日 |
48+
+----------+------------+----------+---------------+
49+
<strong>Output:</strong>
50+
+------------+
51+
| product_id |
52+
+------------+
53+
| 1 |
54+
+------------+
55+
<strong>Explanation:</strong>
56+
Product 1 was ordered in 2020 three times and in 2021 three times. Since it was ordered three times in two consecutive years, we include it in the answer.
57+
Product 2 was ordered one time in 2022. We do not include it in the answer.
58+
</pre>
59+
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **SQL**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```sql
72+
73+
```
74+
75+
<!-- tabs:end -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [2292. Products With Three or More Orders in Two Consecutive Years](https://leetcode.com/problems/products-with-three-or-more-orders-in-two-consecutive-years)
2+
3+
[中文文档](/solution/2200-2299/2292.Products%20With%20Three%20or%20More%20Orders%20in%20Two%20Consecutive%20Years/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Orders</code></p>
8+
9+
<pre>
10+
+---------------+------+
11+
| Column Name | Type |
12+
+---------------+------+
13+
| order_id | int |
14+
| product_id | int |
15+
| quantity | int |
16+
| purchase_date | date |
17+
+---------------+------+
18+
order_id is the primary key for this table.
19+
Each row in this table contains the ID of an order, the id of the product purchased, the quantity, and the purchase date.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Write an SQL query to report the IDs of all the products that were purchased three or more times in two consecutive years.</p>
25+
26+
<p>Return the result table in <strong>any order</strong>.</p>
27+
28+
<p>The query result format is in the following example.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Example 1:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong>
35+
Orders table:
36+
+----------+------------+----------+---------------+
37+
| order_id | product_id | quantity | purchase_date |
38+
+----------+------------+----------+---------------+
39+
| 1 | 1 | 7 | 2020年03月16日 |
40+
| 2 | 1 | 4 | 2020年12月02日 |
41+
| 3 | 1 | 7 | 2020年05月10日 |
42+
| 4 | 1 | 6 | 2021年12月23日 |
43+
| 5 | 1 | 5 | 2021年05月21日 |
44+
| 6 | 1 | 6 | 2021年10月11日 |
45+
| 7 | 2 | 6 | 2022年10月11日 |
46+
+----------+------------+----------+---------------+
47+
<strong>Output:</strong>
48+
+------------+
49+
| product_id |
50+
+------------+
51+
| 1 |
52+
+------------+
53+
<strong>Explanation:</strong>
54+
Product 1 was ordered in 2020 three times and in 2021 three times. Since it was ordered three times in two consecutive years, we include it in the answer.
55+
Product 2 was ordered one time in 2022. We do not include it in the answer.
56+
</pre>
57+
58+
59+
## Solutions
60+
61+
<!-- tabs:start -->
62+
63+
### **SQL**
64+
65+
```sql
66+
67+
```
68+
69+
<!-- tabs:end -->

0 commit comments

Comments
(0)

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