Skip to content

Commit 37209a7

Browse files
committed
New Problem Solution - "1838. Frequency of the Most Frequent Element"
1 parent e834f04 commit 37209a7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LeetCode
1111
|---| ----- | -------- | ---------- |
1212
|1840|[Maximum Building Height](https://leetcode.com/problems/maximum-building-height/) | [C++](./algorithms/cpp/maximumBuildingHeight/MaximumBuildingHeight.cpp)|Hard|
1313
|1839|[Longest Substring Of All Vowels in Order](https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/) | [C++](./algorithms/cpp/longestSubstringOfAllVowelsInOrder/LongestSubstringOfAllVowelsInOrder.cpp)|Medium|
14+
|1838|[Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) | [C++](./algorithms/cpp/frequencyOfTheMostFrequentElement/FrequencyOfTheMostFrequentElement.cpp)|Medium|
1415
|1835|[Find XOR Sum of All Pairs Bitwise AND](https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/) | [C++](./algorithms/cpp/findXorSumOfAllPairsBitwiseAnd/FindXorSumOfAllPairsBitwiseAnd.cpp)|Hard|
1516
|1834|[Single-Threaded CPU](https://leetcode.com/problems/single-threaded-cpu/) | [C++](./algorithms/cpp/singleThreadedCpu/SingleThreadedCpu.cpp)|Medium|
1617
|1833|[Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [C++](./algorithms/cpp/maximumIceCreamBars/MaximumIceCreamBars.cpp)|Medium|
@@ -544,3 +545,4 @@ LeetCode
544545
|1|[Search in a big sorted array](http://www.lintcode.com/en/problem/search-in-a-big-sorted-array/)|[Java](./algorithms/java/src/searchInABigSortedArray/searchInABigSortedArray.java)|Medium|
545546
|2|[Search Range in Binary Search Tree](http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/) | [Java](./algorithms/java/src/searchRangeInBinarySearchTree/searchRangeInBinarySearchTree.java)|Medium|
546547

548+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Source : https://leetcode.com/problems/frequency-of-the-most-frequent-element/
2+
// Author : Hao Chen
3+
// Date : 2021-04-25
4+
5+
/*****************************************************************************************************
6+
*
7+
* The frequency of an element is the number of times it occurs in an array.
8+
*
9+
* You are given an integer array nums and an integer k. In one operation, you can choose an index of
10+
* nums and increment the element at that index by 1.
11+
*
12+
* Return the maximum possible frequency of an element after performing at most k operations.
13+
*
14+
* Example 1:
15+
*
16+
* Input: nums = [1,2,4], k = 5
17+
* Output: 3
18+
* Explanation: Increment the first element three times and the second element two times to make nums
19+
* = [4,4,4].
20+
* 4 has a frequency of 3.
21+
*
22+
* Example 2:
23+
*
24+
* Input: nums = [1,4,8,13], k = 5
25+
* Output: 2
26+
* Explanation: There are multiple optimal solutions:
27+
* - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
28+
* - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
29+
* - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
30+
*
31+
* Example 3:
32+
*
33+
* Input: nums = [3,9,6], k = 2
34+
* Output: 1
35+
*
36+
* Constraints:
37+
*
38+
* 1 <= nums.length <= 10^5
39+
* 1 <= nums[i] <= 10^5
40+
* 1 <= k <= 10^5
41+
******************************************************************************************************/
42+
43+
class Solution {
44+
public:
45+
int maxFrequency(vector<int>& nums, int k) {
46+
sort(nums.begin(), nums.end());
47+
int m = 1;
48+
int start = 0;
49+
int i = 1;
50+
for(; i<nums.size(); i++){
51+
long delta = nums[i] - nums[i-1];
52+
k -= delta * (i - start);;
53+
if (k < 0 ) {
54+
// remove the first one
55+
k += (nums[i] - nums[start]) ;
56+
start++;
57+
}
58+
m = max(m, i - start +1);
59+
60+
}
61+
return m;
62+
}
63+
};

0 commit comments

Comments
 (0)