Skip to content

Commit 756987e

Browse files
authored
Merge pull request #1958 from kiryl-labada/main
Update C# solutions
2 parents 187941b + 6793711 commit 756987e

File tree

5 files changed

+80
-193
lines changed

5 files changed

+80
-193
lines changed

csharp/0110-balanced-binary-tree.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,24 @@
1111
* }
1212
* }
1313
*/
14-
public class Solution
15-
{
14+
public class Solution {
15+
private bool _result = true;
1616

17-
//T: O(N) and S: O(H)
18-
public bool IsBalanced(TreeNode root)
19-
{
20-
return checkHeight(root) != int.MinValue;
17+
public bool IsBalanced(TreeNode root) {
18+
Dfs(root);
19+
return _result;
2120
}
2221

23-
private int checkHeight(TreeNode root)
24-
{
25-
if (root == null)
22+
private int Dfs(TreeNode root) {
23+
if(root == null) {
2624
return -1;
27-
var leftHeight = checkHeight(root.left);
28-
if (leftHeight == int.MinValue) return leftHeight;
25+
}
2926

30-
var rightHeight = checkHeight(root.right);
31-
if (rightHeight == int.MinValue) return rightHeight;
27+
var leftDepth = Dfs(root.left);
28+
var rightDepth = Dfs(root.right);
3229

33-
var heightDiff = leftHeight - rightHeight;
34-
if (Math.Abs(heightDiff) > 1)
35-
return int.MinValue;
36-
else
37-
return Math.Max(leftHeight, rightHeight) + 1;
30+
_result = _result && (Math.Abs(rightDepth - leftDepth) <= 1);
3831

32+
return Math.Max(leftDepth, rightDepth) + 1;
3933
}
40-
}
34+
}

csharp/0146-lru-cache.cs

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,39 @@
1-
public class Node
2-
{
3-
public int val;
4-
public int key;
5-
public Node prev;
6-
public Node next;
1+
public class LRUCache {
2+
private Dictionary<int, LinkedListNode<(int key, int value)>> _dict = new();
3+
private LinkedList<(int key, int value)> _values = new();
74

8-
public Node(int key, int val)
9-
{
10-
this.key = key;
11-
this.val = val;
12-
prev = null;
13-
next = null;
14-
}
15-
}
5+
private int _capacity;
166

17-
public class LRUCache
18-
{
19-
private Dictionary<int, Node> keyValue = new();
20-
private int _capacity;
21-
private Node left;
22-
private Node right;
23-
24-
//T: O(1), S: O(Capacity)
25-
public LRUCache(int capacity)
26-
{
7+
public LRUCache(int capacity) {
278
_capacity = capacity;
28-
left = new Node(0, 0);
29-
right = new Node(0, 0);
30-
// left : LRU, right : MRU (Most recently used)
31-
left.next = right;
32-
right.prev = left;
339
}
10+
11+
public int Get(int key) {
12+
if (!_dict.ContainsKey(key)) {
13+
return -1;
14+
}
3415

35-
//Remove from list
36-
private void remove(Node node)
37-
{
38-
var prev = node.prev;
39-
var next = node.next;
40-
41-
prev.next = next;
42-
next.prev = prev;
43-
}
44-
45-
// Insert at right
46-
private void insert(Node node)
47-
{
48-
var next = right;
49-
var prev = right.prev;
50-
51-
node.next = next;
52-
next.prev = node;
53-
prev.next = node;
54-
node.prev = prev;
16+
var node = _dict[key];
17+
_values.Remove(node);
18+
_values.AddFirst(node);
5519

20+
return node.Value.value;
5621
}
57-
58-
public int Get(int key)
59-
{
60-
if (!keyValue.ContainsKey(key))
61-
return -1;
62-
// we need to update this to be MRU
63-
var node = keyValue[key];
64-
remove(node);
65-
insert(node);
66-
return node.val;
67-
}
68-
69-
public void Put(int key, int value)
70-
{
71-
if (keyValue.ContainsKey(key))
72-
{
73-
var node = keyValue[key];
74-
keyValue.Remove(key);
75-
remove(node);
22+
23+
public void Put(int key, int value) {
24+
if (!_dict.ContainsKey(key) && _dict.Count >= _capacity) {
25+
var node = _values.Last;
26+
_dict.Remove(node.Value.key);
27+
_values.Remove(node);
7628
}
7729

78-
var newNode = new Node(key, value);
79-
keyValue.Add(key, newNode);
80-
insert(newNode);
81-
82-
if (keyValue.Count > _capacity)
83-
{
84-
// remove from the list and delete the LRU from dictionary
85-
var lru = left.next;
86-
remove(lru);
87-
keyValue.Remove(lru.key);
30+
var existingNode = _dict.GetValueOrDefault(key);
31+
if (existingNode != null) {
32+
_values.Remove(existingNode);
8833
}
8934

35+
_values.AddFirst((key, value));
36+
_dict[key] = _values.First;
9037
}
9138
}
9239

@@ -95,4 +42,4 @@ public void Put(int key, int value)
9542
* LRUCache obj = new LRUCache(capacity);
9643
* int param_1 = obj.Get(key);
9744
* obj.Put(key,value);
98-
*/
45+
*/

csharp/0199-binary-tree-right-side-view.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,19 @@
1212
* }
1313
*/
1414
public class Solution {
15+
private List<int> _result = new();
16+
1517
public IList<int> RightSideView(TreeNode root) {
16-
var result = new List<int>();
17-
if(root == null)
18-
return result;
19-
var q = new Queue<TreeNode>();
20-
q.Enqueue(root);
21-
22-
// traverse the tree using BFS
23-
while(true) {
24-
var count = q.Count;
25-
if(count == 0) break;
26-
27-
for(var i = 0; i < count; i++) {
28-
var cur = q. Dequeue();
29-
30-
if(cur.left != null)
31-
q.Enqueue(cur.left);
32-
if(cur.right != null)
33-
q.Enqueue(cur.right);
34-
35-
36-
// only add the last node from each level, i.e the rightmost node
37-
if(i == count - 1) {
38-
result.Add(cur.val);
39-
}
40-
}
41-
}
42-
43-
return result;
18+
Dfs(root, 0);
19+
return _result;
4420
}
45-
}
21+
22+
private void Dfs(TreeNode root, int level) {
23+
if (root == null) return;
24+
if (level >= _result.Count) _result.Add(root.val);
25+
26+
// At first visit right node
27+
Dfs(root.right, level + 1);
28+
Dfs(root.left, level + 1);
29+
}
30+
}
Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Definition for a binary tree node.
3-
* public class TreeNode {
3+
* public class TreeNode {
44
* public int val;
55
* public TreeNode left;
66
* public TreeNode right;
@@ -12,34 +12,18 @@
1212
* }
1313
*/
1414
public class Solution {
15-
public bool IsSameTree(TreeNode one, TreeNode another) {
16-
if (one == null || another == null)
17-
return one == another;
18-
19-
return
20-
one.val == another.val &&
21-
IsSameTree(one.left, another.left) &&
22-
IsSameTree(one.right, another.right);
23-
}
2415
public bool IsSubtree(TreeNode root, TreeNode subRoot) {
25-
if (subRoot == null) return true;
26-
if (root == null) return false;
27-
28-
var nodeToVisit = new Queue<TreeNode>();
16+
if (root == null) return root == subRoot;
17+
if (root.val == subRoot?.val && IsSameTree(root, subRoot)) return true;
2918

30-
nodeToVisit.Enqueue(root);
31-
32-
while (nodeToVisit.Count > 0) {
33-
var cur = nodeToVisit.Dequeue();
34-
var isSame = IsSameTree(cur, subRoot);
35-
if (isSame) return true;
19+
return IsSubtree(root.left, subRoot) || IsSubtree(root.right, subRoot);
20+
}
3621

37-
if (cur.left != null)
38-
nodeToVisit.Enqueue(cur.left);
39-
if (cur.right != null)
40-
nodeToVisit.Enqueue(cur.right);
41-
}
22+
public bool IsSameTree(TreeNode p, TreeNode q) {
23+
if (p == null || q == null) return p == q;
4224

43-
return false;
25+
return p.val == q.val
26+
&& IsSameTree(p.left, q.left)
27+
&& IsSameTree(p.right, q.right);
4428
}
45-
}
29+
}
Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,21 @@
11
public class Solution {
2-
// Closest to origin minHeap
3-
private PriorityQueue<int[], double> pq;
4-
private int size;
5-
// T: O(Max(M, KLogM)), S: O(k)
62
public int[][] KClosest(int[][] points, int k) {
7-
pq = new PriorityQueue<int[], double>();
8-
size = k;
9-
10-
AddToPriorityQueue(points);
11-
12-
return Closest();
13-
}
14-
15-
public class MaxHeap : IComparer<double>{
16-
public int Compare(double x, double y){
17-
if( x< y) return 1;å
18-
else if (x > y) return -1;
19-
else return 0;
20-
}
21-
}
22-
23-
// T: O(M)
24-
private void AddToPriorityQueue(int[][] points){
25-
foreach(var point in points){
26-
//var value = (double) Math.Sqrt(point[0]*point[0] + point[1]*point[1]);
27-
var value = (double) point[0]*point[0] + point[1]*point[1];
28-
pq.Enqueue(point, value);
29-
30-
31-
}
32-
}
33-
34-
// T: O(KLogM)
35-
private int[][] Closest(){
36-
var result = new List<int[]>();
37-
while(size > 0){
38-
result.Add(pq.Dequeue());
39-
size--;
3+
var items = points.Select(point => {
4+
long x = point[0];
5+
long y = point[1];
6+
7+
return (point, x * x + y * y);
8+
});
9+
10+
int[][] result = new int[k][];
11+
// T: O(n)
12+
PriorityQueue<int[], long> queue = new(items);
13+
14+
// T: O(k log(n))
15+
for (int i = 0; i < k; i++) {
16+
result[i] = queue.Dequeue();
4017
}
41-
42-
return result.ToArray();
18+
19+
return result;
4320
}
4421
}

0 commit comments

Comments
 (0)