-
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.
- Loading branch information
1 parent
76b5c95
commit d093a5c
Showing
6 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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
12 changes: 12 additions & 0 deletions
12
elixir/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.ex
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,12 @@ | ||
defmodule LeetCode.Solutions.TopKFrequentElements do | ||
@moduledoc false | ||
|
||
@spec call(nums :: [integer], k :: integer) :: [integer] | ||
def call(nums, k) do | ||
nums | ||
|> Enum.frequencies() | ||
|> Enum.sort(fn {_num1, x}, {_num2, y} -> x >= y end) | ||
|> Enum.take(k) | ||
|> Enum.map(fn {num, _frequency} -> num end) | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
elixir/test/solutions/0347_top_k_frequent_elements/top_k_frequent_elements_test.exs
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,13 @@ | ||
defmodule LeetCode.Solutions.TopKFrequentElementsTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias LeetCode.Solutions.TopKFrequentElements | ||
|
||
test "Case 1 works" do | ||
assert TopKFrequentElements.call([1, 1, 1, 2, 2, 3], 2) == [1, 2] | ||
end | ||
|
||
test "Case 2 works" do | ||
assert TopKFrequentElements.call([1], 1) == [1] | ||
end | ||
end |
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,46 @@ | ||
# Top K Frequent Elements | ||
|
||
**Link to Problem**: https://leetcode.com/problems/top-k-frequent-elements | ||
|
||
## Solutions | ||
|
||
- [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) | ||
|
||
## Description | ||
|
||
Given an integer array `nums` and an integer `k`, return the `k` most frequent elements. You may return the answer in **any order**. | ||
|
||
## Examples | ||
|
||
### Example 1 | ||
|
||
``` | ||
Input: nums = [1,1,1,2,2,3], k = 2 | ||
Output: [1,2] | ||
``` | ||
|
||
### Example 2 | ||
|
||
``` | ||
Input: nums = [1], k = 1 | ||
Output: [1] | ||
``` | ||
|
||
## Approach | ||
|
||
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 | ||
and we sort the list/array based on the frequency in descending order. | ||
|
||
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 | ||
the tuple/subarray. | ||
|
||
## Thoughts | ||
|
||
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 | ||
just that the syntax looks slightly different. | ||
|
||
For example, Ruby has the spaceship operator `<=>` while Elixir has to use `<=` or `>=`. | ||
|
||
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 | ||
how to iterate through a hash map. |
9 changes: 9 additions & 0 deletions
9
ruby/lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements.rb
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
def top_k_frequent(nums, k_elements) | ||
nums.tally | ||
.to_a | ||
.sort { |a, b| b[1] <=> a[1] } | ||
.first(k_elements) | ||
.map(&:first) | ||
end |
14 changes: 14 additions & 0 deletions
14
ruby/test/solutions/0347_top_k_frequent_elements/top_k_frequent_elements_test.rb
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require_relative '../../../lib/solutions/0347_top_k_frequent_elements/top_k_frequent_elements' | ||
|
||
class TopKFrequentElementsTest < Minitest::Test | ||
def test_case_1_works | ||
assert_equal [1, 2], top_k_frequent([1, 1, 1, 2, 2, 3], 2) | ||
end | ||
|
||
def test_case_2_works | ||
assert_equal [1], top_k_frequent([1], 1) | ||
end | ||
end |