fast solution 451

This commit is contained in:
Пытков Роман
2024-02-07 22:08:26 +03:00
parent bfb8311fe7
commit c47d2185b5
3 changed files with 55 additions and 30 deletions

View File

@@ -89,11 +89,9 @@
</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/451. Sort Characters By Frequency.csproj" afterDir="false" /> <change afterPath="$PROJECT_DIR$/451. Sort Characters By Frequency/Fast/FastSolution.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/451. Sort Characters By Frequency/Program.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/451. Sort Characters By Frequency/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$/Leetcode.sln" beforeDir="false" afterPath="$PROJECT_DIR$/Leetcode.sln" 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" />
</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" />
@@ -125,33 +123,34 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent">{ <component name="PropertiesComponent"><![CDATA[{
&quot;keyToString&quot;: { "keyToString": {
&quot;.NET Project.14. Longest Common Prefix.executor&quot;: &quot;Run&quot;, ".NET Project.14. Longest Common Prefix.executor": "Run",
&quot;.NET Project.150. Evaluate Reverse Polish Notation.executor&quot;: &quot;Run&quot;, ".NET Project.150. Evaluate Reverse Polish Notation.executor": "Run",
&quot;.NET Project.9. Palindrome Number.executor&quot;: &quot;Run&quot;, ".NET Project.451. Sort Characters By Frequency.executor": "Run",
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;, ".NET Project.9. Palindrome Number.executor": "Run",
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;, "ASKED_ADD_EXTERNAL_FILES": "true",
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, "RunOnceActivity.OpenProjectViewOnStart": "true",
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;, "RunOnceActivity.ShowReadmeOnStart": "true",
&quot;git-widget-placeholder&quot;: &quot;main&quot;, "WebServerToolWindowFactoryState": "false",
&quot;last_opened_file_path&quot;: &quot;/home/nullptr/Projects/dotnet/Leetcode/15. 3Sum/15. 3Sum.csproj&quot;, "git-widget-placeholder": "main",
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;, "last_opened_file_path": "/home/nullptr/Projects/dotnet/Leetcode/15. 3Sum/15. 3Sum.csproj",
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;, "node.js.detected.package.eslint": "true",
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;, "node.js.detected.package.tslint": "true",
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.eslint": "(autodetect)",
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;, "node.js.selected.package.tslint": "(autodetect)",
&quot;settings.editor.selected.configurable&quot;: &quot;SolutionBuilderGeneralOptionsPage&quot;, "nodejs_package_manager_path": "npm",
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot; "settings.editor.selected.configurable": "SolutionBuilderGeneralOptionsPage",
"vue.rearranger.settings.migration": "true"
}, },
&quot;keyToStringList&quot;: { "keyToStringList": {
&quot;rider.external.source.directories&quot;: [ "rider.external.source.directories": [
&quot;/home/nullptr/.config/JetBrains/Rider2023.3/resharper-host/DecompilerCache&quot;, "/home/nullptr/.config/JetBrains/Rider2023.3/resharper-host/DecompilerCache",
&quot;/home/nullptr/.config/JetBrains/Rider2023.3/resharper-host/SourcesCache&quot;, "/home/nullptr/.config/JetBrains/Rider2023.3/resharper-host/SourcesCache",
&quot;/home/nullptr/.local/share/Symbols/src&quot; "/home/nullptr/.local/share/Symbols/src"
] ]
} }
}</component> }]]></component>
<component name="RunManager" selected=".NET Project.451. Sort Characters By Frequency"> <component name="RunManager" selected=".NET Project.451. Sort Characters By Frequency">
<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="" />
@@ -1702,7 +1701,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="422000" /> <workItem from="1707331420927" duration="1097000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -0,0 +1,26 @@
using System.Text;
namespace _451._Sort_Characters_By_Frequency.Fast;
public class Solution
{
private static int Comparer((char, int) el1, (char, int) el2)
{
return el2.Item2.CompareTo(el1.Item2);
}
public string FrequencySort(string s)
{
var count = new (char, int)[128];
for (var ch = (char)0; ch < count.Length; ch++)
count[ch].Item1 = ch;
foreach (var c in s)
count[c].Item2++;
var sb = new StringBuilder();
Array.Sort(count, Comparer);
for (var ch = (char)0; ch < count.Length; ch++)
while (count[ch].Item2-- > 0)
sb.Append(count[ch].Item1);
return sb.ToString();
}
}

View File

@@ -1,4 +1,4 @@
using _451._Sort_Characters_By_Frequency; using _451._Sort_Characters_By_Frequency;
var sol = new Solution(); var sol = new _451._Sort_Characters_By_Frequency.Fast.Solution();
Console.WriteLine(sol.FrequencySort("gbngjaaaaaaaafsdgnjk;fdsnjkg;fds")); Console.WriteLine(sol.FrequencySort("gbngjaaaaaaaafsdgnjk;fdsnjkg;fds"));