Skip to content

Commit 68a0aaf

Browse files
committed
solve 2696 and 460
1 parent cb1a1df commit 68a0aaf

File tree

8 files changed

+293
-0
lines changed

8 files changed

+293
-0
lines changed

2696/MinimumString.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimumString", "MinimumString\MinimumString.csproj", "{3DD60306-FB03-4D07-B80E-EB276954CF7E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{3DD60306-FB03-4D07-B80E-EB276954CF7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{3DD60306-FB03-4D07-B80E-EB276954CF7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{3DD60306-FB03-4D07-B80E-EB276954CF7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{3DD60306-FB03-4D07-B80E-EB276954CF7E}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

2696/MinimumString/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");

2696/MinimumString/solution.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int MinLength(string s) {
3+
int length = s.Length+1;
4+
while (s.Length < length)
5+
{
6+
length = s.Length;
7+
s= RemoveAB(s);
8+
s= RemoveCD(s);
9+
}
10+
return length;
11+
12+
13+
}
14+
private string RemoveAB(string s)
15+
{
16+
return s.Replace("AB","");
17+
}
18+
19+
private string RemoveCD(string s)
20+
{
21+
return s.Replace("CD","");
22+
}
23+
}

460/LFUCache.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFUCache", "LFUCache\LFUCache.csproj", "{80223BB9-DBDC-4441-A665-26925C8AF63C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{80223BB9-DBDC-4441-A665-26925C8AF63C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{80223BB9-DBDC-4441-A665-26925C8AF63C}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{80223BB9-DBDC-4441-A665-26925C8AF63C}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{80223BB9-DBDC-4441-A665-26925C8AF63C}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

460/LFUCache/LFUCache.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

460/LFUCache/Program.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+

2+
int[][] arguments = [[2],[1,1],[2,2],[1],[3,3],[2],[3],[4,4],[1],[3],[4]];
3+
string [] commands = ["LFUCache","put","put","get","put","get","get","put","get","get","get"];
4+
LFUCache cache = null;
5+
6+
for (int i = 0; i < commands.Length; i++)
7+
{
8+
switch (commands[i])
9+
{
10+
case "LFUCache":
11+
// Initialize cache with given capacity
12+
cache = new LFUCache(arguments[i][0]);
13+
Console.WriteLine($"LFUCache initialized with capacity {arguments[i][0]}");
14+
break;
15+
16+
case "put":
17+
// Call put(k, v)
18+
cache.Put(arguments[i][0], arguments[i][1]);
19+
Console.WriteLine($"put({arguments[i][0]}, {arguments[i][1]})");
20+
break;
21+
22+
case "get":
23+
// Call get(k)
24+
int result = cache.Get(arguments[i][0]);
25+
Console.WriteLine($"get({arguments[i][0]}) -> {result}");
26+
break;
27+
28+
default:
29+
Console.WriteLine("Invalid command");
30+
break;
31+
}
32+
}

460/LFUCache/solution.cs

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
public class LFUCache {
2+
Dictionary<int ,(int,int)> keyValueFrequencyPairs;
3+
FrequencyMaintaner frequencyMaintaner;
4+
int capacity;
5+
int Count;
6+
7+
8+
public LFUCache(int capacity) {
9+
Count = 0;
10+
this.capacity = capacity;
11+
frequencyMaintaner = new FrequencyMaintaner();
12+
keyValueFrequencyPairs = new Dictionary<int ,(int,int)>();
13+
14+
}
15+
16+
public int Get(int key) {
17+
if (keyValueFrequencyPairs.ContainsKey(key)){
18+
int value;
19+
int freq;
20+
(value,freq) = keyValueFrequencyPairs[key];
21+
frequencyMaintaner.Incrament(key,freq);
22+
keyValueFrequencyPairs[key] = (value,freq+1);
23+
return value;
24+
}
25+
return -1;
26+
27+
28+
}
29+
30+
public void Put(int key, int value) {
31+
if (keyValueFrequencyPairs.ContainsKey(key)){
32+
int oldValue;
33+
int freq;
34+
(oldValue,freq) = keyValueFrequencyPairs[key];
35+
frequencyMaintaner.Incrament(key,freq);
36+
keyValueFrequencyPairs[key] = (value,freq+1);
37+
}
38+
else{
39+
if (capacity>Count){
40+
keyValueFrequencyPairs.Add(key,(value,1));
41+
frequencyMaintaner.Add(key);
42+
Count++;
43+
}
44+
else{
45+
int depKey = frequencyMaintaner.popLowestKey();
46+
keyValueFrequencyPairs.Remove(depKey);
47+
keyValueFrequencyPairs.Add(key,(value,1));
48+
frequencyMaintaner.Add(key);
49+
}
50+
}
51+
52+
}
53+
}
54+
public class FrequencyMaintaner{
55+
Dictionary<int ,SpecialSet> FrequencyKeySetPairs ;
56+
int LowestFreq;
57+
Dictionary<int, Node> nodeMap;
58+
59+
public FrequencyMaintaner(){
60+
FrequencyKeySetPairs = new Dictionary<int , SpecialSet>();
61+
LowestFreq = 0;
62+
nodeMap = new Dictionary<int , Node>();
63+
}
64+
65+
//adds the (new) key to the 1th frequency
66+
internal void Add(int key)
67+
{
68+
if (!nodeMap.ContainsKey(key))
69+
{
70+
nodeMap.Add(key, new Node(key));
71+
}
72+
Node KeyNode = nodeMap[key];
73+
LowestFreq = 1;
74+
if (FrequencyKeySetPairs.ContainsKey(1))
75+
{
76+
FrequencyKeySetPairs[1].add(KeyNode);
77+
}
78+
else{
79+
FrequencyKeySetPairs.Add(1, new SpecialSet());
80+
FrequencyKeySetPairs[1].add(KeyNode);
81+
}
82+
}
83+
84+
// given the current frequency of the key it removes it from the current special set and adds it to the next
85+
internal void Incrament(int key, int freq)
86+
{
87+
Node KeyNode = nodeMap[key];
88+
FrequencyKeySetPairs[freq].remove(KeyNode);
89+
if(FrequencyKeySetPairs[freq].count() ==0){
90+
FrequencyKeySetPairs.Remove(freq);
91+
if (LowestFreq == freq){
92+
LowestFreq ++;
93+
}
94+
}
95+
if (FrequencyKeySetPairs.ContainsKey(freq+1)){
96+
FrequencyKeySetPairs[freq+1].add(KeyNode);
97+
}
98+
else{
99+
FrequencyKeySetPairs.Add(freq+1,new SpecialSet());
100+
FrequencyKeySetPairs[freq+1].add(KeyNode);
101+
}
102+
}
103+
104+
//finds the lowest frequency specialset and pops the oldest value a temporal assumption
105+
// is made that the next thing that will be called is add on a new value to fix the lowest frequency
106+
internal int popLowestKey()
107+
{
108+
int key = FrequencyKeySetPairs[LowestFreq].popOldest();
109+
Node KeyNode = nodeMap[key];
110+
FrequencyKeySetPairs[LowestFreq].remove(KeyNode);
111+
nodeMap.Remove(key);
112+
if (FrequencyKeySetPairs[LowestFreq].count()==0){FrequencyKeySetPairs.Remove(LowestFreq); }
113+
return key;
114+
}
115+
}
116+
// spcialSet that uses a LinkedList to maintain O(1) for insertion deletion and removing the first to be inserted
117+
// deletion is done by letting refrences of nodes delete themselves
118+
public class SpecialSet{
119+
public int Count;
120+
public Node left;
121+
public Node right;
122+
public SpecialSet(){
123+
left = new Node(0);
124+
right = new Node(0);
125+
left.next = right;
126+
right.prev = left;
127+
128+
}
129+
public void add (Node val)
130+
{
131+
val.AddSelfAfter(left);
132+
Count++;
133+
}
134+
135+
public void remove (Node val)
136+
{
137+
val.removeSelf();
138+
Count--;
139+
}
140+
public int popOldest(){
141+
Node toPop = right.prev;
142+
toPop.removeSelf();
143+
return toPop.Key;
144+
145+
}
146+
public int count ()=> Count;
147+
148+
}
149+
public class Node {
150+
public int Key;
151+
public Node next;
152+
public Node prev;
153+
public Node(int Key){
154+
this.Key = Key;
155+
}
156+
157+
internal void AddSelfAfter(Node val)
158+
{
159+
Node next = val.next;
160+
val.next = this;
161+
this.prev = val;
162+
this.next = next;
163+
next.prev = this;
164+
165+
}
166+
167+
internal void removeSelf()
168+
{
169+
this.prev.next = this.next;
170+
this.next.prev = this.prev;
171+
}
172+
}

0 commit comments

Comments
 (0)