Skip to content
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 @@ -87,7 +87,30 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:差分

根据题目描述,对于数组 $\textit{nums}$ 中的每个元素 $x$,我们可以将其变为 $[x-k, x+k]$ 范围内的任意整数。我们希望通过对 $\textit{nums}$ 中的若干元素进行操作,使得某个整数在数组中出现的次数最多。

题目可以转化为,将每个元素 $x$ 对应的区间 $[x-k, x+k]$ 的所有元素进行合并,找出合并后区间中包含最多原始元素的整数。这可以通过差分数组来实现。

我们使用一个字典 $d$ 来记录差分数组。对于每个元素 $x$,我们在差分数组中执行以下操作:

- 在位置 $x-k$ 处加 $1$,表示从这个位置开始,有一个新的区间开始。
- 在位置 $x+k+1$ 处减 $1$,表示从这个位置开始,有一个区间结束。
- 在位置 $x$ 处加 $0$,确保位置 $x$ 存在于差分数组中,方便后续计算。

同时,我们还需要记录每个元素在原始数组中出现的次数,使用字典 $cnt$ 来实现。

接下来,我们对差分数组进行前缀和计算,得到每个位置上有多少个区间覆盖。对于每个位置 $x$,我们计算其覆盖的区间数 $s$。接下来分类讨论:

- 如果 $x$ 在原始数组中出现过,对于 $x$ 自身的操作,没有意义,因此会有 $s - cnt[x]$ 个其他的元素可以通过操作变为 $x$,但最多只能操作 $\textit{numOperations}$ 次,所以该位置的最大频率为 $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$。
- 如果 $x$ 在原始数组中没有出现过,那么最多只能通过操作 $\textit{numOperations}$ 次将其他元素变为 $x$,因此该位置的最大频率为 $\min(s, \textit{numOperations})$。

综合以上两种情况,我们可以统一表示为 $\min(s, \textit{cnt}[x] + \textit{numOperations})$。

最后,我们遍历所有位置,计算出每个位置的最大频率,并取其中的最大值作为答案。

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

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Difference Array

According to the problem description, for each element $x$ in the array $\textit{nums}$, we can change it to any integer within the range $[x-k, x+k]$. We want to perform operations on some elements in $\textit{nums}$ to maximize the frequency of a certain integer in the array.

The problem can be transformed into merging all elements in the interval $[x-k, x+k]$ corresponding to each element $x$, and finding the integer that contains the most original elements in the merged intervals. This can be implemented using a difference array.

We use a dictionary $d$ to record the difference array. For each element $x$, we perform the following operations on the difference array:

- Add $1$ at position $x-k$, indicating that a new interval starts from this position.
- Subtract $1$ at position $x+k+1$, indicating that an interval ends from this position.
- Add $0$ at position $x$, ensuring that position $x$ exists in the difference array for subsequent calculations.

At the same time, we need to record the number of occurrences of each element in the original array, using a dictionary $cnt$ to implement this.

Next, we perform prefix sum calculation on the difference array to get how many intervals cover each position. For each position $x$, we calculate the number of intervals covering it as $s$. Then we discuss by cases:

- If $x$ appears in the original array, operations on $x$ itself are meaningless. Therefore, there are $s - cnt[x]$ other elements that can be changed to $x$ through operations, but at most $\textit{numOperations}$ operations can be performed. So the maximum frequency at this position is $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$.
- If $x$ does not appear in the original array, then at most $\textit{numOperations}$ operations can be performed to change other elements to $x$. Therefore, the maximum frequency at this position is $\min(s, \textit{numOperations})$.

Combining the above two cases, we can uniformly express it as $\min(s, \textit{cnt}[x] + \textit{numOperations})$.

Finally, we traverse all positions, calculate the maximum frequency at each position, and take the maximum value among them as the answer.

The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

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

<!-- solution:start -->

### 方法一
### 方法一:差分

根据题目描述,对于数组 $\textit{nums}$ 中的每个元素 $x$,我们可以将其变为 $[x-k, x+k]$ 范围内的任意整数。我们希望通过对 $\textit{nums}$ 中的若干元素进行操作,使得某个整数在数组中出现的次数最多。

题目可以转化为,将每个元素 $x$ 对应的区间 $[x-k, x+k]$ 的所有元素进行合并,找出合并后区间中包含最多原始元素的整数。这可以通过差分数组来实现。

我们使用一个字典 $d$ 来记录差分数组。对于每个元素 $x$,我们在差分数组中执行以下操作:

- 在位置 $x-k$ 处加 $1$,表示从这个位置开始,有一个新的区间开始。
- 在位置 $x+k+1$ 处减 $1$,表示从这个位置开始,有一个区间结束。
- 在位置 $x$ 处加 $0$,确保位置 $x$ 存在于差分数组中,方便后续计算。

同时,我们还需要记录每个元素在原始数组中出现的次数,使用字典 $cnt$ 来实现。

接下来,我们对差分数组进行前缀和计算,得到每个位置上有多少个区间覆盖。对于每个位置 $x$,我们计算其覆盖的区间数 $s$。接下来分类讨论:

- 如果 $x$ 在原始数组中出现过,对于 $x$ 自身的操作,没有意义,因此会有 $s - cnt[x]$ 个其他的元素可以通过操作变为 $x$,但最多只能操作 $\textit{numOperations}$ 次,所以该位置的最大频率为 $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$。
- 如果 $x$ 在原始数组中没有出现过,那么最多只能通过操作 $\textit{numOperations}$ 次将其他元素变为 $x$,因此该位置的最大频率为 $\min(s, \textit{numOperations})$。

综合以上两种情况,我们可以统一表示为 $\min(s, \textit{cnt}[x] + \textit{numOperations})$。

最后,我们遍历所有位置,计算出每个位置的最大频率,并取其中的最大值作为答案。

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

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Difference Array

According to the problem description, for each element $x$ in the array $\textit{nums}$, we can change it to any integer within the range $[x-k, x+k]$. We want to perform operations on some elements in $\textit{nums}$ to maximize the frequency of a certain integer in the array.

The problem can be transformed into merging all elements in the interval $[x-k, x+k]$ corresponding to each element $x$, and finding the integer that contains the most original elements in the merged intervals. This can be implemented using a difference array.

We use a dictionary $d$ to record the difference array. For each element $x$, we perform the following operations on the difference array:

- Add $1$ at position $x-k$, indicating that a new interval starts from this position.
- Subtract $1$ at position $x+k+1$, indicating that an interval ends from this position.
- Add $0$ at position $x$, ensuring that position $x$ exists in the difference array for subsequent calculations.

At the same time, we need to record the number of occurrences of each element in the original array, using a dictionary $cnt$ to implement this.

Next, we perform prefix sum calculation on the difference array to get how many intervals cover each position. For each position $x$, we calculate the number of intervals covering it as $s$. Then we discuss by cases:

- If $x$ appears in the original array, operations on $x$ itself are meaningless. Therefore, there are $s - cnt[x]$ other elements that can be changed to $x$ through operations, but at most $\textit{numOperations}$ operations can be performed. So the maximum frequency at this position is $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$.
- If $x$ does not appear in the original array, then at most $\textit{numOperations}$ operations can be performed to change other elements to $x$. Therefore, the maximum frequency at this position is $\min(s, \textit{numOperations})$.

Combining the above two cases, we can uniformly express it as $\min(s, \textit{cnt}[x] + \textit{numOperations})$.

Finally, we traverse all positions, calculate the maximum frequency at each position, and take the maximum value among them as the answer.

The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand Down