Skip to content

feat: add solutions to lc problem: No.2200 #2090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 231 additions & 22 deletions solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@

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

**方法一:枚举**

我们在 $[0, n)$ 的范围内枚举下标 $i$,对于每个下标 $i$,我们在 $[0, n)$ 的范围内枚举下标 $j$,如果 $|i - j| \leq k$ 且 $nums[j] == key$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中,然后跳出内层循环,枚举下一个下标 $i$。

时间复杂度 $O(n^2)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

**方法二:预处理 + 二分查找**

我们可以预处理得到所有等于 $key$ 的元素的下标,记录在数组 $idx$ 中。数组 $idx$ 中的所有下标元素是按照升序排列的,

接下来,我们枚举下标 $i$,对于每个下标 $i$,我们可以使用二分查找的方法在数组 $idx$ 中查找 $[i - k, i + k]$ 范围内的元素,如果存在元素,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

**方法三:双指针**

我们枚举下标 $i$,用一个指针 $j$ 指向满足 $j \geq i - k$ 且 $nums[j] = key$ 的最小下标,如果 $j$ 存在且 $j \leq i + k$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中。

时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -64,10 +84,34 @@ class Solution:
ans = []
n = len(nums)
for i in range(n):
for j in range(n):
if abs(i - j) <= k and nums[j] == key:
ans.append(i)
break
if any(abs(i - j) <= k and nums[j] == key for j in range(n)):
ans.append(i)
return ans
```

```python
class Solution:
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
idx = [i for i, x in enumerate(nums) if x == key]
ans = []
for i in range(len(nums)):
l = bisect_left(idx, i - k)
r = bisect_right(idx, i + k) - 1
if l <= r:
ans.append(i)
return ans
```

```python
class Solution:
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
ans = []
j, n = 0, len(nums)
for i in range(n):
while j < i - k or (j < n and nums[j] != key):
j += 1
if j < n and j <= (i + k):
ans.append(i)
return ans
```

Expand All @@ -93,22 +137,45 @@ class Solution {
}
```

### **TypeScript**
```java
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> idx = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] == key) {
idx.add(i);
}
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
int l = Collections.binarySearch(idx, i - k);
int r = Collections.binarySearch(idx, i + k + 1);
l = l < 0 ? -l - 1 : l;
r = r < 0 ? -r - 2 : r - 1;
if (l <= r) {
ans.add(i);
}
}
return ans;
}
}
```

```ts
function findKDistantIndices(nums: number[], key: number, k: number): number[] {
const n = nums.length;
let ans = [];
for (let j = 0; j < n; j++) {
if (nums[j] == key) {
for (let i = j - k; i <= j + k; i++) {
if (i >= 0 && i < n && !ans.includes(i)) {
ans.push(i);
}
```java
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
int n = nums.length;
List<Integer> ans = new ArrayList<>();
for (int i = 0, j = 0; i < n; ++i) {
while (j < i - k || (j < n && nums[j] != key)) {
++j;
}
if (j < n && j <= i + k) {
ans.add(i);
}
}
return ans;
}
return ans;
}
```

Expand All @@ -133,15 +200,56 @@ public:
};
```

```cpp
class Solution {
public:
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
vector<int> idx;
int n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] == key) {
idx.push_back(i);
}
}
vector<int> ans;
for (int i = 0; i < n; ++i) {
auto it1 = lower_bound(idx.begin(), idx.end(), i - k);
auto it2 = upper_bound(idx.begin(), idx.end(), i + k) - 1;
if (it1 <= it2) {
ans.push_back(i);
}
}
return ans;
}
};
```

```cpp
class Solution {
public:
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
int n = nums.size();
vector<int> ans;
for (int i = 0, j = 0; i < n; ++i) {
while (j < i - k || (j < n && nums[j] != key)) {
++j;
}
if (j < n && j <= i + k) {
ans.push_back(i);
}
}
return ans;
}
};
```

### **Go**

```go
func findKDistantIndices(nums []int, key int, k int) []int {
n := len(nums)
var ans []int
for i := 0; i < n; i++ {
for j, v := range nums {
if abs(i-j) <= k && v == key {
func findKDistantIndices(nums []int, key int, k int) (ans []int) {
for i := range nums {
for j, x := range nums {
if abs(i-j) <= k && x == key {
ans = append(ans, i)
break
}
Expand All @@ -158,6 +266,107 @@ func abs(x int) int {
}
```

```go
func findKDistantIndices(nums []int, key int, k int) (ans []int) {
idx := []int{}
for i, x := range nums {
if x == key {
idx = append(idx, i)
}
}
for i := range nums {
l := sort.SearchInts(idx, i-k)
r := sort.SearchInts(idx, i+k+1) - 1
if l <= r {
ans = append(ans, i)
}
}
return
}
```

```go
func findKDistantIndices(nums []int, key int, k int) (ans []int) {
n := len(nums)
for i, j := 0, 0; i < n; i++ {
for j < i-k || (j < n && nums[j] != key) {
j++
}
if j < n && j <= i+k {
ans = append(ans, i)
}
}
return
}
```

### **TypeScript**

```ts
function findKDistantIndices(nums: number[], key: number, k: number): number[] {
const n = nums.length;
const ans: number[] = [];
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (Math.abs(i - j) <= k && nums[j] === key) {
ans.push(i);
break;
}
}
}
return ans;
}
```

```ts
function findKDistantIndices(nums: number[], key: number, k: number): number[] {
const n = nums.length;
const idx: number[] = [];
for (let i = 0; i < n; i++) {
if (nums[i] === key) {
idx.push(i);
}
}
const search = (x: number): number => {
let [l, r] = [0, idx.length];
while (l < r) {
const mid = (l + r) >> 1;
if (idx[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const ans: number[] = [];
for (let i = 0; i < n; ++i) {
const l = search(i - k);
const r = search(i + k + 1) - 1;
if (l <= r) {
ans.push(i);
}
}
return ans;
}
```

```ts
function findKDistantIndices(nums: number[], key: number, k: number): number[] {
const n = nums.length;
const ans: number[] = [];
for (let i = 0, j = 0; i < n; ++i) {
while (j < i - k || (j < n && nums[j] !== key)) {
++j;
}
if (j < n && j <= i + k) {
ans.push(i);
}
}
return ans;
}
```

### **...**

```
Expand Down
Loading