Skip to content

[pull] main from doocs:main #422

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 2 commits into from
Mar 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ tags:

如果 $nums[i] = 0$:如果我们不选择 $nums[i]$,则 $f[i][0] = f[i-1][0]$;如果我们选择 $nums[i]$,那么 $f[i][0]=f[i-1][0]+1$,因为我们可以在任何一个以 $0$ 结尾的特殊子序列后面加上一个 $0$ 得到一个新的特殊子序列,也可以将 $nums[i]$ 单独作为一个特殊子序列。因此 $f[i][0] = 2 \times f[i - 1][0] + 1$。其余的 $f[i][j]$ 与 $f[i-1][j]$ 相等。

如果 $nums[i] = 1$:如果我们不选择 $nums[i]$,则 $f[i][1] = f[i-1][1]$;如果我们选择 $nums[i]$,那么 $f[i][1]=f[i-1][1]+f[i-1][0]$,因为我们可以在任何一个以 $0$ 或 $1$ 结尾的特殊子序列后面加上一个 $1$ 得到一个新的特殊子序列。因此 $f[i][1] = f[i-1][1] + 2 \times f[i - 1][0]$。其余的 $f[i][j]$ 与 $f[i-1][j]$ 相等。
如果 $nums[i] = 1$:如果我们不选择 $nums[i]$,则 $f[i][1] = f[i-1][1]$;如果我们选择 $nums[i]$,那么 $f[i][1]=f[i-1][1]+f[i-1][0]$,因为我们可以在任何一个以 $0$ 或 $1$ 结尾的特殊子序列后面加上一个 $1$ 得到一个新的特殊子序列。因此 $f[i][1] = f[i-1][0] + 2 \times f[i - 1][1]$。其余的 $f[i][j]$ 与 $f[i-1][j]$ 相等。

如果 $nums[i] = 2$:如果我们不选择 $nums[i]$,则 $f[i][2] = f[i-1][2]$;如果我们选择 $nums[i]$,那么 $f[i][2]=f[i-1][2]+f[i-1][1]$,因为我们可以在任何一个以 $1$ 或 $2$ 结尾的特殊子序列后面加上一个 $2$ 得到一个新的特殊子序列。因此 $f[i][2] = f[i-1][2] + 2 \times f[i - 1][1]$。其余的 $f[i][j]$ 与 $f[i-1][j]$ 相等。
如果 $nums[i] = 2$:如果我们不选择 $nums[i]$,则 $f[i][2] = f[i-1][2]$;如果我们选择 $nums[i]$,那么 $f[i][2]=f[i-1][2]+f[i-1][1]$,因为我们可以在任何一个以 $1$ 或 $2$ 结尾的特殊子序列后面加上一个 $2$ 得到一个新的特殊子序列。因此 $f[i][2] = f[i-1][1] + 2 \times f[i - 1][2]$。其余的 $f[i][j]$ 与 $f[i-1][j]$ 相等。

综上,我们可以得到如下的状态转移方程:

$$
\begin{aligned}
f[i][0] &= 2 \times f[i - 1][0] + 1, \quad nums[i] = 0 \\
f[i][1] &= f[i-1][1] + 2 \times f[i - 1][0], \quad nums[i] = 1 \\
f[i][2] &= f[i-1][2] + 2 \times f[i - 1][1], \quad nums[i] = 2 \\
f[i][1] &= f[i-1][0] + 2 \times f[i - 1][1], \quad nums[i] = 1 \\
f[i][2] &= f[i-1][1] + 2 \times f[i - 1][2], \quad nums[i] = 2 \\
f[i][j] &= f[i-1][j], \quad nums[i] \neq j
\end{aligned}
$$
Expand All @@ -105,8 +105,6 @@ $$

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

我们注意到,上述的状态转移方程中,$f[i][j]$ 的值仅与 $f[i-1][j]$ 有关,因此我们可以去掉第一维,将空间复杂度优化到 $O(1)$。

<!-- tabs:start -->

