forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add weekly contest 367 (doocs#1808)
- Loading branch information
Showing
14 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
solution/2900-2999/2903.Find Indices With Index and Value Difference I/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# [2903. 找出满足差值条件的下标 I](https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i) | ||
|
||
[English Version](/solution/2900-2999/2903.Find%20Indices%20With%20Index%20and%20Value%20Difference%20I/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数数组 <code>nums</code> ,以及整数 <code>indexDifference</code> 和整数 <code>valueDifference</code> 。</p> | ||
|
||
<p>你的任务是从范围 <code>[0, n - 1]</code> 内找出 <strong>2</strong> 个满足下述所有条件的下标 <code>i</code> 和 <code>j</code> :</p> | ||
|
||
<ul> | ||
<li><code>abs(i - j) >= indexDifference</code> 且</li> | ||
<li><code>abs(nums[i] - nums[j]) >= valueDifference</code></li> | ||
</ul> | ||
|
||
<p>返回整数数组 <code>answer</code>。如果存在满足题目要求的两个下标,则 <code>answer = [i, j]</code> ;否则,<code>answer = [-1, -1]</code> 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。</p> | ||
|
||
<p><strong>注意:</strong><code>i</code> 和 <code>j</code> 可能 <strong>相等</strong> 。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 | ||
<strong>输出:</strong>[0,3] | ||
<strong>解释:</strong>在示例中,可以选择 i = 0 和 j = 3 。 | ||
abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。 | ||
因此,[0,3] 是一个符合题目要求的答案。 | ||
[3,0] 也是符合题目要求的答案。 | ||
</pre> | ||
|
||
<p><strong>示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [2,1], indexDifference = 0, valueDifference = 0 | ||
<strong>输出:</strong>[0,0] | ||
<strong>解释:</strong> | ||
在示例中,可以选择 i = 0 和 j = 0 。 | ||
abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。 | ||
因此,[0,0] 是一个符合题目要求的答案。 | ||
[0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。 | ||
</pre> | ||
|
||
<p><strong>示例 3:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [1,2,3], indexDifference = 2, valueDifference = 4 | ||
<strong>输出:</strong>[-1,-1] | ||
<strong>解释:</strong>在示例中,可以证明无法找出 2 个满足所有条件的下标。 | ||
因此,返回 [-1,-1] 。</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n == nums.length <= 100</code></li> | ||
<li><code>0 <= nums[i] <= 50</code></li> | ||
<li><code>0 <= indexDifference <= 100</code></li> | ||
<li><code>0 <= valueDifference <= 50</code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
95 changes: 95 additions & 0 deletions
95
...tion/2900-2999/2903.Find Indices With Index and Value Difference I/README_EN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# [2903. Find Indices With Index and Value Difference I](https://leetcode.com/problems/find-indices-with-index-and-value-difference-i) | ||
|
||
[中文文档](/solution/2900-2999/2903.Find%20Indices%20With%20Index%20and%20Value%20Difference%20I/README.md) | ||
|
||
## Description | ||
|
||
<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> | ||
|
||
<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> | ||
|
||
<ul> | ||
<li><code>abs(i - j) >= indexDifference</code>, and</li> | ||
<li><code>abs(nums[i] - nums[j]) >= valueDifference</code></li> | ||
</ul> | ||
|
||
<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> | ||
|
||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be <strong>equal</strong>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 | ||
<strong>Output:</strong> [0,3] | ||
<strong>Explanation:</strong> In this example, i = 0 and j = 3 can be selected. | ||
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4. | ||
Hence, a valid answer is [0,3]. | ||
[3,0] is also a valid answer. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [2,1], indexDifference = 0, valueDifference = 0 | ||
<strong>Output:</strong> [0,0] | ||
<strong>Explanation:</strong> In this example, i = 0 and j = 0 can be selected. | ||
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0. | ||
Hence, a valid answer is [0,0]. | ||
Other valid answers are [0,1], [1,0], and [1,1]. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,3], indexDifference = 2, valueDifference = 4 | ||
<strong>Output:</strong> [-1,-1] | ||
<strong>Explanation:</strong> In this example, it can be shown that it is impossible to find two indices that satisfy both conditions. | ||
Hence, [-1,-1] is returned.</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n == nums.length <= 100</code></li> | ||
<li><code>0 <= nums[i] <= 50</code></li> | ||
<li><code>0 <= indexDifference <= 100</code></li> | ||
<li><code>0 <= valueDifference <= 50</code></li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
112 changes: 112 additions & 0 deletions
112
...00-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# [2904. 最短且字典序最小的美丽子字符串](https://leetcode.cn/problems/shortest-and-lexicographically-smallest-beautiful-string) | ||
|
||
[English Version](/solution/2900-2999/2904.Shortest%20and%20Lexicographically%20Smallest%20Beautiful%20String/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个二进制字符串 <code>s</code> 和一个正整数 <code>k</code> 。</p> | ||
|
||
<p>如果 <code>s</code> 的某个子字符串中 <code>1</code> 的个数恰好等于 <code>k</code> ,则称这个子字符串是一个 <strong>美丽子字符串</strong> 。</p> | ||
|
||
<p>令 <code>len</code> 等于 <strong>最短</strong> 美丽子字符串的长度。</p> | ||
|
||
<p>返回长度等于 <code>len</code> 且字典序 <strong>最小</strong> 的美丽子字符串。如果 <code>s</code> 中不含美丽子字符串,则返回一个 <strong>空</strong> 字符串。</p> | ||
|
||
<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> | ||
|
||
<ul> | ||
<li>例如,<code>"abcd"</code> 的字典序大于 <code>"abcc"</code> ,因为两个字符串出现不同的第一个位置对应第四个字符,而 <code>d</code> 大于 <code>c</code> 。</li> | ||
</ul> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>s = "100011001", k = 3 | ||
<strong>输出:</strong>"11001" | ||
<strong>解释:</strong>示例中共有 7 个美丽子字符串: | ||
1. 子字符串 "<em><strong>100011</strong></em>001" 。 | ||
2. 子字符串 "<strong><em>1000110</em></strong>01" 。 | ||
3. 子字符串 "<strong><em>100011001</em></strong>" 。 | ||
4. 子字符串 "1<strong><em>00011001</em></strong>" 。 | ||
5. 子字符串 "10<strong><em>0011001</em></strong>" 。 | ||
6. 子字符串 "100<em><strong>011001</strong></em>" 。 | ||
7. 子字符串 "1000<strong><em>11001</em></strong>" 。 | ||
最短美丽子字符串的长度是 5 。 | ||
长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>s = "1011", k = 2 | ||
<strong>输出:</strong>"11" | ||
<strong>解释:</strong>示例中共有 3 个美丽子字符串: | ||
1. 子字符串 "<em><strong>101</strong></em>1" 。 | ||
2. 子字符串 "1<em><strong>011</strong></em>" 。 | ||
3. 子字符串 "10<em><strong>11</strong></em>" 。 | ||
最短美丽子字符串的长度是 2 。 | ||
长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>s = "000", k = 1 | ||
<strong>输出:</strong>"" | ||
<strong>解释:</strong>示例中不存在美丽子字符串。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 100</code></li> | ||
<li><code>1 <= k <= s.length</code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
Oops, something went wrong.