Skip to content

Commit e1db205

Browse files
committed
feat: add solutions to lc problems: No.2475~2478
* No.2475.Number of Unequal Triplets in Array * No.2476.Closest Nodes Queries in a Binary Search Tree * No.2477.Minimum Fuel Cost to Report to the Capital * No.2478.Number of Beautiful Partitions
1 parent 87b316a commit e1db205

File tree

48 files changed

+2629
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2629
-74
lines changed

lcof2/剑指 Offer II 055. 二叉搜索树迭代器/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class BSTIterator {
511511
// }
512512
// }
513513
use std::rc::Rc;
514-
use std::cell::RefCell;
514+
use std::cell::RefCell;
515515
struct BSTIterator {
516516
stack: Vec<i32>
517517
}

solution/0700-0799/0775.Global and Local Inversions/README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
<p><strong>全局倒置</strong> 的数目等于满足下述条件不同下标对 <code>(i, j)</code> 的数目:</p>
1212

1313
<ul>
14-
<li><code>0 <= i < j < n</code></li>
15-
<li><code>nums[i] > nums[j]</code></li>
14+
<li><code>0 &lt;= i &lt; j &lt; n</code></li>
15+
<li><code>nums[i] &gt; nums[j]</code></li>
1616
</ul>
1717

1818
<p><strong>局部倒置</strong> 的数目等于满足下述条件的下标 <code>i</code> 的数目:</p>
1919

2020
<ul>
21-
<li><code>0 <= i < n - 1</code></li>
22-
<li><code>nums[i] > nums[i + 1]</code></li>
21+
<li><code>0 &lt;= i &lt; n - 1</code></li>
22+
<li><code>nums[i] &gt; nums[i + 1]</code></li>
2323
</ul>
2424

2525
<p>当数组 <code>nums</code> 中 <strong>全局倒置</strong> 的数量等于 <strong>局部倒置</strong> 的数量时,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
2626

27-
<p> </p>
27+
<p>&nbsp;</p>
2828

2929
<p><strong>示例 1:</strong></p>
3030

@@ -42,12 +42,14 @@
4242
<strong>解释:</strong>有 2 个全局倒置,和 1 个局部倒置。
4343
</pre>
4444

45+
&nbsp;
46+
4547
<p><strong>提示:</strong></p>
4648

4749
<ul>
4850
<li><code>n == nums.length</code></li>
49-
<li><code>1 <= n <= 5000</code></li>
50-
<li><code>0 <= nums[i] < n</code></li>
51+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
52+
<li><code>0 &lt;= nums[i] &lt; n</code></li>
5153
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
5254
<li><code>nums</code> 是范围 <code>[0, n - 1]</code> 内所有数字组成的一个排列</li>
5355
</ul>

solution/0800-0899/0808.Soup Servings/README.md

+13-19
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@
5555

5656
**方法一:记忆化搜索**
5757

58-
在这道题中,由于每次操作都是 $25$ 的倍数,因此,我们可以将每 `25ml` 的汤视为一份。这样就能将数据规模缩小到 $\left \lceil \frac{n}{25} \right \rceil $。
58+
在这道题中,由于每次操作都是 $25$ 的倍数,因此,我们可以将每 $25ml$ 的汤视为一份。这样就能将数据规模缩小到 $\left \lceil \frac{n}{25} \right \rceil $。
5959

6060
我们设计一个函数 $dfs(i, j)$,表示当前剩余 $i$ 份汤 $A$ 和 $j$ 份汤 $B$ 的结果概率。
6161

62-
当 $i \leq 0 \cap j \leq 0$ 时,表示两种汤都分配完了,此时应该返回 $0.5$;当 $i \leq 0$ 时,表示汤 $A$ 先分配完了,此时应该返回 $1$;当 $j \leq 0$ 时,表示汤 $B$ 先分配完了,此时应该返回 $0$。
62+
当 $i \leq 0$ 并且 $j \leq 0$ 时,表示两种汤都分配完了,此时应该返回 $0.5$;当 $i \leq 0$ 时,表示汤 $A$ 先分配完了,此时应该返回 $1$;当 $j \leq 0$ 时,表示汤 $B$ 先分配完了,此时应该返回 $0$。
6363

6464
接下来,对于每一次操作,我们都有四种选择,即:
6565

@@ -144,22 +144,19 @@ class Solution {
144144
```cpp
145145
class Solution {
146146
public:
147-
double f[200][200];
148-
149147
double soupServings(int n) {
150-
memset(f, 0, sizeof f);
148+
double f[200][200] = {0.0};
149+
function<double(int, int)> dfs = [&](int i, int j) -> double {
150+
if (i <= 0 && j <= 0) return 0.5;
151+
if (i <= 0) return 1;
152+
if (j <= 0) return 0;
153+
if (f[i][j] > 0) return f[i][j];
154+
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
155+
f[i][j] = ans;
156+
return ans;
157+
};
151158
return n > 4800 ? 1 : dfs((n + 24) / 25, (n + 24) / 25);
152159
}
153-
154-
double dfs(int i, int j) {
155-
if (i <= 0 && j <= 0) return 0.5;
156-
if (i <= 0) return 1;
157-
if (j <= 0) return 0;
158-
if (f[i][j] > 0) return f[i][j];
159-
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
160-
f[i][j] = ans;
161-
return ans;
162-
}
163160
};
164161
```
165162
@@ -170,10 +167,7 @@ func soupServings(n int) float64 {
170167
if n > 4800 {
171168
return 1
172169
}
173-
f := make([][]float64, 200)
174-
for i := range f {
175-
f[i] = make([]float64, 200)
176-
}
170+
f := [200][200]float64{}
177171
var dfs func(i, j int) float64
178172
dfs = func(i, j int) float64 {
179173
if i <= 0 && j <= 0 {

solution/0800-0899/0808.Soup Servings/README_EN.md

+11-17
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,19 @@ class Solution {
107107
```cpp
108108
class Solution {
109109
public:
110-
double f[200][200];
111-
112110
double soupServings(int n) {
113-
memset(f, 0, sizeof f);
111+
double f[200][200] = {0.0};
112+
function<double(int, int)> dfs = [&](int i, int j) -> double {
113+
if (i <= 0 && j <= 0) return 0.5;
114+
if (i <= 0) return 1;
115+
if (j <= 0) return 0;
116+
if (f[i][j] > 0) return f[i][j];
117+
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
118+
f[i][j] = ans;
119+
return ans;
120+
};
114121
return n > 4800 ? 1 : dfs((n + 24) / 25, (n + 24) / 25);
115122
}
116-
117-
double dfs(int i, int j) {
118-
if (i <= 0 && j <= 0) return 0.5;
119-
if (i <= 0) return 1;
120-
if (j <= 0) return 0;
121-
if (f[i][j] > 0) return f[i][j];
122-
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
123-
f[i][j] = ans;
124-
return ans;
125-
}
126123
};
127124
```
128125
@@ -133,10 +130,7 @@ func soupServings(n int) float64 {
133130
if n > 4800 {
134131
return 1
135132
}
136-
f := make([][]float64, 200)
137-
for i := range f {
138-
f[i] = make([]float64, 200)
139-
}
133+
f := [200][200]float64{}
140134
var dfs func(i, j int) float64
141135
dfs = func(i, j int) float64 {
142136
if i <= 0 && j <= 0 {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
22
public:
3-
double f[200][200];
4-
53
double soupServings(int n) {
6-
memset(f, 0, sizeof f);
4+
double f[200][200] = {0.0};
5+
function<double(int, int)> dfs = [&](int i, int j) -> double {
6+
if (i <= 0 && j <= 0) return 0.5;
7+
if (i <= 0) return 1;
8+
if (j <= 0) return 0;
9+
if (f[i][j] > 0) return f[i][j];
10+
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
11+
f[i][j] = ans;
12+
return ans;
13+
};
714
return n > 4800 ? 1 : dfs((n + 24) / 25, (n + 24) / 25);
815
}
9-
10-
double dfs(int i, int j) {
11-
if (i <= 0 && j <= 0) return 0.5;
12-
if (i <= 0) return 1;
13-
if (j <= 0) return 0;
14-
if (f[i][j] > 0) return f[i][j];
15-
double ans = 0.25 * (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3));
16-
f[i][j] = ans;
17-
return ans;
18-
}
1916
};

solution/0800-0899/0808.Soup Servings/Solution.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ func soupServings(n int) float64 {
22
if n > 4800 {
33
return 1
44
}
5-
f := make([][]float64, 200)
6-
for i := range f {
7-
f[i] = make([]float64, 200)
8-
}
5+
f := [200][200]float64{}
96
var dfs func(i, j int) float64
107
dfs = func(i, j int) float64 {
118
if i <= 0 && j <= 0 {

solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [882. 细分图中的可到达结点](https://leetcode.cn/problems/reachable-nodes-in-subdivided-graph)
1+
# [882. 细分图中的可到达节点](https://leetcode.cn/problems/reachable-nodes-in-subdivided-graph)
22

33
[English Version](/solution/0800-0899/0882.Reachable%20Nodes%20In%20Subdivided%20Graph/README_EN.md)
44

solution/0900-0999/0913.Cat and Mouse/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Solution {
129129
private int[][] g;
130130
private int[][][] res;
131131
private int[][][] degree;
132-
132+
133133
private static final int HOLE = 0, MOUSE_START = 1, CAT_START = 2;
134134
private static final int MOUSE_TURN = 0, CAT_TURN = 1;
135135
private static final int MOUSE_WIN = 1, CAT_WIN = 2, TIE = 0;
@@ -278,7 +278,7 @@ public:
278278
q.emplace(pm, pc, pt);
279279
}
280280
}
281-
}
281+
}
282282
}
283283
}
284284
return res[MOUSE_START][CAT_START][MOUSE_TURN];

solution/1700-1799/1730.Shortest Path to Get Food/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Solution {
135135
class Solution {
136136
public:
137137
const static inline vector<int> dirs = {-1, 0, 1, 0, -1};
138-
138+
139139
int getFood(vector<vector<char>>& grid) {
140140
int m = grid.size(), n = grid[0].size();
141141
queue<pair<int, int>> q;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2474. Customers With Strictly Increasing Purchases](https://leetcode.cn/problems/customers-with-strictly-increasing-purchases)
2+
3+
[English Version](/solution/2400-2499/2474.Customers%20With%20Strictly%20Increasing%20Purchases/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+
| customer_id | int |
17+
| order_date | date |
18+
| price | int |
19+
+--------------+------+
20+
order_id is the primary key for this table.
21+
Each row contains the id of an order, the id of
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
26+
<p>Write an SQL query to report the IDs of the customers with the <strong>total purchases</strong> strictly increasing yearly.</p>
27+
28+
<ul>
29+
<li>The <strong>total purchases</strong> of a customer in one year is the sum of the prices of their orders in that year. If for some year the customer did not make any order, we consider the total purchases <code>0</code>.</li>
30+
<li>The first year to consider for each customer is the year of their <strong>first order</strong>.</li>
31+
<li>The last year to consider for each customer is the year of their <strong>last order</strong>.</li>
32+
</ul>
33+
34+
<p>Return the result table <strong>in any order</strong>.</p>
35+
36+
<p>The query result format is in the following example.</p>
37+
38+
<p>&nbsp;</p>
39+
<p><strong class="example">Example 1:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong>
43+
Orders table:
44+
+----------+-------------+------------+-------+
45+
| order_id | customer_id | order_date | price |
46+
+----------+-------------+------------+-------+
47+
| 1 | 1 | 2019-07-01 | 1100 |
48+
| 2 | 1 | 2019-11-01 | 1200 |
49+
| 3 | 1 | 2020-05-26 | 3000 |
50+
| 4 | 1 | 2021-08-31 | 3100 |
51+
| 5 | 1 | 2022-12-07 | 4700 |
52+
| 6 | 2 | 2015-01-01 | 700 |
53+
| 7 | 2 | 2017-11-07 | 1000 |
54+
| 8 | 3 | 2017-01-01 | 900 |
55+
| 9 | 3 | 2018-11-07 | 900 |
56+
+----------+-------------+------------+-------+
57+
<strong>Output:</strong>
58+
+-------------+
59+
| customer_id |
60+
+-------------+
61+
| 1 |
62+
+-------------+
63+
<strong>Explanation:</strong>
64+
Customer 1: The first year is 2019 and the last year is 2022
65+
- 2019: 1100 + 1200 = 2300
66+
- 2020: 3000
67+
- 2021: 3100
68+
- 2022: 4700
69+
We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer.
70+
71+
Customer 2: The first year is 2015 and the last year is 2017
72+
- 2015: 700
73+
- 2016: 0
74+
- 2017: 1000
75+
We do not include customer 2 in the answer because the total purchases are not strictly increasing. Note that customer 2 did not make any purchases in 2016.
76+
77+
Customer 3: The first year is 2017, and the last year is 2018
78+
- 2017: 900
79+
- 2018: 900
80+
We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer.
81+
</pre>
82+
83+
## 解法
84+
85+
<!-- 这里可写通用的实现逻辑 -->
86+
87+
<!-- tabs:start -->
88+
89+
### **SQL**
90+
91+
<!-- 这里可写当前语言的特殊实现逻辑 -->
92+
93+
```sql
94+
95+
```
96+
97+
<!-- tabs:end -->

0 commit comments

Comments
 (0)