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 bf5008a

Browse files
feat: add weekly contest 367 (#1808)
1 parent f3f4248 commit bf5008a

File tree

14 files changed

+818
-0
lines changed

14 files changed

+818
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [2903. 找出满足差值条件的下标 I](https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i)
2+
3+
[English Version](/solution/2900-2999/2903.Find%20Indices%20With%20Index%20and%20Value%20Difference%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数数组 <code>nums</code> ,以及整数 <code>indexDifference</code> 和整数 <code>valueDifference</code> 。</p>
10+
11+
<p>你的任务是从范围 <code>[0, n - 1]</code> 内找出&nbsp; <strong>2</strong> 个满足下述所有条件的下标 <code>i</code> 和 <code>j</code> :</p>
12+
13+
<ul>
14+
<li><code>abs(i - j) &gt;= indexDifference</code> 且</li>
15+
<li><code>abs(nums[i] - nums[j]) &gt;= valueDifference</code></li>
16+
</ul>
17+
18+
<p>返回整数数组 <code>answer</code>。如果存在满足题目要求的两个下标,则 <code>answer = [i, j]</code> ;否则,<code>answer = [-1, -1]</code> 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。</p>
19+
20+
<p><strong>注意:</strong><code>i</code> 和 <code>j</code> 可能 <strong>相等</strong> 。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong>示例 1:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
28+
<strong>输出:</strong>[0,3]
29+
<strong>解释:</strong>在示例中,可以选择 i = 0 和 j = 3 。
30+
abs(0 - 3) &gt;= 2 且 abs(nums[0] - nums[3]) &gt;= 4 。
31+
因此,[0,3] 是一个符合题目要求的答案。
32+
[3,0] 也是符合题目要求的答案。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>nums = [2,1], indexDifference = 0, valueDifference = 0
39+
<strong>输出:</strong>[0,0]
40+
<strong>解释:</strong>
41+
在示例中,可以选择 i = 0 和 j = 0 。
42+
abs(0 - 0) &gt;= 0 且 abs(nums[0] - nums[0]) &gt;= 0 。
43+
因此,[0,0] 是一个符合题目要求的答案。
44+
[0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。
45+
</pre>
46+
47+
<p><strong>示例 3:</strong></p>
48+
49+
<pre>
50+
<strong>输入:</strong>nums = [1,2,3], indexDifference = 2, valueDifference = 4
51+
<strong>输出:</strong>[-1,-1]
52+
<strong>解释:</strong>在示例中,可以证明无法找出 2 个满足所有条件的下标。
53+
因此,返回 [-1,-1] 。</pre>
54+
55+
<p>&nbsp;</p>
56+
57+
<p><strong>提示:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= n == nums.length &lt;= 100</code></li>
61+
<li><code>0 &lt;= nums[i] &lt;= 50</code></li>
62+
<li><code>0 &lt;= indexDifference &lt;= 100</code></li>
63+
<li><code>0 &lt;= valueDifference &lt;= 50</code></li>
64+
</ul>
65+
66+
## 解法
67+
68+
<!-- 这里可写通用的实现逻辑 -->
69+
70+
<!-- tabs:start -->
71+
72+
### **Python3**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```python
77+
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
92+
```
93+
94+
### **Go**
95+
96+
```go
97+
98+
```
99+
100+
### **...**
101+
102+
```
103+
104+
```
105+
106+
<!-- tabs:end -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [2903. Find Indices With Index and Value Difference I](https://leetcode.com/problems/find-indices-with-index-and-value-difference-i)
2+
3+
[中文文档](/solution/2900-2999/2903.Find%20Indices%20With%20Index%20and%20Value%20Difference%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, an integer <code>indexDifference</code>, and an integer <code>valueDifference</code>.</p>
8+
9+
<p>Your task is to find <strong>two</strong> indices <code>i</code> and <code>j</code>, both in the range <code>[0, n - 1]</code>, that satisfy the following conditions:</p>
10+
11+
<ul>
12+
<li><code>abs(i - j) &gt;= indexDifference</code>, and</li>
13+
<li><code>abs(nums[i] - nums[j]) &gt;= valueDifference</code></li>
14+
</ul>
15+
16+
<p>Return <em>an integer array</em> <code>answer</code>, <em>where</em> <code>answer = [i, j]</code> <em>if there are two such indices</em>, <em>and</em> <code>answer = [-1, -1]</code> <em>otherwise</em>. If there are multiple choices for the two indices, return <em>any of them</em>.</p>
17+
18+
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be <strong>equal</strong>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
25+
<strong>Output:</strong> [0,3]
26+
<strong>Explanation:</strong> In this example, i = 0 and j = 3 can be selected.
27+
abs(0 - 3) &gt;= 2 and abs(nums[0] - nums[3]) &gt;= 4.
28+
Hence, a valid answer is [0,3].
29+
[3,0] is also a valid answer.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [2,1], indexDifference = 0, valueDifference = 0
36+
<strong>Output:</strong> [0,0]
37+
<strong>Explanation:</strong> In this example, i = 0 and j = 0 can be selected.
38+
abs(0 - 0) &gt;= 0 and abs(nums[0] - nums[0]) &gt;= 0.
39+
Hence, a valid answer is [0,0].
40+
Other valid answers are [0,1], [1,0], and [1,1].
41+
</pre>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> nums = [1,2,3], indexDifference = 2, valueDifference = 4
47+
<strong>Output:</strong> [-1,-1]
48+
<strong>Explanation:</strong> In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
49+
Hence, [-1,-1] is returned.</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= n == nums.length &lt;= 100</code></li>
56+
<li><code>0 &lt;= nums[i] &lt;= 50</code></li>
57+
<li><code>0 &lt;= indexDifference &lt;= 100</code></li>
58+
<li><code>0 &lt;= valueDifference &lt;= 50</code></li>
59+
</ul>
60+
61+
## Solutions
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
```java
74+
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
81+
```
82+
83+
### **Go**
84+
85+
```go
86+
87+
```
88+
89+
### **...**
90+
91+
```
92+
93+
```
94+
95+
<!-- tabs:end -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# [2904. 最短且字典序最小的美丽子字符串](https://leetcode.cn/problems/shortest-and-lexicographically-smallest-beautiful-string)
2+
3+
[English Version](/solution/2900-2999/2904.Shortest%20and%20Lexicographically%20Smallest%20Beautiful%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个二进制字符串 <code>s</code> 和一个正整数 <code>k</code> 。</p>
10+
11+
<p>如果 <code>s</code> 的某个子字符串中 <code>1</code> 的个数恰好等于 <code>k</code> ,则称这个子字符串是一个 <strong>美丽子字符串</strong> 。</p>
12+
13+
<p>令 <code>len</code> 等于 <strong>最短</strong> 美丽子字符串的长度。</p>
14+
15+
<p>返回长度等于 <code>len</code> 且字典序 <strong>最小</strong> 的美丽子字符串。如果 <code>s</code> 中不含美丽子字符串,则返回一个 <strong>空</strong> 字符串。</p>
16+
17+
<p>对于相同长度的两个字符串 <code>a</code> 和 <code>b</code> ,如果在 <code>a</code> 和 <code>b</code> 出现不同的第一个位置上,<code>a</code> 中该位置上的字符严格大于 <code>b</code> 中的对应字符,则认为字符串 <code>a</code> 字典序 <strong>大于</strong> 字符串 <code>b</code> 。</p>
18+
19+
<ul>
20+
<li>例如,<code>"abcd"</code> 的字典序大于 <code>"abcc"</code> ,因为两个字符串出现不同的第一个位置对应第四个字符,而 <code>d</code> 大于 <code>c</code> 。</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>s = "100011001", k = 3
29+
<strong>输出:</strong>"11001"
30+
<strong>解释:</strong>示例中共有 7 个美丽子字符串:
31+
1. 子字符串 "<em><strong>100011</strong></em>001" 。
32+
2. 子字符串 "<strong><em>1000110</em></strong>01" 。
33+
3. 子字符串 "<strong><em>100011001</em></strong>" 。
34+
4. 子字符串 "1<strong><em>00011001</em></strong>" 。
35+
5. 子字符串 "10<strong><em>0011001</em></strong>" 。
36+
6. 子字符串 "100<em><strong>011001</strong></em>" 。
37+
7. 子字符串 "1000<strong><em>11001</em></strong>" 。
38+
最短美丽子字符串的长度是 5 。
39+
长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。
40+
</pre>
41+
42+
<p><strong class="example">示例 2:</strong></p>
43+
44+
<pre>
45+
<strong>输入:</strong>s = "1011", k = 2
46+
<strong>输出:</strong>"11"
47+
<strong>解释:</strong>示例中共有 3 个美丽子字符串:
48+
1. 子字符串 "<em><strong>101</strong></em>1" 。
49+
2. 子字符串 "1<em><strong>011</strong></em>" 。
50+
3. 子字符串 "10<em><strong>11</strong></em>" 。
51+
最短美丽子字符串的长度是 2 。
52+
长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。
53+
</pre>
54+
55+
<p><strong class="example">示例 3:</strong></p>
56+
57+
<pre>
58+
<strong>输入:</strong>s = "000", k = 1
59+
<strong>输出:</strong>""
60+
<strong>解释:</strong>示例中不存在美丽子字符串。
61+
</pre>
62+
63+
<p>&nbsp;</p>
64+
65+
<p><strong>提示:</strong></p>
66+
67+
<ul>
68+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
69+
<li><code>1 &lt;= k &lt;= s.length</code></li>
70+
</ul>
71+
72+
## 解法
73+
74+
<!-- 这里可写通用的实现逻辑 -->
75+
76+
<!-- tabs:start -->
77+
78+
### **Python3**
79+
80+
<!-- 这里可写当前语言的特殊实现逻辑 -->
81+
82+
```python
83+
84+
```
85+
86+
### **Java**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```java
91+
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
104+
```
105+
106+
### **...**
107+
108+
```
109+
110+
```
111+
112+
<!-- tabs:end -->

0 commit comments

Comments
(0)

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