From 9b3c8c3fef3f70a9bcf104ba7497bb54b45dec96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=BA=D0=BE=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Wed, 7 Feb 2024 23:27:37 +0300 Subject: [PATCH] 347. Top K Frequent Elements --- .idea/.idea.Leetcode/.idea/workspace.xml | 30 ++++++++++++++++--- .../347. Top K Frequent Elements.csproj | 11 +++++++ 347. Top K Frequent Elements/Program.cs | 4 +++ 347. Top K Frequent Elements/Solution.cs | 13 ++++++++ Leetcode.sln | 6 ++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 347. Top K Frequent Elements/347. Top K Frequent Elements.csproj create mode 100644 347. Top K Frequent Elements/Program.cs create mode 100644 347. Top K Frequent Elements/Solution.cs diff --git a/.idea/.idea.Leetcode/.idea/workspace.xml b/.idea/.idea.Leetcode/.idea/workspace.xml index 6fcef90..09d9488 100644 --- a/.idea/.idea.Leetcode/.idea/workspace.xml +++ b/.idea/.idea.Leetcode/.idea/workspace.xml @@ -42,6 +42,7 @@ 299. Bulls and Cows/299. Bulls and Cows.csproj 3. Longest Substring Without Repeating Characters/3. Longest Substring Without Repeating Characters.csproj 344. Reverse String/344. Reverse String.csproj + 347. Top K Frequent Elements/347. Top K Frequent Elements.csproj 35. Search Insert Position/35. Search Insert Position.csproj 350. Intersection of Two Arrays II/350. Intersection of Two Arrays II.csproj 36. Valid Sudoku/36. Valid Sudoku.csproj @@ -89,9 +90,11 @@ - + + + - + - + + + diff --git a/347. Top K Frequent Elements/347. Top K Frequent Elements.csproj b/347. Top K Frequent Elements/347. Top K Frequent Elements.csproj new file mode 100644 index 0000000..cf4697e --- /dev/null +++ b/347. Top K Frequent Elements/347. Top K Frequent Elements.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _347._Top_K_Frequent_Elements + enable + enable + + + diff --git a/347. Top K Frequent Elements/Program.cs b/347. Top K Frequent Elements/Program.cs new file mode 100644 index 0000000..bc7878c --- /dev/null +++ b/347. Top K Frequent Elements/Program.cs @@ -0,0 +1,4 @@ +using _347._Top_K_Frequent_Elements; + +var sol = new Solution(); +Console.WriteLine(string.Join(", ", sol.TopKFrequent([1,1,1,2,2,3], 2))); \ No newline at end of file diff --git a/347. Top K Frequent Elements/Solution.cs b/347. Top K Frequent Elements/Solution.cs new file mode 100644 index 0000000..1d95825 --- /dev/null +++ b/347. Top K Frequent Elements/Solution.cs @@ -0,0 +1,13 @@ +namespace _347._Top_K_Frequent_Elements; + +public class Solution +{ + public int[] TopKFrequent(int[] nums, int k) + { + var dictionary = new Dictionary(); + foreach (var num in nums) + if (!dictionary.TryAdd(num, 1)) + dictionary[num]++; + return dictionary.OrderByDescending(kp=>kp.Value).Take(k).Select(kp=>kp.Key).ToArray(); + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index ce77058..01fd750 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -173,6 +173,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "9. Palindrome Number", "9. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "451. Sort Characters By Frequency", "451. Sort Characters By Frequency\451. Sort Characters By Frequency.csproj", "{CA2D97FB-8F9D-40C0-BCE6-CBF0528AE296}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "347. Top K Frequent Elements", "347. Top K Frequent Elements\347. Top K Frequent Elements.csproj", "{F4DE54A1-6881-445A-A6B6-2206293D2F2E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -519,6 +521,10 @@ Global {CA2D97FB-8F9D-40C0-BCE6-CBF0528AE296}.Debug|Any CPU.Build.0 = Debug|Any CPU {CA2D97FB-8F9D-40C0-BCE6-CBF0528AE296}.Release|Any CPU.ActiveCfg = Release|Any CPU {CA2D97FB-8F9D-40C0-BCE6-CBF0528AE296}.Release|Any CPU.Build.0 = Release|Any CPU + {F4DE54A1-6881-445A-A6B6-2206293D2F2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4DE54A1-6881-445A-A6B6-2206293D2F2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4DE54A1-6881-445A-A6B6-2206293D2F2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4DE54A1-6881-445A-A6B6-2206293D2F2E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE