Skip to content

Commit b41fb4a

Browse files
committed
Added priority queue problem solutions
1 parent b5513a0 commit b41fb4a

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
3+
namespace ConsoleApp1.PriorityQueue.Easy;
4+
5+
// 703. Kth Largest Element in a Stream
6+
public class KthLargest
7+
{
8+
private PriorityQueue<int, int> _priorityQueue;
9+
private int k;
10+
11+
public KthLargest(int k, int[] nums)
12+
{
13+
_priorityQueue = new PriorityQueue<int, int>();
14+
this.k = k;
15+
InitQueue(nums);
16+
}
17+
18+
// Time complexity: O(log(n));
19+
public int Add(int val)
20+
{
21+
if (_priorityQueue.Count < k)
22+
_priorityQueue.Enqueue(val, val);
23+
else if (val > _priorityQueue.Peek())
24+
{
25+
_priorityQueue.Dequeue();
26+
_priorityQueue.Enqueue(val, val);
27+
}
28+
29+
return _priorityQueue.Peek();
30+
}
31+
32+
// Time complexity: O(nlog(k)); Space complexity: O(k).
33+
public void InitQueue(int[] nums)
34+
{
35+
foreach (var item in nums)
36+
Add(item);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace ConsoleApp1.PriorityQueue.Easy;
5+
6+
// 1046. Last Stone Weight
7+
public class LastStoneWeight
8+
{
9+
// Time complexity: O(nlog(n)); Space complexity: O(n).
10+
public int GetLastStoneWeight(int[] stones)
11+
{
12+
var stonesWithPriority = stones.Select(item => (item, item));
13+
var priorityQueue = new PriorityQueue<int, int>(
14+
stonesWithPriority,
15+
Comparer<int>.Create((x,y) => y-x)
16+
);
17+
18+
while (priorityQueue.Count > 1)
19+
{
20+
var stone1 = priorityQueue.Dequeue();
21+
var stone2 = priorityQueue.Dequeue();
22+
23+
var diff = stone1 - stone2;
24+
25+
if (diff != 0) priorityQueue.Enqueue(diff, diff);
26+
}
27+
28+
return priorityQueue.Count > 0 ? priorityQueue.Peek() : 0;
29+
}
30+
}

AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/SymmetricTree.cs

+22
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
namespace ConsoleApp1.Trees.Easy;
55

6+
// 101. Symmetric Tree
67
public class SymmetricTree
78
{
9+
// Time complexity: O(n); Space complexity: O(h) - height of the tree.
810
public bool IsSymmetric(TreeNode root)
911
{
1012
if (root == null) return true;
@@ -32,4 +34,24 @@ public bool IsSymmetric(TreeNode root)
3234

3335
return true;
3436
}
37+
38+
// Time complexity: O(n); Space complexity: O(h) - height of the tree.
39+
public bool IsSymmetric2(TreeNode root)
40+
{
41+
if (root == null)
42+
return true;
43+
44+
return IsMirror(root.left, root.right);
45+
}
46+
47+
private bool IsMirror(TreeNode node1, TreeNode node2)
48+
{
49+
if (node1 == null && node2 == null)
50+
return true;
51+
52+
if (node1 == null || node2 == null)
53+
return false;
54+
55+
return node1.val == node2.val && IsMirror(node1.left, node2.right) && IsMirror(node1.right, node2.left);
56+
}
3557
}

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Binary Search](#binary-search)
1515
- [Linked List](#linked-list)
1616
- [Trees](#trees)
17+
- [Priority Queue](#priority-queue)
1718
- [Hints](#hints)
1819

1920
## Arrays & Hashing
@@ -97,6 +98,7 @@
9798
| ------- | ---------- | --------------- | ---------------- | -------------- |
9899
| [94. Binary Tree Inorder Traversal](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/BinaryTreeInorderTraversal.cs) | Easy | O(n) | O(n) | - For iterative approach use stack (while stack.Any || currNode != null) <br /> - If currNode != null - push node to stack, go left <br /> - If currNode == null - get prevNode from stack, add value to result,go the prevNode.right |
99100
| [100. Same Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/SameTree.cs) | Easy | O(n) | O(n) | Check that values of trees nodes are equal, use recursion to repeat for each node (left and right) |
101+
| [101. Symmetric Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/SymmetricTree.cs) | Easy | O(n) | O(h) | - Start to check from root left and right children <br /> - If both null - return true, if onle one is null - false, then if left.val != right.val - return false <br /> - Then we need to check node1.left with node2.right and node1.right with node2.left (repeat recursively) |
100102
| [104. Maximum Depth of Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/MaximumDepthOfBinaryTree.cs) | Easy | O(n) | O(n) | - DFS iterative - use stack to store nodes and depth <br /> - BFS iterative - use queue to count levels <br /> - Recursion - return Max of recursive calls of left and right children + 1 |
101103
| [108. Convert Sorted Array to Binary Search Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/ConvertSortedArrayToBinarySearchTree.cs) | Easy | O(n) | O(log(n)) | - Use **Divide and Conquer** approach: mid value - node value, left subarray - left subtree, right subarray - right subtree <br /> - Use recursion for both left and right subtrees |
102104
| [110. Balanced Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/MaximumDepthOfBinaryTree.cs) | Easy | O(n) | O(n) | - Find max depth of left and right children <br /> - Difference should be <= 1 <br /> - Repeat recursively for each node, if diff is invalid - mark result flag|
@@ -108,6 +110,13 @@
108110
| [572. Subtree of Another Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/SubtreeOfAnotherTree.cs) | Easy | O(nm) | O(nm) | For each node where value = subroot.value we need to check if it is SameTree(problem 100) or node.left/node.right is SameTree as subroot |
109111
| [606. Construct String from Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/ConstructStringFromBinaryTree.cs) | Easy | O(n) | O(n) | Use DFS to recursively build string with PreOrder Traversal |
110112

113+
## Priority Queue
114+
115+
| Problem | Complexity | Time Complexity | Space Complexity | Solution Hints |
116+
| ------- | ---------- | --------------- | ---------------- | -------------- |
117+
| [703. Kth Largest Element in a Stream](AlgorithmsAndDS/AlgorithmsAndDS/PriorityQueue/Easy/KthLargestElementInStream.cs) | Easy | O(log(n)) | O(k) | - Put elements to MinHeap always maintaining k elements <br /> - During adding peek element from queue and compare with value, dequeue and enqueue new value if it is greater |
118+
| [1046. Last Stone Weight](AlgorithmsAndDS/AlgorithmsAndDS/PriorityQueue/Easy/LastStoneWeight.cs) | Easy | O(nlog(n)) | O(n) | - Put all elements into a priority queue <br /> - Pop out the two biggest, push back the difference, until there are no more two elements left |
119+
111120
## Hints
112121

113122
- Find range sum/sum in array - prefix/postfix sum of array

0 commit comments

Comments
 (0)