Skip to content

Commit 86d256a

Browse files
authored
Solve 347 - Top K Frequent Elements (#10)
1 parent 76b5c95 commit 86d256a

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This is a monorepo containing my solutions to LeetCode problems written in vario
1212
- 49 - Group Anagrams - [Notes](notes/0049_group_anagrams.md) | [Elixir](elixir/lib/solutions/0049_group_anagrams/group_anagrams.ex) | [Ruby](ruby/lib/solutions/0049_group_anagrams/group_anagrams.rb)
1313
- 217 - Contains Duplicate - [Notes](notes/0217_contains_duplicate.md) | [Elixir](elixir/lib/solutions/0217_contains_duplicate/contains_duplicate.ex) | [Ruby](ruby/lib/solutions/0217_contains_duplicate/contains_duplicate.rb)
1414
- 242 - Valid Anagram - [Notes](notes/0242_valid_anagram.md) | [Elixir](elixir/lib/solutions/0242_valid_anagram/valid_anagram.ex) | [Ruby](ruby/lib/solutions/0242_valid_anagram/valid_anagram.rb)
15+
- 347 - Top K Frequent Elements - [Notes](notes/0347_top_k_frequent_elements.md) | [Elixir](elixir/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.ex) | [Ruby](ruby/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.rb)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule LeetCode.Solutions.TopKFrequentElements do
2+
@moduledoc false
3+
4+
@spec call(nums :: [integer], k :: integer) :: [integer]
5+
def call(nums, k) do
6+
nums
7+
|> Enum.frequencies()
8+
|> Enum.sort(fn {_num1, x}, {_num2, y} -> x >= y end)
9+
|> Enum.take(k)
10+
|> Enum.map(fn {num, _frequency} -> num end)
11+
end
12+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule LeetCode.Solutions.TopKFrequentElementsTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LeetCode.Solutions.TopKFrequentElements
5+
6+
test "Case 1 works" do
7+
assert TopKFrequentElements.call([1, 1, 1, 2, 2, 3], 2) == [1, 2]
8+
end
9+
10+
test "Case 2 works" do
11+
assert TopKFrequentElements.call([1], 1) == [1]
12+
end
13+
end

notes/0347_top_k_frequent_elements.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Top K Frequent Elements
2+
3+
**Link to Problem**: https://leetcode.com/problems/top-k-frequent-elements
4+
5+
## Solutions
6+
7+
- [Elixir](../elixir/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.ex)
8+
- [Ruby](../ruby/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.rb)
9+
10+
## Description
11+
12+
Given an integer array `nums` and an integer `k`, return the `k` most frequent elements. You may return the answer in **any order**.
13+
14+
## Examples
15+
16+
### Example 1
17+
18+
```
19+
Input: nums = [1,1,1,2,2,3], k = 2
20+
Output: [1,2]
21+
```
22+
23+
### Example 2
24+
25+
```
26+
Input: nums = [1], k = 1
27+
Output: [1]
28+
```
29+
30+
## Approach
31+
32+
We produce a hash map of the frequencies of each element in the list/array. We then convert this hash map back into a list/array
33+
and we sort the list/array based on the frequency in descending order.
34+
35+
Once the list/array has been sorted, we take the first `k` elements and we produce a new list/array containing the first element of
36+
the tuple/subarray.
37+
38+
## Thoughts
39+
40+
One thing I learned from this problem is how the sorting works for both Elixir and Ruby. Apparently, they work the same way. It's
41+
just that the syntax looks slightly different.
42+
43+
For example, Ruby has the spaceship operator `<=>` while Elixir has to use `<=` or `>=`.
44+
45+
Also, for Elixir, we can skip the step where we have to convert the hash map into a list because the `Enum` module already knows
46+
how to iterate through a hash map.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
def top_k_frequent(nums, k_elements)
4+
nums.tally
5+
.to_a
6+
.sort { |a, b| b[1] <=> a[1] }
7+
.first(k_elements)
8+
.map(&:first)
9+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require_relative '../../../lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements'
5+
6+
class TopKFrequentElementsTest < Minitest::Test
7+
def test_case_1_works
8+
assert_equal [1, 2], top_k_frequent([1, 1, 1, 2, 2, 3], 2)
9+
end
10+
11+
def test_case_2_works
12+
assert_equal [1], top_k_frequent([1], 1)
13+
end
14+
end

0 commit comments

Comments
 (0)