Skip to content

Commit bbd8d92

Browse files
committed
add more solutions
1 parent 74a04d8 commit bbd8d92

File tree

219 files changed

+19395
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+19395
-183
lines changed

c#/accounts_merge.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,128 @@
1-
# accounts_merge.md
1+
# 721. [Accounts Merge](https://leetcode.com/problems/accounts-merge/)
2+
3+
## Approach 1: Union-Find
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(A * logA), where A is the total number of emails
9+
// Space Complexity: O(A)
10+
using System;
11+
using System.Collections.Generic;
12+
13+
public class Solution {
14+
public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) {
15+
// Map each email to a parent email
16+
Dictionary<string, string> parent = new Dictionary<string, string>();
17+
// Map to retrieve the name associated with each email
18+
Dictionary<string, string> owner = new Dictionary<string, string>();
19+
20+
// Initialize parent to self and store owner for each email
21+
foreach (var account in accounts) {
22+
string ownerName = account[0];
23+
for (int i = 1; i < account.Count; i++) {
24+
if (!parent.ContainsKey(account[i])) {
25+
parent[account[i]] = account[i]; // Each email is its own parent initially
26+
owner[account[i]] = ownerName; // Record owner of each email
27+
}
28+
}
29+
}
30+
31+
// Union-Find: Union the first email with the rest in each account
32+
foreach (var account in accounts) {
33+
string firstEmail = Find(account[1], parent);
34+
for (int i = 2; i < account.Count; i++) {
35+
string nextEmail = Find(account[i], parent);
36+
parent[nextEmail] = firstEmail;
37+
}
38+
}
39+
40+
// Group emails by their root parent
41+
Dictionary<string, SortedSet<string>> unionedEmails = new Dictionary<string, SortedSet<string>>();
42+
foreach (var email in parent.Keys) {
43+
string rootEmail = Find(email, parent);
44+
if (!unionedEmails.ContainsKey(rootEmail))
45+
unionedEmails[rootEmail] = new SortedSet<string>();
46+
unionedEmails[rootEmail].Add(email);
47+
}
48+
49+
// Construct result using owners and grouped emails
50+
List<IList<string>> result = new List<IList<string>>();
51+
foreach (var rootEmail in unionedEmails.Keys) {
52+
List<string> emails = new List<string>(unionedEmails[rootEmail]);
53+
emails.Insert(0, owner[rootEmail]); // Add owner to the start
54+
result.Add(emails);
55+
}
56+
57+
return result;
58+
}
59+
60+
// Helper method to find the root of an email
61+
private string Find(string email, Dictionary<string, string> parent) {
62+
if (parent[email] != email) {
63+
parent[email] = Find(parent[email], parent); // Path compression
64+
}
65+
return parent[email];
66+
}
67+
}
68+
```
69+
70+
## Approach 2: DFS to Group Emails
71+
72+
### Solution
73+
csharp
74+
```csharp
75+
// Time Complexity: O(A * N), where A is the total number of emails, and N is the average number of emails in an account
76+
// Space Complexity: O(A)
77+
using System;
78+
using System.Collections.Generic;
79+
80+
public class Solution {
81+
public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) {
82+
Dictionary<string, string> emailToName = new Dictionary<string, string>();
83+
Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();
84+
85+
// Build graph and map email to owner
86+
foreach (var account in accounts) {
87+
string name = account[0];
88+
for (int i = 1; i < account.Count; i++) {
89+
emailToName[account[i]] = name;
90+
if (!graph.ContainsKey(account[i]))
91+
graph[account[i]] = new List<string>();
92+
if (i == 1) continue;
93+
// Create undirected connections
94+
graph[account[i - 1]].Add(account[i]);
95+
graph[account[i]].Add(account[i - 1]);
96+
}
97+
}
98+
99+
// Traverse graph to merge accounts
100+
HashSet<string> visited = new HashSet<string>();
101+
List<IList<string>> result = new List<IList<string>>();
102+
103+
foreach (var email in emailToName.Keys) {
104+
if (!visited.Contains(email)) {
105+
List<string> list = new List<string>();
106+
DFS(email, graph, visited, list);
107+
list.Sort();
108+
list.Insert(0, emailToName[email]); // Add owner to the start
109+
result.Add(list);
110+
}
111+
}
112+
113+
return result;
114+
}
115+
116+
// Helper method for DFS traversal
117+
private void DFS(string email, Dictionary<string, List<string>> graph, HashSet<string> visited, List<string> list) {
118+
visited.Add(email);
119+
list.Add(email);
120+
foreach (var neighbor in graph[email]) {
121+
if (!visited.Contains(neighbor)) {
122+
DFS(neighbor, graph, visited, list);
123+
}
124+
}
125+
}
126+
}
127+
```
2128

c#/binary_tree_paths.md

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,144 @@
1-
# binary_tree_paths.md
1+
# 257. [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)
2+
3+
## Approach 1: Recursive Depth-First Search (DFS)
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n), where n is the number of nodes in the tree
9+
// Space Complexity: O(h), where h is the height of the tree for recursion stack
10+
using System.Collections.Generic;
11+
12+
public class TreeNode {
13+
public int val;
14+
public TreeNode left;
15+
public TreeNode right;
16+
public TreeNode(int x) { val = x; }
17+
}
18+
19+
public class Solution {
20+
public IList<string> BinaryTreePaths(TreeNode root) {
21+
List<string> result = new List<string>();
22+
if (root != null) {
23+
Dfs(root, "", result);
24+
}
25+
return result;
26+
}
27+
28+
private void Dfs(TreeNode node, string path, List<string> result) {
29+
path += node.val;
30+
31+
if (node.left == null && node.right == null) {
32+
result.Add(path);
33+
return;
34+
}
35+
36+
if (node.left != null) {
37+
Dfs(node.left, path + "->", result);
38+
}
39+
if (node.right != null) {
40+
Dfs(node.right, path + "->", result);
41+
}
42+
}
43+
}
44+
```
45+
46+
## Approach 2: Iterative Depth-First Search (Using Stack)
47+
48+
### Solution
49+
csharp
50+
```csharp
51+
// Time Complexity: O(n), where n is the number of nodes in the tree
52+
// Space Complexity: O(n), for the stack and path storage
53+
using System.Collections.Generic;
54+
55+
public class TreeNode {
56+
public int val;
57+
public TreeNode left;
58+
public TreeNode right;
59+
public TreeNode(int x) { val = x; }
60+
}
61+
62+
public class Solution {
63+
public IList<string> BinaryTreePaths(TreeNode root) {
64+
List<string> result = new List<string>();
65+
if (root == null) {
66+
return result;
67+
}
68+
69+
Stack<TreeNode> stack = new Stack<TreeNode>();
70+
Stack<string> paths = new Stack<string>();
71+
stack.Push(root);
72+
paths.Push(root.val.ToString());
73+
74+
while (stack.Count > 0) {
75+
TreeNode currentNode = stack.Pop();
76+
string currentPath = paths.Pop();
77+
78+
if (currentNode.left == null && currentNode.right == null) {
79+
result.Add(currentPath);
80+
}
81+
82+
if (currentNode.right != null) {
83+
stack.Push(currentNode.right);
84+
paths.Push(currentPath + "->" + currentNode.right.val);
85+
}
86+
if (currentNode.left != null) {
87+
stack.Push(currentNode.left);
88+
paths.Push(currentPath + "->" + currentNode.left.val);
89+
}
90+
}
91+
92+
return result;
93+
}
94+
}
95+
```
96+
97+
## Approach 3: Backtracking
98+
99+
### Solution
100+
csharp
101+
```csharp
102+
// Time Complexity: O(n), where n is the number of nodes in the tree
103+
// Space Complexity: O(h), where h is the height of the tree for recursion stack
104+
using System.Collections.Generic;
105+
using System.Text;
106+
107+
public class TreeNode {
108+
public int val;
109+
public TreeNode left;
110+
public TreeNode right;
111+
public TreeNode(int x) { val = x; }
112+
}
113+
114+
public class Solution {
115+
public IList<string> BinaryTreePaths(TreeNode root) {
116+
List<string> result = new List<string>();
117+
if (root == null) {
118+
return result;
119+
}
120+
Backtrack(root, new StringBuilder(), result);
121+
return result;
122+
}
123+
124+
private void Backtrack(TreeNode node, StringBuilder path, List<string> result) {
125+
int len = path.Length;
126+
path.Append(node.val);
127+
128+
if (node.left == null && node.right == null) {
129+
result.Add(path.ToString());
130+
} else {
131+
path.Append("->");
132+
if (node.left != null) {
133+
Backtrack(node.left, path, result);
134+
}
135+
if (node.right != null) {
136+
Backtrack(node.right, path, result);
137+
}
138+
}
139+
140+
path.Length = len;
141+
}
142+
}
143+
```
2144

0 commit comments

Comments
 (0)