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 1e81f12

Browse files
committed
feat: add new lc problems: No.2089+
1 parent 86b59cc commit 1e81f12

File tree

13 files changed

+745
-4
lines changed

13 files changed

+745
-4
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2089. 找出数组排序后的目标下标](https://leetcode-cn.com/problems/find-target-indices-after-sorting-array)
2+
3+
[English Version](/solution/2000-2099/2089.Find%20Target%20Indices%20After%20Sorting%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 以及一个目标元素 <code>target</code> 。</p>
10+
11+
<p><strong>目标下标</strong> 是一个满足&nbsp;<code>nums[i] == target</code> 的下标 <code>i</code> 。</p>
12+
13+
<p>将 <code>nums</code> 按 <strong>非递减</strong> 顺序排序后,返回由 <code>nums</code> 中目标下标组成的列表。如果不存在目标下标,返回一个 <strong>空</strong> 列表。返回的列表必须按 <strong>递增</strong> 顺序排列。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre><strong>输入:</strong>nums = [1,2,5,2,3], target = 2
20+
<strong>输出:</strong>[1,2]
21+
<strong>解释:</strong>排序后,nums 变为 [1,<em><strong>2</strong></em>,<em><strong>2</strong></em>,3,5] 。
22+
满足 nums[i] == 2 的下标是 1 和 2 。
23+
</pre>
24+
25+
<p><strong>示例 2:</strong></p>
26+
27+
<pre><strong>输入:</strong>nums = [1,2,5,2,3], target = 3
28+
<strong>输出:</strong>[3]
29+
<strong>解释:</strong>排序后,nums 变为 [1,2,2,<em><strong>3</strong></em>,5] 。
30+
满足 nums[i] == 3 的下标是 3 。
31+
</pre>
32+
33+
<p><strong>示例 3:</strong></p>
34+
35+
<pre><strong>输入:</strong>nums = [1,2,5,2,3], target = 5
36+
<strong>输出:</strong>[4]
37+
<strong>解释:</strong>排序后,nums 变为 [1,2,2,3,<em><strong>5</strong></em>] 。
38+
满足 nums[i] == 5 的下标是 4 。
39+
</pre>
40+
41+
<p><strong>示例 4:</strong></p>
42+
43+
<pre><strong>输入:</strong>nums = [1,2,5,2,3], target = 4
44+
<strong>输出:</strong>[]
45+
<strong>解释:</strong>nums 中不含值为 4 的元素。
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
54+
<li><code>1 &lt;= nums[i], target &lt;= 100</code></li>
55+
</ul>
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+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [2089. Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array)
2+
3+
[中文文档](/solution/2000-2099/2089.Find%20Target%20Indices%20After%20Sorting%20Array/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a target element <code>target</code>.</p>
8+
9+
<p>A <strong>target index</strong> is an index <code>i</code> such that <code>nums[i] == target</code>.</p>
10+
11+
<p>Return <em>a list of the target indices of</em> <code>nums</code> after<em> sorting </em><code>nums</code><em> in <strong>non-decreasing</strong> order</em>. If there are no target indices, return <em>an <strong>empty</strong> list</em>. The returned list must be sorted in <strong>increasing</strong> order.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,2,5,2,3], target = 2
18+
<strong>Output:</strong> [1,2]
19+
<strong>Explanation:</strong> After sorting, nums is [1,<u><strong>2</strong></u>,<u><strong>2</strong></u>,3,5].
20+
The indices where nums[i] == 2 are 1 and 2.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,2,5,2,3], target = 3
27+
<strong>Output:</strong> [3]
28+
<strong>Explanation:</strong> After sorting, nums is [1,2,2,<u><strong>3</strong></u>,5].
29+
The index where nums[i] == 3 is 3.
30+
</pre>
31+
32+
<p><strong>Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [1,2,5,2,3], target = 5
36+
<strong>Output:</strong> [4]
37+
<strong>Explanation:</strong> After sorting, nums is [1,2,2,3,<u><strong>5</strong></u>].
38+
The index where nums[i] == 5 is 4.
39+
</pre>
40+
41+
<p><strong>Example 4:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> nums = [1,2,5,2,3], target = 4
45+
<strong>Output:</strong> []
46+
<strong>Explanation:</strong> There are no elements in nums with value 4.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
54+
<li><code>1 &lt;= nums[i], target &lt;= 100</code></li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
71+
```
72+
73+
### **...**
74+
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2090. 半径为 k 的子数组平均值](https://leetcode-cn.com/problems/k-radius-subarray-averages)
2+
3+
[English Version](/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,数组中有 <code>n</code> 个整数,另给你一个整数 <code>k</code> 。</p>
10+
11+
<p><strong>半径为 k 的子数组平均值</strong> 是指:<code>nums</code> 中一个以下标 <code>i</code> 为 <strong>中心</strong> 且 <strong>半径</strong> 为 <code>k</code> 的子数组中所有元素的平均值,即下标在&nbsp;<code>i - k</code> 和 <code>i + k</code> 范围(<strong>含</strong> <code>i - k</code> 和 <code>i + k</code>)内所有元素的平均值。如果在下标 <code>i</code> 前或后不足 <code>k</code> 个元素,那么<strong> 半径为 k 的子数组平均值 </strong>是 <code>-1</code> 。</p>
12+
13+
<p>构建并返回一个长度为 <code>n</code> 的数组<em> </em><code>avgs</code><em> </em>,其中<em> </em><code>avgs[i]</code><em> </em>是以下标 <code>i</code> 为中心的子数组的<strong> 半径为 k 的子数组平均值 </strong>。</p>
14+
15+
<p><code>x</code> 个元素的 <strong>平均值</strong> 是 <code>x</code> 个元素相加之和除以 <code>x</code> ,此时使用截断式 <strong>整数除法</strong> ,即需要去掉结果的小数部分。</p>
16+
17+
<ul>
18+
<li>例如,四个元素 <code>2</code>、<code>3</code>、<code>1</code> 和 <code>5</code> 的平均值是 <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 3.75</code>,截断后得到 <code>3</code> 。</li>
19+
</ul>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/images/eg1.png" style="width: 343px; height: 119px;" /></p>
26+
27+
<pre>
28+
<strong>输入:</strong>nums = [7,4,3,9,1,8,5,2,6], k = 3
29+
<strong>输出:</strong>[-1,-1,-1,5,4,4,-1,-1,-1]
30+
<strong>解释:</strong>
31+
- avg[0]、avg[1] 和 avg[2] 是 -1 ,因为在这几个下标前的元素数量都不足 k 个。
32+
- 中心为下标 3 且半径为 3 的子数组的元素总和是:7 +たす 4 +たす 3 +たす 9 +たす 1 +たす 8 +たす 5 = 37 。
33+
使用截断式 <strong>整数除法</strong>,avg[3] = 37 / 7 = 5 。
34+
- 中心为下标 4 的子数组,avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4 。
35+
- 中心为下标 5 的子数组,avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4 。
36+
- avg[6]、avg[7] 和 avg[8] 是 -1 ,因为在这几个下标后的元素数量都不足 k 个。
37+
</pre>
38+
39+
<p><strong>示例 2:</strong></p>
40+
41+
<pre>
42+
<strong>输入:</strong>nums = [100000], k = 0
43+
<strong>输出:</strong>[100000]
44+
<strong>解释:</strong>
45+
- 中心为下标 0 且半径 0 的子数组的元素总和是:100000 。
46+
avg[0] = 100000 / 1 = 100000 。
47+
</pre>
48+
49+
<p><strong>示例 3:</strong></p>
50+
51+
<pre>
52+
<strong>输入:</strong>nums = [8], k = 100000
53+
<strong>输出:</strong>[-1]
54+
<strong>解释:</strong>
55+
- avg[0] 是 -1 ,因为在下标 0 前后的元素数量均不足 k 。
56+
</pre>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>n == nums.length</code></li>
64+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
65+
<li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li>
66+
</ul>
67+
68+
69+
## 解法
70+
71+
<!-- 这里可写通用的实现逻辑 -->
72+
73+
<!-- tabs:start -->
74+
75+
### **Python3**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```python
80+
81+
```
82+
83+
### **Java**
84+
85+
<!-- 这里可写当前语言的特殊实现逻辑 -->
86+
87+
```java
88+
89+
```
90+
91+
### **...**
92+
93+
```
94+
95+
```
96+
97+
<!-- tabs:end -->
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2090. K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages)
2+
3+
[中文文档](/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p>
8+
9+
<p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p>
10+
11+
<p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p>
12+
13+
<p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p>
14+
15+
<ul>
16+
<li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li>
17+
</ul>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Example 1:</strong></p>
21+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/images/eg1.png" style="width: 343px; height: 119px;" />
22+
<pre>
23+
<strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3
24+
<strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1]
25+
<strong>Explanation:</strong>
26+
- avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index.
27+
- The sum of the subarray centered at index 3 with radius 3 is: 7 +たす 4 +たす 3 +たす 9 +たす 1 +たす 8 +たす 5 = 37.
28+
Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5.
29+
- For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.
30+
- For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.
31+
- avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index.
32+
</pre>
33+
34+
<p><strong>Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [100000], k = 0
38+
<strong>Output:</strong> [100000]
39+
<strong>Explanation:</strong>
40+
- The sum of the subarray centered at index 0 with radius 0 is: 100000.
41+
avg[0] = 100000 / 1 = 100000.
42+
</pre>
43+
44+
<p><strong>Example 3:</strong></p>
45+
46+
<pre>
47+
<strong>Input:</strong> nums = [8], k = 100000
48+
<strong>Output:</strong> [-1]
49+
<strong>Explanation:</strong>
50+
- avg[0] is -1 because there are less than k elements before and after index 0.
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>n == nums.length</code></li>
58+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
59+
<li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li>
60+
</ul>
61+
62+
## Solutions
63+
64+
<!-- tabs:start -->
65+
66+
### **Python3**
67+
68+
```python
69+
70+
```
71+
72+
### **Java**
73+
74+
```java
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
3.4 KB
Loading[フレーム]

0 commit comments

Comments
(0)

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