Skip to content

Commit ac97fdb

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 3005
1 parent e573bd5 commit ac97fdb

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
299299
- [2785 Sort Vowels in a String](https://leetcode.com/problems/sort-vowels-in-a-string/description/)
300300
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
301+
- [3005 Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/description/)
301302
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
302303
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
303304
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import collections
2+
from typing import List
3+
4+
5+
class Solution:
6+
"""Base class for all LeetCode Problems."""
7+
8+
def maxFrequencyElements(self, nums: List[int]) -> int:
9+
"""
10+
You are given an array nums consisting of positive integers.
11+
12+
Return the total frequencies of elements in nums such that those elements all
13+
have the maximum frequency.
14+
15+
The frequency of an element is the number of occurrences of that element in the
16+
array.
17+
"""
18+
# (num -> frequency)
19+
num_to_frequency = collections.defaultdict(int)
20+
21+
# (frequency -> count)
22+
frequency_to_count = collections.defaultdict(int)
23+
24+
# Count the frequency and the number of frequencies for the numbers
25+
for n in nums:
26+
if num_to_frequency[n] > 0:
27+
frequency_to_count[num_to_frequency[n]] -= 1
28+
num_to_frequency[n] += 1
29+
frequency_to_count[num_to_frequency[n]] += 1
30+
31+
# Get the maximum frequency and the count of that frequency
32+
max_frequency = max(num_to_frequency.values())
33+
max_count = frequency_to_count[max_frequency]
34+
35+
return max_frequency * max_count
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._3005_count_elements_with_maximum_frequency import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 2, 2, 3, 1, 4], 4),
12+
([1, 2, 3, 4, 5], 5),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
max_frequency = Solution().maxFrequencyElements(nums)
18+
assert max_frequency == expected

0 commit comments

Comments
 (0)