-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_3005.cpp
More file actions
60 lines (51 loc) · 1.73 KB
/
Leetcode_3005.cpp
File metadata and controls
60 lines (51 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
// 3005. Count Elements With Maximum Frequency
// Link: https://leetcode.com/problems/count-elements-with-maximum-frequency/
/*
Problem:
You are given an array nums consisting of positive integers.
Return the total frequencies of elements in nums such that those elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that element in the array.
Approach:
- Use a hashmap to count the frequency of each element in the array.
- Find the maximum frequency among all elements.
- Count the total occurrences of all elements that have this maximum frequency.
Key Insights:
- Hashmap efficiently tracks element frequencies in a single pass.
- Two-phase approach: first determine max frequency, then sum occurrences.
- The answer is the product of (max frequency) × (number of elements with max frequency).
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(k), where k is the number of unique elements in the array.
*/
class Solution
{
public:
int maxFrequencyElements(vector<int> &nums)
{
// Step 1: Count the frequency of each element using a hashmap
unordered_map<int, int> frequencyMap;
for (int num : nums)
{
frequencyMap[num]++;
}
// Step 2: Find the maximum frequency
int maxFrequency = 0;
for (auto &pair : frequencyMap)
{
maxFrequency = max(maxFrequency, pair.second);
}
// Step 3: Count total occurrences of elements with maximum frequency
int totalCount = 0;
for (auto &pair : frequencyMap)
{
if (pair.second == maxFrequency)
{
totalCount += pair.second;
}
}
return totalCount;
}
};