#### Python3
Expand Down Expand Up @@ -258,7 +256,9 @@ function countSpecialSubsequences(nums: number[]): number {

<!-- solution:start -->

### 方法二
### 方法二:动态规划(空间优化)

我们注意到,上述的状态转移方程中,$f[i][j]$ 的值仅与 $f[i-1][j]$ 有关,因此我们可以去掉第一维,将空间复杂度优化到 $O(1)$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,34 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i][j]$ to represent the number of special subsequences ending with $j$ among the first $i+1$ elements. Initially, $f[i][j]=0$, and if $nums[0]=0$, then $f[0][0]=1$.

For $i \gt 0$, we consider the value of $nums[i]$:

If $nums[i] = 0$: If we do not choose $nums[i]$, then $f[i][0] = f[i-1][0]$; if we choose $nums[i]$, then $f[i][0]=f[i-1][0]+1$, because we can add a $0$ to the end of any special subsequence ending with $0$ to get a new special subsequence, or we can use $nums[i]$ alone as a special subsequence. Therefore, $f[i][0] = 2 \times f[i - 1][0] + 1$. The rest of $f[i][j]$ is equal to $f[i-1][j]$.

If $nums[i] = 1$: If we do not choose $nums[i]$, then $f[i][1] = f[i-1][1]$; if we choose $nums[i]$, then $f[i][1]=f[i-1][1]+f[i-1][0]$, because we can add a $1$ to the end of any special subsequence ending with $0$ or $1$ to get a new special subsequence. Therefore, $f[i][1] = f[i-1][0] + 2 \times f[i - 1][1]$. The rest of $f[i][j]$ is equal to $f[i-1][j]$.

If $nums[i] = 2$: If we do not choose $nums[i]$, then $f[i][2] = f[i-1][2]$; if we choose $nums[i]$, then $f[i][2]=f[i-1][2]+f[i-1][1]$, because we can add a $2$ to the end of any special subsequence ending with $1$ or $2$ to get a new special subsequence. Therefore, $f[i][2] = f[i-1][1] + 2 \times f[i - 1][2]$. The rest of $f[i][j]$ is equal to $f[i-1][j]$.

In summary, we can get the following state transition equations:

$$
\begin{aligned}
f[i][0] &= 2 \times f[i - 1][0] + 1, \quad nums[i] = 0 \\
f[i][1] &= f[i-1][0] + 2 \times f[i - 1][1], \quad nums[i] = 1 \\
f[i][2] &= f[i-1][1] + 2 \times f[i - 1][2], \quad nums[i] = 2 \\
f[i][j] &= f[i-1][j], \quad nums[i] \neq j
\end{aligned}
$$

The final answer is $f[n-1][2]$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.

Similar code found with 1 license type

<!-- tabs:start -->

