Skip to content

Commit c73f583

Browse files
authored
Merge pull request #1195 from ShrujanKotturi/main
Updated remaining C# solutions
2 parents 77f932e + 6bf5d65 commit c73f583

37 files changed

+1818
-58
lines changed

Diff for: README.md

+39-38
Large diffs are not rendered by default.

Diff for: csharp/10-Regular-Expression-Matching.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution
2+
{
3+
// T: O(M*N) | S: O(M*N)
4+
public bool IsMatch(string s, string p)
5+
{
6+
// Top down
7+
var cache = new Dictionary<(int, int), bool>();
8+
9+
bool dfs(int i, int j)
10+
{
11+
if (cache.ContainsKey((i, j)))
12+
return cache[(i, j)];
13+
if (i >= s.Length && j >= p.Length)
14+
return true;
15+
if (j >= p.Length)
16+
return false;
17+
18+
var match = i < s.Length && (s[i] == p[j] || p[j] == '.');
19+
if (j + 1 < p.Length && p[j + 1] == '*')
20+
{
21+
cache.TryAdd((i, j), false);
22+
cache[(i, j)] = (match && dfs(i + 1, j)) || //use *
23+
dfs(i, j + 2); //dont use *
24+
return cache[(i, j)];
25+
}
26+
27+
if (match)
28+
{
29+
cache.TryAdd((i, j), false);
30+
cache[(i, j)] = dfs(i + 1, j + 1);
31+
return cache[(i, j)];
32+
33+
}
34+
35+
cache.TryAdd((i, j), false);
36+
cache[(i, j)] = false;
37+
return cache[(i, j)];
38+
39+
}
40+
41+
return dfs(0, 0);
42+
}
43+
}

