347. Top K Frequent Elements
This commit is contained in:
30
.idea/.idea.Leetcode/.idea/workspace.xml
generated
30
.idea/.idea.Leetcode/.idea/workspace.xml
generated
@@ -42,6 +42,7 @@
|
|||||||
<projectFile>299. Bulls and Cows/299. Bulls and Cows.csproj</projectFile>
|
<projectFile>299. Bulls and Cows/299. Bulls and Cows.csproj</projectFile>
|
||||||
<projectFile>3. Longest Substring Without Repeating Characters/3. Longest Substring Without Repeating Characters.csproj</projectFile>
|
<projectFile>3. Longest Substring Without Repeating Characters/3. Longest Substring Without Repeating Characters.csproj</projectFile>
|
||||||
<projectFile>344. Reverse String/344. Reverse String.csproj</projectFile>
|
<projectFile>344. Reverse String/344. Reverse String.csproj</projectFile>
|
||||||
|
<projectFile>347. Top K Frequent Elements/347. Top K Frequent Elements.csproj</projectFile>
|
||||||
<projectFile>35. Search Insert Position/35. Search Insert Position.csproj</projectFile>
|
<projectFile>35. Search Insert Position/35. Search Insert Position.csproj</projectFile>
|
||||||
<projectFile>350. Intersection of Two Arrays II/350. Intersection of Two Arrays II.csproj</projectFile>
|
<projectFile>350. Intersection of Two Arrays II/350. Intersection of Two Arrays II.csproj</projectFile>
|
||||||
<projectFile>36. Valid Sudoku/36. Valid Sudoku.csproj</projectFile>
|
<projectFile>36. Valid Sudoku/36. Valid Sudoku.csproj</projectFile>
|
||||||
@@ -89,9 +90,11 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="11f2de1c-1ee5-4aa4-91e6-11975ebd421f" name="Changes" comment="">
|
<list default="true" id="11f2de1c-1ee5-4aa4-91e6-11975ebd421f" name="Changes" comment="">
|
||||||
<change afterPath="$PROJECT_DIR$/451. Sort Characters By Frequency/Fast/FastSolution.cs" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/347. Top K Frequent Elements/347. Top K Frequent Elements.csproj" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/347. Top K Frequent Elements/Program.cs" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/347. Top K Frequent Elements/Solution.cs" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Leetcode/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/451. Sort Characters By Frequency/Program.cs" beforeDir="false" afterPath="$PROJECT_DIR$/451. Sort Characters By Frequency/Program.cs" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Leetcode.sln" beforeDir="false" afterPath="$PROJECT_DIR$/Leetcode.sln" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@@ -127,6 +130,7 @@
|
|||||||
"keyToString": {
|
"keyToString": {
|
||||||
".NET Project.14. Longest Common Prefix.executor": "Run",
|
".NET Project.14. Longest Common Prefix.executor": "Run",
|
||||||
".NET Project.150. Evaluate Reverse Polish Notation.executor": "Run",
|
".NET Project.150. Evaluate Reverse Polish Notation.executor": "Run",
|
||||||
|
".NET Project.347. Top K Frequent Elements.executor": "Run",
|
||||||
".NET Project.451. Sort Characters By Frequency.executor": "Run",
|
".NET Project.451. Sort Characters By Frequency.executor": "Run",
|
||||||
".NET Project.9. Palindrome Number.executor": "Run",
|
".NET Project.9. Palindrome Number.executor": "Run",
|
||||||
"ASKED_ADD_EXTERNAL_FILES": "true",
|
"ASKED_ADD_EXTERNAL_FILES": "true",
|
||||||
@@ -151,7 +155,7 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
}]]></component>
|
}]]></component>
|
||||||
<component name="RunManager" selected=".NET Project.451. Sort Characters By Frequency">
|
<component name="RunManager" selected=".NET Project.347. Top K Frequent Elements">
|
||||||
<configuration name="101. Symmetric Tree" type="DotNetProject" factoryName=".NET Project">
|
<configuration name="101. Symmetric Tree" type="DotNetProject" factoryName=".NET Project">
|
||||||
<option name="EXE_PATH" value="" />
|
<option name="EXE_PATH" value="" />
|
||||||
<option name="PROGRAM_PARAMETERS" value="" />
|
<option name="PROGRAM_PARAMETERS" value="" />
|
||||||
@@ -908,6 +912,24 @@
|
|||||||
<option name="Build" />
|
<option name="Build" />
|
||||||
</method>
|
</method>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<configuration name="347. Top K Frequent Elements" type="DotNetProject" factoryName=".NET Project">
|
||||||
|
<option name="EXE_PATH" value="" />
|
||||||
|
<option name="PROGRAM_PARAMETERS" value="" />
|
||||||
|
<option name="WORKING_DIRECTORY" value="" />
|
||||||
|
<option name="PASS_PARENT_ENVS" value="1" />
|
||||||
|
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||||
|
<option name="USE_MONO" value="0" />
|
||||||
|
<option name="RUNTIME_ARGUMENTS" value="" />
|
||||||
|
<option name="PROJECT_PATH" value="$PROJECT_DIR$/347. Top K Frequent Elements/347. Top K Frequent Elements.csproj" />
|
||||||
|
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
|
||||||
|
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
|
||||||
|
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
|
||||||
|
<option name="PROJECT_KIND" value="DotNetCore" />
|
||||||
|
<option name="PROJECT_TFM" value="" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Build" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
<configuration name="350. Intersection of Two Arrays II" type="DotNetProject" factoryName=".NET Project">
|
<configuration name="350. Intersection of Two Arrays II" type="DotNetProject" factoryName=".NET Project">
|
||||||
<option name="EXE_PATH" value="" />
|
<option name="EXE_PATH" value="" />
|
||||||
<option name="PROGRAM_PARAMETERS" value="" />
|
<option name="PROGRAM_PARAMETERS" value="" />
|
||||||
@@ -1701,7 +1723,7 @@
|
|||||||
<workItem from="1678030078340" duration="114000" />
|
<workItem from="1678030078340" duration="114000" />
|
||||||
<workItem from="1680804946367" duration="3697000" />
|
<workItem from="1680804946367" duration="3697000" />
|
||||||
<workItem from="1706644472303" duration="7903000" />
|
<workItem from="1706644472303" duration="7903000" />
|
||||||
<workItem from="1707331420927" duration="1097000" />
|
<workItem from="1707331420927" duration="3074000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>_347._Top_K_Frequent_Elements</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
4
347. Top K Frequent Elements/Program.cs
Normal file
4
347. Top K Frequent Elements/Program.cs
Normal file
@@ -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)));
|
||||||
13
347. Top K Frequent Elements/Solution.cs
Normal file
13
347. Top K Frequent Elements/Solution.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace _347._Top_K_Frequent_Elements;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int[] TopKFrequent(int[] nums, int k)
|
||||||
|
{
|
||||||
|
var dictionary = new Dictionary<int, int>();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -173,6 +173,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "9. Palindrome Number", "9.
|
|||||||
EndProject
|
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}"
|
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
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{CA2D97FB-8F9D-40C0-BCE6-CBF0528AE296}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user