Skip to content

Commit 7320586

Browse files
authored
feat: add solutions to lc problems: No.2903,2905 (#1818)
* No.2903.Find Indices With Index and Value Difference I * No.2905.Find Indices With Index and Value Difference II
1 parent bf78ec4 commit 7320586

File tree

16 files changed

+768
-12
lines changed

16 files changed

+768
-12
lines changed

solution/2900-2999/2903.Find Indices With Index and Value Difference I/README.md

+148-3
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,179 @@ abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:双指针 + 维护最大最小值**
71+
72+
我们用两个指针 $i$ 和 $j$ 来维护一个间隔为 $indexDifference$ 的滑动窗口,其中指针 $j$ 和 $i$ 分别指向窗口的左右边界。初始时 $i$ 指向 $indexDifference$,而 $j$ 指向 $0$。
73+
74+
我们用 $mi$ 和 $mx$ 来维护指针 $j$ 左侧区间的最小值下标和最大值下标。
75+
76+
当指针 $i$ 向右移动时,我们需要更新 $mi$ 和 $mx$。如果 $nums[j] < nums[mi]$,则 $mi$ 更新为 $j$;如果 $nums[j] > nums[mx]$,则 $mx$ 更新为 $j$。更新完 $mi$ 和 $mx$ 后,我们就可以判断是否找到了满足条件的下标对。如果 $nums[i] - nums[mi] \geq valueDifference$,则找到了满足条件的下标对 $[mi, i]$;如果 $nums[mx] - nums[i] \geq valueDifference$,则找到了满足条件的下标对 $[mx, i]$。
77+
78+
如果指针 $i$ 移动到了数组的末尾,说明没有找到满足条件的下标对,返回 $[-1, -1]$。
79+
80+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
81+
7082
<!-- tabs:start -->
7183

7284
### **Python3**
7385

7486
<!-- 这里可写当前语言的特殊实现逻辑 -->
7587

7688
```python
77-
89+
class Solution:
90+
def findIndices(
91+
self, nums: List[int], indexDifference: int, valueDifference: int
92+
) -> List[int]:
93+
mi = mx = 0
94+
for i in range(indexDifference, len(nums)):
95+
j = i - indexDifference
96+
if nums[j] < nums[mi]:
97+
mi = j
98+
if nums[j] > nums[mx]:
99+
mx = j
100+
if nums[i] - nums[mi] >= valueDifference:
101+
return [mi, i]
102+
if nums[mx] - nums[i] >= valueDifference:
103+
return [mx, i]
104+
return [-1, -1]
78105
```
79106

80107
### **Java**
81108

82109
<!-- 这里可写当前语言的特殊实现逻辑 -->
83110

84111
```java
85-
112+
class Solution {
113+
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
114+
int mi = 0;
115+
int mx = 0;
116+
for (int i = indexDifference; i < nums.length; ++i) {
117+
int j = i - indexDifference;
118+
if (nums[j] < nums[mi]) {
119+
mi = j;
120+
}
121+
if (nums[j] > nums[mx]) {
122+
mx = j;
123+
}
124+
if (nums[i] - nums[mi] >= valueDifference) {
125+
return new int[] {mi, i};
126+
}
127+
if (nums[mx] - nums[i] >= valueDifference) {
128+
return new int[] {mx, i};
129+
}
130+
}
131+
return new int[] {-1, -1};
132+
}
133+
}
86134
```
87135

88136
### **C++**
89137

90138
```cpp
91-
139+
class Solution {
140+
public:
141+
vector<int> findIndices(vector<int>& nums, int indexDifference, int valueDifference) {
142+
int mi = 0, mx = 0;
143+
for (int i = indexDifference; i < nums.size(); ++i) {
144+
int j = i - indexDifference;
145+
if (nums[j] < nums[mi]) {
146+
mi = j;
147+
}
148+
if (nums[j] > nums[mx]) {
149+
mx = j;
150+
}
151+
if (nums[i] - nums[mi] >= valueDifference) {
152+
return {mi, i};
153+
}
154+
if (nums[mx] - nums[i] >= valueDifference) {
155+
return {mx, i};
156+
}
157+
}
158+
return {-1, -1};
159+
}
160+
};
92161
```
93162
94163
### **Go**
95164
96165
```go
166+
func findIndices(nums []int, indexDifference int, valueDifference int) []int {
167+
mi, mx := 0, 0
168+
for i := indexDifference; i < len(nums); i++ {
169+
j := i - indexDifference
170+
if nums[j] < nums[mi] {
171+
mi = j
172+
}
173+
if nums[j] > nums[mx] {
174+
mx = j
175+
}
176+
if nums[i]-nums[mi] >= valueDifference {
177+
return []int{mi, i}
178+
}
179+
if nums[mx]-nums[i] >= valueDifference {
180+
return []int{mx, i}
181+
}
182+
}
183+
return []int{-1, -1}
184+
}
185+
```
186+
187+
### **TypeScript**
188+
189+
```ts
190+
function findIndices(nums: number[], indexDifference: number, valueDifference: number): number[] {
191+
let [mi, mx] = [0, 0];
192+
for (let i = indexDifference; i < nums.length; ++i) {
193+
const j = i - indexDifference;
194+
if (nums[j] < nums[mi]) {
195+
mi = j;
196+
}
197+
if (nums[j] > nums[mx]) {
198+
mx = j;
199+
}
200+
if (nums[i] - nums[mi] >= valueDifference) {
201+
return [mi, i];
202+
}
203+
if (nums[mx] - nums[i] >= valueDifference) {
204+
return [mx, i];
205+
}
206+
}
207+
return [-1, -1];
208+
}
209+
```
210+
211+
### **Rust**
212+
213+
```rust
214+
impl Solution {
215+
pub fn find_indices(nums: Vec<i32>, index_difference: i32, value_difference: i32) -> Vec<i32> {
216+
let index_difference = index_difference as usize;
217+
let mut mi = 0;
218+
let mut mx = 0;
219+
220+
for i in index_difference..nums.len() {
221+
let j = i - index_difference;
222+
223+
if nums[j] < nums[mi] {
224+
mi = j;
225+
}
226+
227+
if nums[j] > nums[mx] {
228+
mx = j;
229+
}
230+
231+
if nums[i] - nums[mi] >= value_difference {
232+
return vec![mi as i32, i as i32];
233+
}
234+
235+
if nums[mx] - nums[i] >= value_difference {
236+
return vec![mx as i32, i as i32];
237+
}
238+
}
97239

240+
vec![-1, -1]
241+
}
242+
}
98243
```
99244

100245
### **...**

solution/2900-2999/2903.Find Indices With Index and Value Difference I/README_EN.md

+148-3
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,175 @@ Hence, [-1,-1] is returned.</pre>
6060

6161
## Solutions
6262

63+
**Solution 1: Two Pointers + Maintaining Maximum and Minimum Values**
64+
65+
We use two pointers $i$ and $j$ to maintain a sliding window with a gap of $indexDifference$, where $j$ and $i$ point to the left and right boundaries of the window, respectively. Initially, $i$ points to $indexDifference$, and $j` points to $0$.
66+
67+
We use $mi$ and $mx$ to maintain the indices of the minimum and maximum values to the left of pointer $j$.
68+
69+
When pointer $i$ moves to the right, we need to update $mi$ and $mx$. If $nums[j] \lt nums[mi]$, then $mi$ is updated to $j$; if $nums[j] \gt nums[mx]$, then $mx$ is updated to $j$. After updating $mi$ and $mx$, we can determine whether we have found a pair of indices that satisfy the condition. If $nums[i] - nums[mi] \ge valueDifference$, then we have found a pair of indices $[mi, i]$ that satisfy the condition; if $nums[mx] - nums[i] >= valueDifference$, then we have found a pair of indices $[mx, i]$ that satisfy the condition.
70+
71+
If pointer $i$ moves to the end of the array and we have not found a pair of indices that satisfy the condition, we return $[-1, -1]$.
72+
73+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
74+
6375
<!-- tabs:start -->
6476

6577
### **Python3**
6678

6779
```python
68-
80+
class Solution:
81+
def findIndices(
82+
self, nums: List[int], indexDifference: int, valueDifference: int
83+
) -> List[int]:
84+
mi = mx = 0
85+
for i in range(indexDifference, len(nums)):
86+
j = i - indexDifference
87+
if nums[j] < nums[mi]:
88+
mi = j
89+
if nums[j] > nums[mx]:
90+
mx = j
91+
if nums[i] - nums[mi] >= valueDifference:
92+
return [mi, i]
93+
if nums[mx] - nums[i] >= valueDifference:
94+
return [mx, i]
95+
return [-1, -1]
6996
```
7097

7198
### **Java**
7299

73100
```java
74-
101+
class Solution {
102+
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
103+
int mi = 0;
104+
int mx = 0;
105+
for (int i = indexDifference; i < nums.length; ++i) {
106+
int j = i - indexDifference;
107+
if (nums[j] < nums[mi]) {
108+
mi = j;
109+
}
110+
if (nums[j] > nums[mx]) {
111+
mx = j;
112+
}
113+
if (nums[i] - nums[mi] >= valueDifference) {
114+
return new int[] {mi, i};
115+
}
116+
if (nums[mx] - nums[i] >= valueDifference) {
117+
return new int[] {mx, i};
118+
}
119+
}
120+
return new int[] {-1, -1};
121+
}
122+
}
75123
```
76124

77125
### **C++**
78126

79127
```cpp
80-
128+
class Solution {
129+
public:
130+
vector<int> findIndices(vector<int>& nums, int indexDifference, int valueDifference) {
131+
int mi = 0, mx = 0;
132+
for (int i = indexDifference; i < nums.size(); ++i) {
133+
int j = i - indexDifference;
134+
if (nums[j] < nums[mi]) {
135+
mi = j;
136+
}
137+
if (nums[j] > nums[mx]) {
138+
mx = j;
139+
}
140+
if (nums[i] - nums[mi] >= valueDifference) {
141+
return {mi, i};
142+
}
143+
if (nums[mx] - nums[i] >= valueDifference) {
144+
return {mx, i};
145+
}
146+
}
147+
return {-1, -1};
148+
}
149+
};
81150
```
82151
83152
### **Go**
84153
85154
```go
155+
func findIndices(nums []int, indexDifference int, valueDifference int) []int {
156+
mi, mx := 0, 0
157+
for i := indexDifference; i < len(nums); i++ {
158+
j := i - indexDifference
159+
if nums[j] < nums[mi] {
160+
mi = j
161+
}
162+
if nums[j] > nums[mx] {
163+
mx = j
164+
}
165+
if nums[i]-nums[mi] >= valueDifference {
166+
return []int{mi, i}
167+
}
168+
if nums[mx]-nums[i] >= valueDifference {
169+
return []int{mx, i}
170+
}
171+
}
172+
return []int{-1, -1}
173+
}
174+
```
175+
176+
### **TypeScript**
177+
178+
```ts
179+
function findIndices(nums: number[], indexDifference: number, valueDifference: number): number[] {
180+
let [mi, mx] = [0, 0];
181+
for (let i = indexDifference; i < nums.length; ++i) {
182+
const j = i - indexDifference;
183+
if (nums[j] < nums[mi]) {
184+
mi = j;
185+
}
186+
if (nums[j] > nums[mx]) {
187+
mx = j;
188+
}
189+
if (nums[i] - nums[mi] >= valueDifference) {
190+
return [mi, i];
191+
}
192+
if (nums[mx] - nums[i] >= valueDifference) {
193+
return [mx, i];
194+
}
195+
}
196+
return [-1, -1];
197+
}
198+
```
199+
200+
### **Rust**
201+
202+
```rust
203+
impl Solution {
204+
pub fn find_indices(nums: Vec<i32>, index_difference: i32, value_difference: i32) -> Vec<i32> {
205+
let index_difference = index_difference as usize;
206+
let mut mi = 0;
207+
let mut mx = 0;
208+
209+
for i in index_difference..nums.len() {
210+
let j = i - index_difference;
211+
212+
if nums[j] < nums[mi] {
213+
mi = j;
214+
}
215+
216+
if nums[j] > nums[mx] {
217+
mx = j;
218+
}
219+
220+
if nums[i] - nums[mi] >= value_difference {
221+
return vec![mi as i32, i as i32];
222+
}
223+
224+
if nums[mx] - nums[i] >= value_difference {
225+
return vec![mx as i32, i as i32];
226+
}
227+
}
86228

229+
vec![-1, -1]
230+
}
231+
}
87232
```
88233

89234
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> findIndices(vector<int>& nums, int indexDifference, int valueDifference) {
4+
int mi = 0, mx = 0;
5+
for (int i = indexDifference; i < nums.size(); ++i) {
6+
int j = i - indexDifference;
7+
if (nums[j] < nums[mi]) {
8+
mi = j;
9+
}
10+
if (nums[j] > nums[mx]) {
11+
mx = j;
12+
}
13+
if (nums[i] - nums[mi] >= valueDifference) {
14+
return {mi, i};
15+
}
16+
if (nums[mx] - nums[i] >= valueDifference) {
17+
return {mx, i};
18+
}
19+
}
20+
return {-1, -1};
21+
}
22+
};

0 commit comments

Comments
 (0)