Diff for: csharp/1046-Last-Stone-Weight.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
public class Solution
2+
{
3+
private PriorityQueue<int, int> pq;
4+
5+
// T: O(NLogN)
6+
public int LastStoneWeight(int[] stones)
7+
{
8+
pq = new PriorityQueue<int, int>(new MaxHeapComparer());
9+
10+
AddStones(stones);
11+
ComputeLastStoneWeight();
12+
13+
return pq.Count == 0 ? 0 : pq.Dequeue();
14+
}
15+
16+
17+
private void AddStones(int[] stones)
18+
{
19+
foreach (var stone in stones)
20+
{
21+
// T: Heapify is O(N) for every enqueued item
22+
pq.Enqueue(stone, stone);
23+
}
24+
}
25+
26+
// T: O(NLogN), to get max value its O(LogN) and we perform this for N items => O(NLogN)
27+
private void ComputeLastStoneWeight()
28+
{
29+
while (pq.Count > 1)
30+
{
31+
var y = pq.Dequeue();
32+
var x = pq.Dequeue();
33+
34+
if (x != y)
35+
{
36+
var diff = y - x;
37+
pq.Enqueue(diff, diff);
38+
}
39+
}
40+
}
41+
42+
public class MaxHeapComparer : IComparer<int>
43+
{
44+
public int Compare(int x, int y)
45+
{
46+
return y - x;
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
15+
public class Solution
16+
{
17+
public TreeNode BuildTree(int[] preorder, int[] inorder)
18+
{
19+
return BuildTreeHelper(0, 0, inorder.Length - 1, preorder, inorder);
20+
}
21+
22+
private TreeNode BuildTreeHelper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder)
23+
{
24+
if (preorder.Length == 0 && inorder.Length == 0)
25+
return null;
26+
27+
if (preStart > preorder.Length - 1 || inStart > inEnd)
28+
return null;
29+
30+
var rootNode = new TreeNode(preorder[preStart]);
31+
var mid = Array.IndexOf(inorder, preorder[preStart]);
32+
33+
rootNode.left = BuildTreeHelper(preStart + 1, inStart, mid - 1, preorder, inorder);
34+
rootNode.right = BuildTreeHelper(preStart + mid - inStart + 1, mid + 1, inEnd, preorder, inorder);
35+
36+
return rootNode;
37+
}
38+
}

Diff for: csharp/110-Balanced-Binary-Tree.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution
15+
{
16+
17+
//T: O(N) and S: O(H)
18+
public bool IsBalanced(TreeNode root)
19+
{
20+
return checkHeight(root) != int.MinValue;
21+
}
22+
23+
private int checkHeight(TreeNode root)
24+
{
25+
if (root == null)
26+
return -1;
27+
var leftHeight = checkHeight(root.left);
28+
if (leftHeight == int.MinValue) return leftHeight;
29+
30+
var rightHeight = checkHeight(root.right);
31+
if (rightHeight == int.MinValue) return rightHeight;
32+
33+
var heightDiff = leftHeight - rightHeight;
34+
if (Math.Abs(heightDiff) > 1)
35+
return int.MinValue;
36+
else
37+
return Math.Max(leftHeight, rightHeight) + 1;
38+
39+
}
40+
}

Diff for: csharp/127-Word-Ladder.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class Solution
2+
{
3+
//T: O(N^2*M) where N is the length of the word, M is total no. of words
4+
//S: O(N^2*M) where N is the length of the word, M is total no. of words
5+
public int LadderLength(string beginWord, string endWord, IList<string> wordList)
6+
{
7+
if (!wordList.Contains(endWord))
8+
{
9+
return 0;
10+
}
11+
12+
var nei = new Dictionary<string,
13+
HashSet<string>>();
14+
if (!wordList.Contains(beginWord))
15+
wordList.Add(beginWord);
16+
17+
foreach (var word in wordList)
18+
{
19+
for (var j = 0; j < word.Length; j++)
20+
{
21+
var pattern = word.Substring(0, j) + "*" + word.Substring(j + 1);
22+
nei.TryAdd(pattern, new HashSet<string>());
23+
nei[pattern].Add(word);
24+
}
25+
}
26+
27+
foreach (var neiWord in nei.Keys)
28+
{
29+
foreach (var val in nei[neiWord])
30+
{
31+
Console.WriteLine("neiWord : " + neiWord + " val : " + val);
32+
}
33+
}
34+
35+
var visited = new HashSet<string>();
36+
visited.Add(beginWord);
37+
38+
var queue = new Queue<string>();
39+
queue.Enqueue(beginWord);
40+
41+
var result = 1;
42+
43+
while (queue.Count > 0)
44+
{
45+
var count = queue.Count;
46+
for (var i = 0; i < count; i++)
47+
{
48+
var item = queue.Dequeue();
49+
if (string.Equals(item, endWord))
50+
return result;
51+
52+
for (var j = 0; j < item.Length; j++)
53+
{
54+
var pattern = item.Substring(0, j) + "*" + item.Substring(j + 1);
55+
foreach (var neiWord in nei[pattern])
56+
{
57+
if (!visited.Contains(neiWord))
58+
{
59+
queue.Enqueue(neiWord);
60+
visited.Add(neiWord);
61+
62+
}
63+
}
64+
65+
}
66+
67+
}
68+
result += 1;
69+
}
70+
71+
return 0;
72+
}
73+
}

Diff for: csharp/131-Palindrome-Partitioning.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
public class Solution
2+
{
3+
//O(N.2^N)
4+
public IList<IList<string>> Partition(string s)
5+
{
6+
var result = new List<IList<string>>();
7+
var stack = new List<string>();
8+
9+
void dfs(int i)
10+
{
11+
if (i >= s.Length)
12+
{
13+
result.Add(stack.ToList());
14+
return;
15+
}
16+
17+
for (var j = i; j < s.Length; j++)
18+
{
19+
if (IsPalindrome(s, i, j))
20+
{
21+
stack.Add(s.Substring(i, j - i + 1));
22+
dfs(j + 1);
23+
stack.RemoveAt(stack.Count - 1);
24+
}
25+
}
26+
27+
}
28+
29+
dfs(0);
30+
31+
return result;
32+
}
33+
34+
public bool IsPalindrome(string s, int l, int r)
35+
{
36+
while (l < r)
37+
{
38+
if (s[l] != s[r])
39+
return false;
40+
l++;
41+
r--;
42+
}
43+
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)