Skip to content

Commit 7281ca0

Browse files
committed
Added tasks 300-394
1 parent 30ac022 commit 7281ca0

File tree

6 files changed

+390
-0
lines changed

6 files changed

+390
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115

116116
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
117117
|-|-|-|-|-|-
118+
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
118119

119120
#### Day 17 Dynamic Programming
120121

@@ -126,6 +127,7 @@
126127
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
127128
|-|-|-|-|-|-
128129
| 0072 |[Edit Distance](src/main/erlang/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 251 | 100.00
130+
| 0322 |[Coin Change](src/main/erlang/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 797 | 100.00
129131

130132
#### Day 19 Bit Manipulation
131133

@@ -225,6 +227,7 @@
225227

226228
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
227229
|-|-|-|-|-|-
230+
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
228231

229232
#### Day 4
230233

@@ -416,6 +419,7 @@
416419

417420
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
418421
|-|-|-|-|-|-
422+
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
419423

420424
#### Day 19
421425

@@ -427,6 +431,7 @@
427431

428432
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
429433
|-|-|-|-|-|-
434+
| 0322 |[Coin Change](src/main/erlang/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 797 | 100.00
430435

431436
#### Day 21
432437

@@ -807,6 +812,7 @@
807812

808813
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
809814
|-|-|-|-|-|-
815+
| 0394 |[Decode String](src/main/erlang/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
810816

811817
#### Day 15 Heap
812818

@@ -882,6 +888,7 @@
882888
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
883889
|-|-|-|-|-|-
884890
| 0198 |[House Robber](src/main/erlang/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
891+
| 0322 |[Coin Change](src/main/erlang/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 797 | 100.00
885892

886893
#### Day 13 Dynamic Programming
887894

@@ -948,6 +955,7 @@
948955
| 0003 |[Longest Substring Without Repeating Characters](src/main/erlang/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 11 | 100.00
949956
| 0020 |[Valid Parentheses](src/main/erlang/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
950957
| 0005 |[Longest Palindromic Substring](src/main/erlang/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 179 | 100.00
958+
| 0394 |[Decode String](src/main/erlang/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
951959
| 0049 |[Group Anagrams](src/main/erlang/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 13 | 90.00
952960

953961
#### Udemy Binary Search
@@ -1035,6 +1043,7 @@
10351043
| 0152 |[Maximum Product Subarray](src/main/erlang/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
10361044
| 0198 |[House Robber](src/main/erlang/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10371045
| 0070 |[Climbing Stairs](src/main/erlang/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1046+
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
10381047
| 0072 |[Edit Distance](src/main/erlang/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 251 | 100.00
10391048
| 0010 |[Regular Expression Matching](src/main/erlang/g0001_0100/s0010_regular_expression_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 2019 | 100.00
10401049

@@ -1052,6 +1061,7 @@
10521061

10531062
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10541063
|-|-|-|-|-|-
1064+
| 0338 |[Counting Bits](src/main/erlang/g0301_0400/s0338_counting_bits)| Easy | Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 38 | 100.00
10551065

10561066
#### Udemy Design
10571067

@@ -1260,6 +1270,7 @@
12601270
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12611271
|-|-|-|-|-|-
12621272
| 0215 |[Kth Largest Element in an Array](src/main/erlang/g0201_0300/s0215_kth_largest_element_in_an_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 803 | 100.00
1273+
| 0347 |[Top K Frequent Elements](src/main/erlang/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n\*log(n))_Space_O(k) | 12 | 100.00
12631274

12641275
#### Day 21 Heap Priority Queue
12651276

@@ -1355,6 +1366,11 @@
13551366
| 0739 |[Daily Temperatures](src/main/erlang/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 94 | 100.00
13561367
| 0647 |[Palindromic Substrings](src/main/erlang/g0601_0700/s0647_palindromic_substrings)| Medium | String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 17 | 100.00
13571368
| 0560 |[Subarray Sum Equals K](src/main/erlang/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 47 | 100.00
1369+
| 0394 |[Decode String](src/main/erlang/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1370+
| 0347 |[Top K Frequent Elements](src/main/erlang/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 12 | 100.00
1371+
| 0338 |[Counting Bits](src/main/erlang/g0301_0400/s0338_counting_bits)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 38 | 100.00
1372+
| 0322 |[Coin Change](src/main/erlang/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(amount) | 797 | 100.00
1373+
| 0300 |[Longest Increasing Subsequence](src/main/erlang/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 954 | 100.00
13581374
| 0287 |[Find the Duplicate Number](src/main/erlang/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 234 | 100.00
13591375
| 0239 |[Sliding Window Maximum](src/main/erlang/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 422 | 100.00
13601376
| 0238 |[Product of Array Except Self](src/main/erlang/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 22 | 100.00
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 300\. Longest Increasing Subsequence
5+
6+
Medium
7+
8+
Given an integer array `nums`, return the length of the longest strictly increasing subsequence.
9+
10+
A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, `[3,6,2,7]` is a subsequence of the array `[0,3,1,6,2,2,7]`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [10,9,2,5,3,7,101,18]
15+
16+
**Output:** 4
17+
18+
**Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [0,1,0,3,2,3]
23+
24+
**Output:** 4
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [7,7,7,7,7,7,7]
29+
30+
**Output:** 1
31+
32+
**Constraints:**
33+
34+
* `1 <= nums.length <= 2500`
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
37+
**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity?
38+
39+
## Solution
40+
41+
```erlang
42+
-spec length_of_lis(Nums :: [integer()]) -> integer().
43+
length_of_lis(Nums) ->
44+
Pred = fun(V, Index) -> {{Index, V}, Index + 1} end,
45+
{IVals, _} = lists:mapfoldl(Pred, 0, Nums),
46+
[H|T] = IVals,
47+
Tid = ets:new(lis, []),
48+
length_of_lis([], T, H, Tid, 0).
49+
50+
length_of_lis(Before, [], {_, Val}, Tid, Max) ->
51+
max(lis(Before, Val, Tid) + 1, Max);
52+
length_of_lis(Before, [Next|After], Current={Index, Val}, Tid, Max) ->
53+
Lis = lis(Before, Val, Tid) + 1,
54+
ets:insert(Tid, {Index, Lis}),
55+
length_of_lis([Current|Before], After, Next, Tid, max(Max, Lis)).
56+
57+
lis([], _, _) -> 0;
58+
lis(L, N, Tid) ->
59+
Opts = lists:filter(fun({I, V}) -> V < N end, L),
60+
Pred = fun({I, _}) -> ets:lookup_element(Tid, I, 2) end,
61+
case Opts of
62+
[] -> 0;
63+
_ ->
64+
lists:max(lists:map(Pred, Opts))
65+
end.
66+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 322\. Coin Change
5+
6+
Medium
7+
8+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
9+
10+
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
11+
12+
You may assume that you have an infinite number of each kind of coin.
13+
14+
**Example 1:**
15+
16+
**Input:** coins = [1,2,5], amount = 11
17+
18+
**Output:** 3
19+
20+
**Explanation:** 11 = 5 + 5 + 1
21+
22+
**Example 2:**
23+
24+
**Input:** coins = [2], amount = 3
25+
26+
**Output:** -1
27+
28+
**Example 3:**
29+
30+
**Input:** coins = [1], amount = 0
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* `1 <= coins.length <= 12`
37+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
38+
* <code>0 <= amount <= 10<sup>4</sup></code>
39+
40+
## Solution
41+
42+
```erlang
43+
-spec coin_change(Coins :: [integer()], Amount :: integer()) -> integer().
44+
coin_change(Coins, Amount) ->
45+
% Sort coins in ascending order
46+
SortedCoins = lists:sort(Coins),
47+
48+
% Initialize DP map with base case
49+
InitDP = #{0 => 0},
50+
51+
% Build DP table for amounts from 1 to target
52+
case Amount of
53+
0 -> 0;
54+
_ ->
55+
FinalDP = build_dp_table(SortedCoins, lists:seq(1, Amount), InitDP),
56+
% Get result or return -1 if not possible
57+
maps:get(Amount, FinalDP, -1)
58+
end.
59+
60+
% Build DP table for all amounts
61+
-spec build_dp_table([integer()], [integer()], map()) -> map().
62+
build_dp_table(_Coins, [], DP) -> DP;
63+
build_dp_table(Coins, [Amount|Rest], DP) ->
64+
NewDP = process_coins_for_amount(Coins, Amount, DP),
65+
build_dp_table(Coins, Rest, NewDP).
66+
67+
% Process all coins for current amount
68+
-spec process_coins_for_amount([integer()], integer(), map()) -> map().
69+
process_coins_for_amount([], _Amount, DP) -> DP;
70+
process_coins_for_amount([Coin|Rest], Amount, DP) ->
71+
case Coin > Amount of
72+
true ->
73+
% Stop if coin is larger than current amount
74+
DP;
75+
false ->
76+
% Try using current coin
77+
case maps:get(Amount - Coin, DP, undefined) of
78+
undefined ->
79+
% Can't use this coin, try next
80+
process_coins_for_amount(Rest, Amount, DP);
81+
PrevCount ->
82+
% Can use this coin, update minimum coins needed
83+
NewCount = PrevCount + 1,
84+
NewDP = maps:update_with(Amount,
85+
fun(OldVal) -> min(OldVal, NewCount) end,
86+
NewCount,
87+
DP),
88+
process_coins_for_amount(Rest, Amount, NewDP)
89+
end
90+
end.
91+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 338\. Counting Bits
5+
6+
Easy
7+
8+
Given an integer `n`, return _an array_ `ans` _of length_ `n + 1` _such that for each_ `i` (`0 <= i <= n`)_,_ `ans[i]` _is the **number of**_ `1`_**'s** in the binary representation of_ `i`.
9+
10+
**Example 1:**
11+
12+
**Input:** n = 2
13+
14+
**Output:** [0,1,1]
15+
16+
**Explanation:**
17+
18+
0 --> 0
19+
1 --> 1
20+
2 --> 10
21+
22+
**Example 2:**
23+
24+
**Input:** n = 5
25+
26+
**Output:** [0,1,1,2,1,2]
27+
28+
**Explanation:**
29+
30+
0 --> 0
31+
1 --> 1
32+
2 --> 10
33+
3 --> 11
34+
4 --> 100
35+
5 --> 101
36+
37+
**Constraints:**
38+
39+
* <code>0 <= n <= 10<sup>5</sup></code>
40+
41+
**Follow up:**
42+
43+
* It is very easy to come up with a solution with a runtime of `O(n log n)`. Can you do it in linear time `O(n)` and possibly in a single pass?
44+
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)?
45+
46+
## Solution
47+
48+
```erlang
49+
-spec count_bits(N :: integer()) -> [integer()].
50+
count_bits(N) ->
51+
InitMap = maps:from_list([{X, 0} || X <- lists:seq(0, N div 2)]),
52+
lists:reverse(do_count_bits(1, N, InitMap, [0])).
53+
54+
do_count_bits(I, N, _Map, Acc) when I > N ->
55+
Acc;
56+
do_count_bits(I, N, Map, Acc) ->
57+
IBits = maps:get(I div 2, Map) + (I rem 2),
58+
NewMap = case I > (N div 2) of
59+
true -> Map;
60+
false -> maps:put(I, IBits, Map)
61+
end,
62+
do_count_bits(I + 1, N, NewMap, [IBits | Acc]).
63+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Erlang/LeetCode-in-Erlang?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Erlang/LeetCode-in-Erlang/fork)
3+
4+
## 347\. Top K Frequent Elements
5+
6+
Medium
7+
8+
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,1,1,2,2,3], k = 2
13+
14+
**Output:** [1,2]
15+
16+
**Example 2:**
17+
18+
**Input:** nums = [1], k = 1
19+
20+
**Output:** [1]
21+
22+
**Constraints:**
23+
24+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
25+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
26+
* `k` is in the range `[1, the number of unique elements in the array]`.
27+
* It is **guaranteed** that the answer is **unique**.
28+
29+
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size.
30+
31+
## Solution
32+
33+
```erlang
34+
-spec top_k_frequent(Nums :: [integer()], K :: integer()) -> [integer()].
35+
top_k_frequent(Nums, K) ->
36+
% Count frequencies using a map
37+
FreqMap = lists:foldl(
38+
fun(N, Map) ->
39+
maps:update_with(N, fun(V) -> V + 1 end, 1, Map)
40+
end,
41+
#{},
42+
Nums
43+
),
44+
45+
% Convert map to list, sort by frequency (descending), take K elements
46+
FreqList = maps:to_list(FreqMap),
47+
SortedList = lists:sort(
48+
fun({_, Count1}, {_, Count2}) -> Count1 > Count2 end,
49+
FreqList
50+
),
51+
TopK = lists:sublist(SortedList, K),
52+
53+
% Extract only the numbers from the result
54+
[Num || {Num, _Count} <- TopK].
55+
```

0 commit comments

Comments
 (0)