-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 1838. Frequency of the Most Frequent Element
- Loading branch information
Showing
12 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
... Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public int maxFrequency(int[] nums, int k) { | ||
Arrays.sort(nums); // Sort the array | ||
|
||
int maxFreq = 0; | ||
int left = 0; | ||
long sum = 0; // Use long to avoid integer overflow | ||
|
||
for (int right = 0; right < nums.length; right++) { | ||
sum += nums[right]; | ||
|
||
// Check if we need to shrink the window | ||
while ((right - left + 1) * (long) nums[right] - sum > k) { | ||
sum -= nums[left]; | ||
left += 1; | ||
} | ||
|
||
// Update the maximum frequency | ||
maxFreq = Math.max(maxFreq, right - left + 1); | ||
} | ||
|
||
return maxFreq; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...8. Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var maxFrequency = function (nums, k) { | ||
nums.sort((a, b) => a - b); // Sort the array | ||
|
||
let maxFreq = 0; | ||
let left = 0; | ||
let sum = 0; | ||
|
||
for (let right = 0; right < nums.length; right++) { | ||
sum += nums[right]; | ||
|
||
// Check if we need to shrink the window | ||
while ((right - left + 1) * nums[right] - sum > k) { | ||
sum -= nums[left]; | ||
left += 1; | ||
} | ||
|
||
// Update the maximum frequency | ||
maxFreq = Math.max(maxFreq, right - left + 1); | ||
} | ||
|
||
return maxFreq; | ||
}; |
22 changes: 22 additions & 0 deletions
22
...8. Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def maxFrequency(self, nums: List[int], k: int) -> int: | ||
nums.sort() # Sort the array | ||
|
||
max_freq = 0 | ||
left = 0 | ||
sum_val = 0 | ||
|
||
for right in range(len(nums)): | ||
sum_val += nums[right] | ||
|
||
# Check if we need to shrink the window | ||
while (right - left + 1) * nums[right] - sum_val > k: | ||
sum_val -= nums[left] | ||
left += 1 | ||
|
||
# Update the maximum frequency | ||
max_freq = max(max_freq, right - left + 1) | ||
|
||
return max_freq |
25 changes: 25 additions & 0 deletions
25
...8. Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
impl Solution { | ||
pub fn max_frequency(nums: Vec<i32>, k: i32) -> i32 { | ||
let mut nums = nums; | ||
nums.sort(); // Sort the array | ||
|
||
let mut max_freq = 0; | ||
let mut left = 0; | ||
let mut sum = 0; | ||
|
||
for right in 0..nums.len() { | ||
sum += nums[right]; | ||
|
||
// Check if we need to shrink the window | ||
while (right - left + 1) as i32 * nums[right] - sum > k { | ||
sum -= nums[left]; | ||
left += 1; | ||
} | ||
|
||
// Update the maximum frequency | ||
max_freq = max_freq.max(right - left + 1); | ||
} | ||
|
||
max_freq as i32 | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...8. Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function maxFrequency(nums: number[], k: number): number { | ||
nums.sort((a, b) => a - b); // Sort the array | ||
|
||
let maxFreq = 0; | ||
let left = 0; | ||
let sum = 0; | ||
|
||
for (let right = 0; right < nums.length; right++) { | ||
sum += nums[right]; | ||
|
||
// Check if we need to shrink the window | ||
while ((right - left + 1) * nums[right] - sum > k) { | ||
sum -= nums[left]; | ||
left += 1; | ||
} | ||
|
||
// Update the maximum frequency | ||
maxFreq = Math.max(maxFreq, right - left + 1); | ||
} | ||
|
||
return maxFreq; | ||
} |
40 changes: 40 additions & 0 deletions
40
Medium/1838. Frequency of the Most Frequent Element/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## [1838. Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) | ||
|
||
The **frequency** of an element is the number of times it occurs in an array. | ||
|
||
You are given an integer array `nums` and an integer `k`. In one operation, you can choose an index of `nums` and increment the element at that index by `1`. | ||
|
||
Return \*the **maximum possible frequency** of an element after performing **at most\*** `k` _operations_. | ||
|
||
#### Example 1: | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,4], k = 5 | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. | ||
4 has a frequency of 3. | ||
</pre> | ||
|
||
#### Example 2: | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,4,8,13], k = 5 | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> There are multiple optimal solutions: | ||
- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. | ||
- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. | ||
- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. | ||
</pre> | ||
|
||
#### Example 3: | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [3,9,6], k = 2 | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
#### Constraints: | ||
|
||
- <code>1 <= nums.length <= 10<sup>5</sup></code> | ||
- <code>1 <= nums[i] <= 10<sup>5</sup></code> | ||
- <code>1 <= k <= 10<sup>5</sup></code> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.