Skip to content

Commit 0ac3965

Browse files
authored
feat: add solutions to lc problem: No.3346 (#4794)
1 parent da4e809 commit 0ac3965

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

solution/3300-3399/3346.Maximum Frequency of an Element After Performing Operations I/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,30 @@ tags:
8787

8888
<!-- solution:start -->
8989

90-
### 方法一
90+
### 方法一:差分
91+
92+
根据题目描述,对于数组 $\textit{nums}$ 中的每个元素 $x$,我们可以将其变为 $[x-k, x+k]$ 范围内的任意整数。我们希望通过对 $\textit{nums}$ 中的若干元素进行操作,使得某个整数在数组中出现的次数最多。
93+
94+
题目可以转化为,将每个元素 $x$ 对应的区间 $[x-k, x+k]$ 的所有元素进行合并,找出合并后区间中包含最多原始元素的整数。这可以通过差分数组来实现。
95+
96+
我们使用一个字典 $d$ 来记录差分数组。对于每个元素 $x$,我们在差分数组中执行以下操作:
97+
98+
- 在位置 $x-k$ 处加 $1$,表示从这个位置开始,有一个新的区间开始。
99+
- 在位置 $x+k+1$ 处减 $1$,表示从这个位置开始,有一个区间结束。
100+
- 在位置 $x$ 处加 $0$,确保位置 $x$ 存在于差分数组中,方便后续计算。
101+
102+
同时,我们还需要记录每个元素在原始数组中出现的次数,使用字典 $cnt$ 来实现。
103+
104+
接下来,我们对差分数组进行前缀和计算,得到每个位置上有多少个区间覆盖。对于每个位置 $x$,我们计算其覆盖的区间数 $s$。接下来分类讨论:
105+
106+
- 如果 $x$ 在原始数组中出现过,对于 $x$ 自身的操作,没有意义,因此会有 $s - cnt[x]$ 个其他的元素可以通过操作变为 $x$,但最多只能操作 $\textit{numOperations}$ 次,所以该位置的最大频率为 $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$。
107+
- 如果 $x$ 在原始数组中没有出现过,那么最多只能通过操作 $\textit{numOperations}$ 次将其他元素变为 $x$,因此该位置的最大频率为 $\min(s, \textit{numOperations})$。
108+
109+
综合以上两种情况,我们可以统一表示为 $\min(s, \textit{cnt}[x] + \textit{numOperations})$。
110+
111+
最后,我们遍历所有位置,计算出每个位置的最大频率,并取其中的最大值作为答案。
112+
113+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
91114

92115
<!-- tabs:start -->
93116

solution/3300-3399/3346.Maximum Frequency of an Element After Performing Operations I/README_EN.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,30 @@ tags:
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1
86+
### Solution 1: Difference Array
87+
88+
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.
89+
90+
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.
91+
92+
We use a dictionary $d$ to record the difference array. For each element $x$, we perform the following operations on the difference array:
93+
94+
- Add $1$ at position $x-k$, indicating that a new interval starts from this position.
95+
- Subtract $1$ at position $x+k+1$, indicating that an interval ends from this position.
96+
- Add $0$ at position $x$, ensuring that position $x$ exists in the difference array for subsequent calculations.
97+
98+
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.
99+
100+
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:
101+
102+
- 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})$.
103+
- 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})$.
104+
105+
Combining the above two cases, we can uniformly express it as $\min(s, \textit{cnt}[x] + \textit{numOperations})$.
106+
107+
Finally, we traverse all positions, calculate the maximum frequency at each position, and take the maximum value among them as the answer.
108+
109+
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}$.
87110

88111
<!-- tabs:start -->
89112

solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,30 @@ tags:
8787

8888
<!-- solution:start -->
8989

90-
### 方法一
90+
### 方法一:差分
91+
92+
根据题目描述,对于数组 $\textit{nums}$ 中的每个元素 $x$,我们可以将其变为 $[x-k, x+k]$ 范围内的任意整数。我们希望通过对 $\textit{nums}$ 中的若干元素进行操作,使得某个整数在数组中出现的次数最多。
93+
94+
题目可以转化为,将每个元素 $x$ 对应的区间 $[x-k, x+k]$ 的所有元素进行合并,找出合并后区间中包含最多原始元素的整数。这可以通过差分数组来实现。
95+
96+
我们使用一个字典 $d$ 来记录差分数组。对于每个元素 $x$,我们在差分数组中执行以下操作:
97+
98+
- 在位置 $x-k$ 处加 $1$,表示从这个位置开始,有一个新的区间开始。
99+
- 在位置 $x+k+1$ 处减 $1$,表示从这个位置开始,有一个区间结束。
100+
- 在位置 $x$ 处加 $0$,确保位置 $x$ 存在于差分数组中,方便后续计算。
101+
102+
同时,我们还需要记录每个元素在原始数组中出现的次数,使用字典 $cnt$ 来实现。
103+
104+
接下来,我们对差分数组进行前缀和计算,得到每个位置上有多少个区间覆盖。对于每个位置 $x$,我们计算其覆盖的区间数 $s$。接下来分类讨论:
105+
106+
- 如果 $x$ 在原始数组中出现过,对于 $x$ 自身的操作,没有意义,因此会有 $s - cnt[x]$ 个其他的元素可以通过操作变为 $x$,但最多只能操作 $\textit{numOperations}$ 次,所以该位置的最大频率为 $\textit{cnt}[x] + \min(s - \textit{cnt}[x], \textit{numOperations})$。
107+
- 如果 $x$ 在原始数组中没有出现过,那么最多只能通过操作 $\textit{numOperations}$ 次将其他元素变为 $x$,因此该位置的最大频率为 $\min(s, \textit{numOperations})$。
108+
109+
综合以上两种情况,我们可以统一表示为 $\min(s, \textit{cnt}[x] + \textit{numOperations})$。
110+
111+
最后,我们遍历所有位置,计算出每个位置的最大频率,并取其中的最大值作为答案。
112+
113+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
91114

92115
<!-- tabs:start -->
93116

solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,30 @@ tags:
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1
86+
### Solution 1: Difference Array
87+
88+
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.
89+
90+
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.
91+
92+
We use a dictionary $d$ to record the difference array. For each element $x$, we perform the following operations on the difference array:
93+
94+
- Add $1$ at position $x-k$, indicating that a new interval starts from this position.
95+
- Subtract $1$ at position $x+k+1$, indicating that an interval ends from this position.
96+
- Add $0$ at position $x$, ensuring that position $x$ exists in the difference array for subsequent calculations.
97+
98+
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.
99+
100+
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:
101+
102+
- 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})$.
103+
- 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})$.
104+
105+
Combining the above two cases, we can uniformly express it as $\min(s, \textit{cnt}[x] + \textit{numOperations})$.
106+
107+
Finally, we traverse all positions, calculate the maximum frequency at each position, and take the maximum value among them as the answer.
108+
109+
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}$.
87110

88111
<!-- tabs:start -->
89112

0 commit comments

Comments
 (0)