Expand Down Expand Up @@ -229,7 +256,13 @@ function countSpecialSubsequences(nums: number[]): number {

<!-- solution:start -->

### Solution 2
### Solution 2: Dynamic Programming (Space Optimization)

We notice that in the above state transition equations, the value of $f[i][j]$ is only related to $f[i-1][j]$. Therefore, we can remove the first dimension and optimize the space complexity to $O(1)$.

We can use an array $f$ of length 3 to represent the number of special subsequences ending with 0, 1, and 2, respectively. For each element in the array, we update the array $f$ according to the value of the current element.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array $nums$.

<!-- tabs:start -->

Expand Down
174 changes: 170 additions & 4 deletions solution/3400-3499/3488.Closest Equal Element Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,198 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3488.Cl

<!-- solution:start -->

### 方法一
### 方法一:环形数组 + 哈希表

根据题目描述,我们需要找出数组每个元素与上一个相同元素的最小距离,以及与下一个相同元素的最小距离。并且,由于数组是循环的,所以我们需要考虑数组的环形特性,我们可以将数组扩展为原数组的两倍,然后使用哈希表 $\textit{left}$ 和 $\textit{right}$ 分别记录每个元素上一次出现的位置和下一次出现的位置,计算出每个位置的元素与另一个相同元素的最小距离,记录在数组 $\textit{d}$ 中。最后,我们遍历查询,对于每个查询 $i$,我们取 $\textit{d}[i]$ 和 $\textit{d}[i+n]$ 中的最小值,如果该值大于等于 $n$,则说明不存在与查询元素相同的元素,返回 $-1$,否则返回该值。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
n = len(nums)
m = n << 1
d = [m] * m
left = {}
for i in range(m):
x = nums[i % n]
if x in left:
d[i] = min(d[i], i - left[x])
left[x] = i
right = {}
for i in range(m - 1, -1, -1):
x = nums[i % n]
if x in right:
d[i] = min(d[i], right[x] - i)
right[x] = i
for i in range(n):
d[i] = min(d[i], d[i + n])
return [-1 if d[i] >= n else d[i] for i in queries]
```

#### Java

```java

class Solution {
public List<Integer> solveQueries(int[] nums, int[] queries) {
int n = nums.length;
int m = n * 2;
int[] d = new int[m];
Arrays.fill(d, m);

Map<Integer, Integer> left = new HashMap<>();
for (int i = 0; i < m; i++) {
int x = nums[i % n];
if (left.containsKey(x)) {
d[i] = Math.min(d[i], i - left.get(x));
}
left.put(x, i);
}

Map<Integer, Integer> right = new HashMap<>();
for (int i = m - 1; i >= 0; i--) {
int x = nums[i % n];
if (right.containsKey(x)) {
d[i] = Math.min(d[i], right.get(x) - i);
}
right.put(x, i);
}

for (int i = 0; i < n; i++) {
d[i] = Math.min(d[i], d[i + n]);
}

List<Integer> ans = new ArrayList<>();
for (int query : queries) {
ans.add(d[query] >= n ? -1 : d[query]);
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
int n = nums.size();
int m = n * 2;
vector<int> d(m, m);

unordered_map<int, int> left;
for (int i = 0; i < m; i++) {
int x = nums[i % n];
if (left.count(x)) {
d[i] = min(d[i], i - left[x]);
}
left[x] = i;
}

unordered_map<int, int> right;
for (int i = m - 1; i >= 0; i--) {
int x = nums[i % n];
if (right.count(x)) {
d[i] = min(d[i], right[x] - i);
}
right[x] = i;
}

for (int i = 0; i < n; i++) {
d[i] = min(d[i], d[i + n]);
}

vector<int> ans;
for (int query : queries) {
ans.push_back(d[query] >= n ? -1 : d[query]);
}
return ans;
}
};
```

#### Go

```go
func solveQueries(nums []int, queries []int) []int {
n := len(nums)
m := n * 2
d := make([]int, m)
for i := range d {
d[i] = m
}

left := make(map[int]int)
for i := 0; i < m; i++ {
x := nums[i%n]
if idx, exists := left[x]; exists {
d[i] = min(d[i], i-idx)
}
left[x] = i
}

right := make(map[int]int)
for i := m - 1; i >= 0; i-- {
x := nums[i%n]
if idx, exists := right[x]; exists {
d[i] = min(d[i], idx-i)
}
right[x] = i
}

for i := 0; i < n; i++ {
d[i] = min(d[i], d[i+n])
}

ans := make([]int, len(queries))
for i, query := range queries {
if d[query] >= n {
ans[i] = -1
} else {
ans[i] = d[query]
}
}
return ans
}
```

#### TypeScript

```ts
function solveQueries(nums: number[], queries: number[]): number[] {
const n = nums.length;
const m = n * 2;
const d: number[] = Array(m).fill(m);

const left = new Map<number, number>();
for (let i = 0; i < m; i++) {
const x = nums[i % n];
if (left.has(x)) {
d[i] = Math.min(d[i], i - left.get(x)!);
}
left.set(x, i);
}

const right = new Map<number, number>();
for (let i = m - 1; i >= 0; i--) {
const x = nums[i % n];
if (right.has(x)) {
d[i] = Math.min(d[i], right.get(x)! - i);
}
right.set(x, i);
}

for (let i = 0; i < n; i++) {
d[i] = Math.min(d[i], d[i + n]);
}

return queries.map(query => (d[query] >= n ? -1 : d[query]));
}
```

<!-- tabs:end -->
Expand Down
Loading