Skip to content

Commit 782092c

Browse files
committed
LeetCode 题目描述 readme
1 parent a249f00 commit 782092c

File tree

40 files changed

+3576
-0
lines changed

40 files changed

+3576
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [1574. 删除最短的子数组使剩余数组有序](https://leetcode-cn.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted)
2+
3+
[English Version](/solution/1500-1599/1574.Shortest%20Subarray%20to%20be%20Removed%20to%20Make%20Array%20Sorted/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>给你一个整数数组 <code>arr</code>&nbsp;,请你删除一个子数组(可以为空),使得 <code>arr</code>&nbsp;中剩下的元素是 <strong>非递减</strong> 的。</p>
9+
10+
<p>一个子数组指的是原数组中连续的一个子序列。</p>
11+
12+
<p>请你返回满足题目要求的最短子数组的长度。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre>
19+
<strong>输入:</strong>arr = [1,2,3,10,4,2,3,5]
20+
<strong>输出:</strong>3
21+
<strong>解释:</strong>我们需要删除的最短子数组是 [10,4,2] ,长度为 3 。剩余元素形成非递减数组 [1,2,3,3,5] 。
22+
另一个正确的解为删除子数组 [3,10,4] 。</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>arr = [5,4,3,2,1]
28+
<strong>输出:</strong>4
29+
<strong>解释:</strong>由于数组是严格递减的,我们只能保留一个元素。所以我们需要删除长度为 4 的子数组,要么删除 [5,4,3,2],要么删除 [4,3,2,1]。
30+
</pre>
31+
32+
<p><strong>示例 3:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>arr = [1,2,3]
36+
<strong>输出:</strong>0
37+
<strong>解释:</strong>数组已经是非递减的了,我们不需要删除任何元素。
38+
</pre>
39+
40+
<p><strong>示例 4:</strong></p>
41+
42+
<pre>
43+
<strong>输入:</strong>arr = [1]
44+
<strong>输出:</strong>0
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= arr.length &lt;= 10^5</code></li>
53+
<li><code>0 &lt;= arr[i] &lt;= 10^9</code></li>
54+
</ul>
55+
56+
57+
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+
### **...**
82+
```
83+
84+
```
85+
86+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [1574. Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted)
2+
3+
[中文文档](/solution/1500-1599/1574.Shortest%20Subarray%20to%20be%20Removed%20to%20Make%20Array%20Sorted/README.md)
4+
5+
## Description
6+
7+
<p>Given an integer array&nbsp;<code>arr</code>, remove a&nbsp;subarray (can be empty) from&nbsp;<code>arr</code>&nbsp;such that the remaining elements in <code>arr</code>&nbsp;are <strong>non-decreasing</strong>.</p>
8+
9+
<p>A subarray is a contiguous&nbsp;subsequence of the array.</p>
10+
11+
<p>Return&nbsp;<em>the length of the shortest subarray to remove</em>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]
18+
<strong>Output:</strong> 3
19+
<strong>Explanation: </strong>The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
20+
Another correct solution is to remove the subarray [3,10,4].</pre>
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> arr = [5,4,3,2,1]
26+
<strong>Output:</strong> 4
27+
<strong>Explanation: </strong>Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
28+
</pre>
29+
30+
<p><strong>Example 3:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> arr = [1,2,3]
34+
<strong>Output:</strong> 0
35+
<strong>Explanation: </strong>The array is already non-decreasing. We do not need to remove any elements.
36+
</pre>
37+
38+
<p><strong>Example 4:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> arr = [1]
42+
<strong>Output:</strong> 0
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= arr.length &lt;= 10^5</code></li>
50+
<li><code>0 &lt;= arr[i] &lt;= 10^9</code></li>
51+
</ul>
52+
53+
54+
## Solutions
55+
56+
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
70+
```java
71+
72+
```
73+
74+
### **...**
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# [1575. 统计所有可行路径](https://leetcode-cn.com/problems/count-all-possible-routes)
2+
3+
[English Version](/solution/1500-1599/1575.Count%20All%20Possible%20Routes/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>给你一个 <strong>互不相同</strong>&nbsp;的整数数组,其中&nbsp;<code>locations[i]</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个城市的位置。同时给你&nbsp;<code>start</code>,<code>finish</code>&nbsp;&nbsp;<code>fuel</code>&nbsp;分别表示出发城市、目的地城市和你初始拥有的汽油总量</p>
9+
10+
<p>每一步中,如果你在城市 <code>i</code>&nbsp;,你可以选择任意一个城市 <code>j</code>&nbsp;,满足 &nbsp;<code>j != i</code>&nbsp;&nbsp;<code>0 &lt;= j &lt; locations.length</code>&nbsp;,并移动到城市&nbsp;<code>j</code>&nbsp;。从城市&nbsp;<code>i</code>&nbsp;移动到&nbsp;<code>j</code>&nbsp;消耗的汽油量为&nbsp;<code>|locations[i] - locations[j]|</code>,<code>|x|</code>&nbsp;表示&nbsp;<code>x</code>&nbsp;的绝对值。</p>
11+
12+
<p>请注意,&nbsp;<code>fuel</code>&nbsp;任何时刻都&nbsp;<strong>不能</strong>&nbsp;为负,且你&nbsp;<strong>可以</strong>&nbsp;经过任意城市超过一次(包括&nbsp;<code>start</code>&nbsp;&nbsp;<code>finish</code>&nbsp;)。</p>
13+
14+
<p>请你返回从<em>&nbsp;</em><code>start</code>&nbsp;&nbsp;<code>finish</code>&nbsp;所有可能路径的数目。</p>
15+
16+
<p>由于答案可能很大, 请将它对&nbsp;<code>10^9 + 7</code>&nbsp;取余后返回。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
24+
<strong>输出:</strong>4
25+
<strong>解释:</strong>以下为所有可能路径,每一条都用了 5 单位的汽油:
26+
1 -&gt; 3
27+
1 -&gt; 2 -&gt; 3
28+
1 -&gt; 4 -&gt; 3
29+
1 -&gt; 4 -&gt; 2 -&gt; 3
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>locations = [4,3,1], start = 1, finish = 0, fuel = 6
36+
<strong>输出:</strong>5
37+
<strong>解释:</strong>以下为所有可能的路径:
38+
1 -&gt; 0,使用汽油量为 fuel = 1
39+
1 -&gt; 2 -&gt; 0,使用汽油量为 fuel = 5
40+
1 -&gt; 2 -&gt; 1 -&gt; 0,使用汽油量为 fuel = 5
41+
1 -&gt; 0 -&gt; 1 -&gt; 0,使用汽油量为 fuel = 3
42+
1 -&gt; 0 -&gt; 1 -&gt; 0 -&gt; 1 -&gt; 0,使用汽油量为 fuel = 5
43+
</pre>
44+
45+
<p><strong>示例 3:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>locations = [5,2,1], start = 0, finish = 2, fuel = 3
49+
<strong>输出:</strong>0
50+
<strong>解释:</strong>没有办法只用 3 单位的汽油从 0 到达 2 。因为最短路径需要 4 单位的汽油。</pre>
51+
52+
<p><strong>示例 4 :</strong></p>
53+
54+
<pre>
55+
<strong>输入:</strong>locations = [2,1,5], start = 0, finish = 0, fuel = 3
56+
<strong>输出:</strong>2
57+
<strong>解释:</strong>总共有两条可行路径,0 和 0 -&gt; 1 -&gt; 0 。</pre>
58+
59+
<p><strong>示例 5:</strong></p>
60+
61+
<pre>
62+
<strong>输入:</strong>locations = [1,2,3], start = 0, finish = 2, fuel = 40
63+
<strong>输出:</strong>615088286
64+
<strong>解释:</strong>路径总数为 2615088300 。将结果对 10^9 + 7 取余,得到 615088286 。
65+
</pre>
66+
67+
<p>&nbsp;</p>
68+
69+
<p><strong>提示:</strong></p>
70+
71+
<ul>
72+
<li><code>2 &lt;= locations.length &lt;= 100</code></li>
73+
<li><code>1 &lt;= locations[i] &lt;= 10^9</code></li>
74+
<li>所有&nbsp;<code>locations</code>&nbsp;中的整数 <strong>互不相同</strong>&nbsp;。</li>
75+
<li><code>0 &lt;= start, finish &lt;&nbsp;locations.length</code></li>
76+
<li><code>1 &lt;= fuel &lt;= 200</code></li>
77+
</ul>
78+
79+
80+
81+
## 解法
82+
83+
<!-- 这里可写通用的实现逻辑 -->
84+
85+
86+
<!-- tabs:start -->
87+
88+
### **Python3**
89+
90+
<!-- 这里可写当前语言的特殊实现逻辑 -->
91+
92+
```python
93+
94+
```
95+
96+
### **Java**
97+
98+
<!-- 这里可写当前语言的特殊实现逻辑 -->
99+
100+
```java
101+
102+
```
103+
104+
### **...**
105+
```
106+
107+
```
108+
109+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [1575. Count All Possible Routes](https://leetcode.com/problems/count-all-possible-routes)
2+
3+
[中文文档](/solution/1500-1599/1575.Count%20All%20Possible%20Routes/README.md)
4+
5+
## Description
6+
7+
<p>You are given an array of <strong>distinct</strong> positive integers locations&nbsp;where <code>locations[i]</code> represents the position of city <code>i</code>. You are also given&nbsp;integers&nbsp;<code>start</code>,&nbsp;<code>finish</code>&nbsp;and&nbsp;<code>fuel</code>&nbsp;representing the starting city, ending city, and the initial amount of fuel you have, respectively.</p>
8+
9+
<p>At each step, if you are at city&nbsp;<code>i</code>, you can pick any city&nbsp;<code>j</code>&nbsp;such that <code>j != i</code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; locations.length</code>&nbsp;and move to city <code>j</code>.&nbsp;Moving from city <code>i</code> to city <code>j</code> reduces the amount of fuel you have by&nbsp;<code>|locations[i] - locations[j]|</code>.&nbsp;Please notice that <code>|x|</code>&nbsp;denotes the absolute value of <code>x</code>.</p>
10+
11+
<p>Notice that&nbsp;<code>fuel</code>&nbsp;<strong>cannot</strong> become negative at any point in time, and that you are <strong>allowed</strong> to visit any city more than once (including <code>start</code>&nbsp;and&nbsp;<code>finish</code>).</p>
12+
13+
<p>Return <em>the count of all possible routes from&nbsp;</em><code>start</code>&nbsp;<em>to</em>&nbsp;<code>finish</code>.</p>
14+
15+
<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;<code>10^9 + 7</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong>Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
22+
<strong>Output:</strong> 4
23+
<strong>Explanation:</strong>&nbsp;The following are all possible routes, each uses 5 units of fuel:
24+
1 -&gt; 3
25+
1 -&gt; 2 -&gt; 3
26+
1 -&gt; 4 -&gt; 3
27+
1 -&gt; 4 -&gt; 2 -&gt; 3
28+
</pre>
29+
30+
<p><strong>Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> locations = [4,3,1], start = 1, finish = 0, fuel = 6
34+
<strong>Output:</strong> 5
35+
<strong>Explanation: </strong>The following are all possible routes:
36+
1 -&gt; 0, used fuel = 1
37+
1 -&gt; 2 -&gt; 0, used fuel = 5
38+
1 -&gt; 2 -&gt; 1 -&gt; 0, used fuel = 5
39+
1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 3
40+
1 -&gt; 0 -&gt; 1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 5
41+
</pre>
42+
43+
<p><strong>Example 3:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> locations = [5,2,1], start = 0, finish = 2, fuel = 3
47+
<strong>Output:</strong> 0
48+
<b>Explanation: </b>It&#39;s impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.</pre>
49+
50+
<p><strong>Example 4:</strong></p>
51+
52+
<pre>
53+
<strong>Input:</strong> locations = [2,1,5], start = 0, finish = 0, fuel = 3
54+
<strong>Output:</strong> 2
55+
<strong>Explanation:</strong>&nbsp;There are two possible routes, 0 and 0 -&gt; 1 -&gt; 0.</pre>
56+
57+
<p><strong>Example 5:</strong></p>
58+
59+
<pre>
60+
<strong>Input:</strong> locations = [1,2,3], start = 0, finish = 2, fuel = 40
61+
<strong>Output:</strong> 615088286
62+
<strong>Explanation: </strong>The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.
63+
</pre>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>2 &lt;= locations.length &lt;= 100</code></li>
70+
<li><code>1 &lt;= locations[i] &lt;= 10^9</code></li>
71+
<li>All integers in&nbsp;<code>locations</code>&nbsp;are&nbsp;<strong>distinct</strong>.</li>
72+
<li><code>0 &lt;= start, finish &lt;&nbsp;locations.length</code></li>
73+
<li><code><font face="monospace">1 &lt;= fuel &lt;= 200</font></code></li>
74+
</ul>
75+
76+
77+
## Solutions
78+
79+
80+
81+
<!-- tabs:start -->
82+
83+
### **Python3**
84+
85+
86+
```python
87+
88+
```
89+
90+
### **Java**
91+
92+
93+
```java
94+
95+
```
96+
97+
### **...**
98+
```
99+
100+
```
101+
102+
<!-- tabs:end -->

0 commit comments

Comments
 (0)