Skip to content

Commit a1bad9c

Browse files
committed
q
1 parent e80950c commit a1bad9c

File tree

11 files changed

+196
-1
lines changed

11 files changed

+196
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace DataStructures;
2+
3+
public class LRUCache {
4+
5+
private readonly int capacity;
6+
private readonly Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> _cacheMap;
7+
private readonly LinkedList<KeyValuePair<int,int>> _cache;
8+
9+
public LRUCache(int capacity) {
10+
this.capacity = capacity;
11+
_cacheMap = new Dictionary<int, LinkedListNode<KeyValuePair<int, int>>>(capacity);
12+
_cache = new LinkedList<KeyValuePair<int, int>>();
13+
}
14+
15+
public int Get(int key) {
16+
if (_cacheMap.TryGetValue(key, out var node))
17+
{
18+
_cache.Remove(node);
19+
_cache.AddFirst(node);
20+
return node.Value.Value;
21+
}
22+
return -1;
23+
}
24+
25+
public void Put(int key, int value) {
26+
if (_cacheMap.TryGetValue(key, out var node))
27+
{
28+
node.Value = new KeyValuePair<int, int>(key, value);
29+
_cache.Remove(node);
30+
_cache.AddFirst(node);
31+
}
32+
else
33+
{
34+
if (_cacheMap.Count >= capacity)
35+
{
36+
var lastNode = _cache.Last;
37+
_cacheMap.Remove(lastNode.Value.Key);
38+
_cache.RemoveLast();
39+
}
40+
41+
var newNode = new LinkedListNode<KeyValuePair<int, int>>(new KeyValuePair<int, int>(key, value));
42+
_cacheMap.Add(key, newNode);
43+
_cache.AddFirst(newNode);
44+
}
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace DataStructures;
2+
3+
public class TreeNode
4+
{
5+
public int Val { get; set; }
6+
public TreeNode? Left { get; set; }
7+
public TreeNode? Right { get; set; }
8+
9+
public TreeNode(int x)
10+
{
11+
Val = x;
12+
}
13+
14+
public void Build(List<int?> nums, int i, TreeNode cur)
15+
{
16+
var parent = i;
17+
var left = parent * 2 + 1;
18+
var right = parent * 2 + 2;
19+
cur = new TreeNode(nums[i].Value);
20+
if (left < nums.Count && nums[left] != null)
21+
{
22+
Build(nums, left, cur.Left);
23+
}
24+
if (right < nums.Count && nums[right] != null)
25+
{
26+
Build(nums, left, cur.Right);
27+
}
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
namespace DynamicProgramming;
4+
5+
public class CountNumberOfTeams
6+
{
7+
public int NumTeams(int[] rating)
8+
{
9+
// This method counts the number of teams that can be formed
10+
int n = rating.Length;
11+
int count = 0;
12+
13+
// Iterate through each soldier as a potential team leader
14+
// and count how many soldiers are less and greater than the leader
15+
for (int i = 0; i < n; i++)
16+
{ int leftLess = 0,
17+
leftGreater = 0;
18+
int rightLess = 0,
19+
rightGreater = 0;
20+
21+
for (int j = 0; j < n; j++)
22+
{
23+
if (rating[j] < rating[i])
24+
{
25+
if (j < i)
26+
leftLess++;
27+
else
28+
rightLess++;
29+
}
30+
else if (rating[j] > rating[i])
31+
{
32+
if (j < i)
33+
leftGreater++;
34+
else
35+
rightGreater++;
36+
}
37+
}
38+
39+
count += leftLess * rightGreater + leftGreater * rightLess;
40+
}
41+
42+
return count;
43+
}
44+
}

Graph/BFS/BFS.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\..\DataStructures\DataStructures\DataStructures.csproj" />
11+
</ItemGroup>
12+
913
</Project>

Graph/BFS/DijkstraImp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BFS;
2+
3+
public class DijkstraImp
4+
{
5+
public void Dijkstra()
6+
{
7+
}
8+
}

Graph/BFS/NodesDistanceK.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
using DataStructures;
12
namespace BFS;
23

34
public class NodesDistanceK
45
{
5-
6+
public IList<int> DistanceK(TreeNode root, TreeNode target, int k)
7+
{
8+
return [];
9+
}
610
}

Graph/Graphs/Class1.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Graphs;
2+
3+
public class CheapestFlightsWithinKStops
4+
{
5+
public int FindCheapestPrice(int n, int[][] flights, int src, int dst, int k)
6+
{
7+
8+
9+
return 0;
10+
}
11+
}

Graph/Graphs/Graphs.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

LeetCodePatterns.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Graph", "Graph", "{E7AADF05
5353
EndProject
5454
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BFS", "Graph\BFS\BFS.csproj", "{6FD5F7B1-4A40-408E-8BF3-DA89C0873045}"
5555
EndProject
56+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataStructures", "DataStructures", "{2590E36B-A3E2-41EC-8A85-C79D3B72DEC5}"
57+
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures", "DataStructures\DataStructures\DataStructures.csproj", "{BF171E67-B282-4BD8-9511-D4218BDA8BD4}"
59+
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Graphs", "Graph\Graphs\Graphs.csproj", "{AC86D8BA-7B66-40D6-9351-870BBE35457F}"
61+
EndProject
5662
Global
5763
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5864
Debug|Any CPU = Debug|Any CPU
@@ -117,6 +123,14 @@ Global
117123
{6FD5F7B1-4A40-408E-8BF3-DA89C0873045}.Debug|Any CPU.Build.0 = Debug|Any CPU
118124
{6FD5F7B1-4A40-408E-8BF3-DA89C0873045}.Release|Any CPU.ActiveCfg = Release|Any CPU
119125
{6FD5F7B1-4A40-408E-8BF3-DA89C0873045}.Release|Any CPU.Build.0 = Release|Any CPU
126+
{BF171E67-B282-4BD8-9511-D4218BDA8BD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127+
{BF171E67-B282-4BD8-9511-D4218BDA8BD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
128+
{BF171E67-B282-4BD8-9511-D4218BDA8BD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
129+
{BF171E67-B282-4BD8-9511-D4218BDA8BD4}.Release|Any CPU.Build.0 = Release|Any CPU
130+
{AC86D8BA-7B66-40D6-9351-870BBE35457F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131+
{AC86D8BA-7B66-40D6-9351-870BBE35457F}.Debug|Any CPU.Build.0 = Debug|Any CPU
132+
{AC86D8BA-7B66-40D6-9351-870BBE35457F}.Release|Any CPU.ActiveCfg = Release|Any CPU
133+
{AC86D8BA-7B66-40D6-9351-870BBE35457F}.Release|Any CPU.Build.0 = Release|Any CPU
120134
EndGlobalSection
121135
GlobalSection(SolutionProperties) = preSolution
122136
HideSolutionNode = FALSE
@@ -137,6 +151,8 @@ Global
137151
{EB36BCDD-02D6-457A-BA3E-A42F6A8544D3} = {B20F5F9A-5FD0-4C28-AD4F-28CC0591F530}
138152
{34F1DA0D-5897-44D4-AC31-CBFA654D47D7} = {E7AADF05-AF2E-4A54-ACFB-0F8A971BD528}
139153
{6FD5F7B1-4A40-408E-8BF3-DA89C0873045} = {E7AADF05-AF2E-4A54-ACFB-0F8A971BD528}
154+
{BF171E67-B282-4BD8-9511-D4218BDA8BD4} = {2590E36B-A3E2-41EC-8A85-C79D3B72DEC5}
155+
{AC86D8BA-7B66-40D6-9351-870BBE35457F} = {E7AADF05-AF2E-4A54-ACFB-0F8A971BD528}
140156
EndGlobalSection
141157
GlobalSection(ExtensibilityGlobals) = postSolution
142158
SolutionGuid = {86DEDE4A-7156-45A1-BEB3-3C62CF81256E}

0 commit comments

Comments
 (0)