Skip to content

Commit f23c6cd

Browse files
Zanger67/leetcodeZanger67/leetcode
authored andcommitted
Updated markdown files
1 parent 515c832 commit f23c6cd

11 files changed

+2405
-2315
lines changed

README.md

Lines changed: 573 additions & 572 deletions
Large diffs are not rendered by default.

markdowns/Medium.md

Lines changed: 369 additions & 368 deletions
Large diffs are not rendered by default.

markdowns/Questions_By_Code_Length.md

Lines changed: 571 additions & 570 deletions
Large diffs are not rendered by default.

markdowns/Questions_By_Recent.md

Lines changed: 571 additions & 570 deletions
Large diffs are not rendered by default.

markdowns/Topics.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
- [Array](<by_topic/Array.md>) (278 completed)
88
- [String](<by_topic/String.md>) (131 completed)
99
- [Hash Table](<by_topic/Hash Table.md>) (127 completed)
10-
- [Depth-First Search](<by_topic/Depth-First Search.md>) (98 completed)
10+
- [Depth-First Search](<by_topic/Depth-First Search.md>) (99 completed)
1111
- [Tree](<by_topic/Tree.md>) (90 completed)
1212
- [Binary Tree](<by_topic/Binary Tree.md>) (81 completed)
1313
- [Sorting](<by_topic/Sorting.md>) (73 completed)
1414
- [Math](<by_topic/Math.md>) (68 completed)
15-
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (66 completed)
15+
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (67 completed)
1616
- [Two Pointers](<by_topic/Two Pointers.md>) (55 completed)
1717
- [Stack](<by_topic/Stack.md>) (52 completed)
1818
- [Matrix](<by_topic/Matrix.md>) (51 completed)
@@ -29,12 +29,12 @@
2929
- [Trie](<by_topic/Trie.md>) (19 completed)
3030
- [Database](<by_topic/Database.md>) (18 completed)
3131
- [Prefix Sum](<by_topic/Prefix Sum.md>) (17 completed)
32+
- [Graph](<by_topic/Graph.md>) (16 completed)
3233
- [Recursion](<by_topic/Recursion.md>) (16 completed)
3334
- [Binary Search Tree](<by_topic/Binary Search Tree.md>) (16 completed)
34-
- [Graph](<by_topic/Graph.md>) (15 completed)
3535
- [Sliding Window](<by_topic/Sliding Window.md>) (15 completed)
3636
- [Queue](<by_topic/Queue.md>) (14 completed)
37-
- [Union Find](<by_topic/Union Find.md>) (13 completed)
37+
- [Union Find](<by_topic/Union Find.md>) (14 completed)
3838
- [Monotonic Stack](<by_topic/Monotonic Stack.md>) (11 completed)
3939
- [String Matching](<by_topic/String Matching.md>) (8 completed)
4040
- [Divide and Conquer](<by_topic/Divide and Conquer.md>) (7 completed)

markdowns/Weekly_Questions.md

Lines changed: 28 additions & 27 deletions
Large diffs are not rendered by default.

markdowns/_261. Graph Valid Tree.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 261. [Graph Valid Tree](<https://leetcode.com/problems/graph-valid-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : January 29, 2025*
10+
>
11+
> *Last updated : January 29, 2025*
12+
13+
------
14+
15+
> **Related Topics** : **[Depth-First Search](<by_topic/Depth-First Search.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Union Find](<by_topic/Union Find.md>), [Graph](<by_topic/Graph.md>)**
16+
>
17+
> **Acceptance Rate** : **48.88 %**
18+
19+
------
20+
21+
> Notes
22+
>
23+
> - For a tree to be valid, $|E|=|V|-1$ must be true where $E$ is the edge set and $V$ is the vertex set
24+
> - Check for cycle starting from a random node -- track visited nodes
25+
> - If any cycle is found, we return False
26+
> - If no cycle is found, we continue
27+
> - Check if all nodes were visited
28+
> - If all nodes visited, we return True
29+
> - Else, we return False
30+
>
31+
> In theory the all nodes visited check is all that we need as long as avoid infinite looping in cycles. However, it's more efficient if we break out early if a cycle is found.
32+
>
33+
> The reason for the all nodes visited check after cycles is for graphs such as this:
34+
>
35+
> ```
36+
> 1 2
37+
> | / \
38+
> 3 4---5
39+
> ```
40+
>
41+
> This graph has 5 vertices and 4 edges which fulfills the first condition. It's possible we'll start our cycle check on vertex 1 wherein we won't have a cycle, but there's a cycle elsewhere.
42+
>
43+
44+
------
45+
46+
## Solutions
47+
48+
- [m261.py](<../my-submissions/m261.py>)
49+
### Python
50+
#### [m261.py](<../my-submissions/m261.py>)
51+
```Python
52+
class Solution:
53+
def validTree(self, n: int, edges: List[List[int]]) -> bool:
54+
if len(edges) != n - 1 :
55+
return False
56+
57+
# Create edge map
58+
e = defaultdict(set)
59+
for u, v in edges :
60+
e[u].add(v)
61+
e[v].add(u)
62+
63+
# Detect cycle to save runtime -- breaks out early if cycle detected otherwise attempts
64+
# to validate by visiting all n edges
65+
def dfs_detect_cycle(curr: int, prev: int, e: defaultdict, visited: Set[int]) -> bool :
66+
if curr in visited :
67+
return True
68+
visited.add(curr)
69+
if any(dfs_detect_cycle(nxt, curr, e, visited) for nxt in e[curr] if nxt != prev) :
70+
return (True, visited)
71+
return False
72+
73+
# Since dfs_detect_cycle() is called first before the or, visited() will be
74+
# filled prior to len() being tested
75+
visited = set()
76+
if dfs_detect_cycle(1, None, e, visited) or not len(visited) == n :
77+
return False
78+
79+
return True
80+
```
81+

markdowns/by_topic/Breadth-First Search.md

Lines changed: 70 additions & 69 deletions
Large diffs are not rendered by default.

markdowns/by_topic/Depth-First Search.md

Lines changed: 102 additions & 101 deletions
Large diffs are not rendered by default.

markdowns/by_topic/Graph.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
# [Graph](<https://leetcode.com/tag/Graph/>) (15 completed)
1+
# [Graph](<https://leetcode.com/tag/Graph/>) (16 completed)
22

33
*[Back to top](<../../README.md>)*
44

55
------
66

7-
| # | Title | Level | Cats | Solution | Languages | Date Complete |
8-
|-----:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:-------------------------------------------------------------------------------------------------------|:-----------------|:----------------|
9-
| 684 | [Redundant Connection](<https://leetcode.com/problems/redundant-connection>) | Medium | Daily, N150 | [solution](<../_684. Redundant Connection.md>) | py | Jan 29, 2025 |
10-
| 743 | [Network Delay Time](<https://leetcode.com/problems/network-delay-time>) | Medium | N150 | [solution](<../_743. Network Delay Time.md>) | java, py | Jul 28, 2024 |
11-
| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](<https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance>) | Medium | Daily | [solution](<../_1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance.md>) | py | Jul 26, 2024 |
12-
| 1368 | [Minimum Cost to Make at Least One Valid Path in a Grid](<https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid>) | Hard | Daily | [solution](<../_1368. Minimum Cost to Make at Least One Valid Path in a Grid.md>) | py | Jan 18, 2025 |
13-
| 1462 | [Course Schedule IV](<https://leetcode.com/problems/course-schedule-iv>) | Medium | Daily | [solution](<../_1462. Course Schedule IV.md>) | py | Jan 27, 2025 |
14-
| 1579 | [Remove Max Number of Edges to Keep Graph Fully Traversable](<https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable>) | Hard | Daily | [solution](<../_1579. Remove Max Number of Edges to Keep Graph Fully Traversable.md>) | py | Jun 30, 2024 |
15-
| 1791 | [Find Center of Star Graph](<https://leetcode.com/problems/find-center-of-star-graph>) | Easy | Daily | [solution](<../_1791. Find Center of Star Graph.md>) | c, cpp, java, py | Jun 27, 2024 |
16-
| 2045 | [Second Minimum Time to Reach Destination](<https://leetcode.com/problems/second-minimum-time-to-reach-destination>) | Hard | Daily | [solution](<../_2045. Second Minimum Time to Reach Destination.md>) | py | Jul 28, 2024 |
17-
| 2093 | [Minimum Cost to Reach City With Discounts](<https://leetcode.com/problems/minimum-cost-to-reach-city-with-discounts>) | Medium | Weekly Premium | [solution](<../_2093. Minimum Cost to Reach City With Discounts.md>) | py | Jul 23, 2024 |
18-
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](<https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph>) | Medium | Daily | [solution](<../_2192. All Ancestors of a Node in a Directed Acyclic Graph.md>) | java, py | Jun 29, 2024 |
19-
| 2285 | [Maximum Total Importance of Roads](<https://leetcode.com/problems/maximum-total-importance-of-roads>) | Medium | Daily | [solution](<../_2285. Maximum Total Importance of Roads.md>) | c, cpp, java, py | Jun 28, 2024 |
20-
| 2374 | [Node With Highest Edge Score](<https://leetcode.com/problems/node-with-highest-edge-score>) | Medium | | [solution](<../_2374. Node With Highest Edge Score.md>) | py | Jun 18, 2024 |
21-
| 2392 | [Build a Matrix With Conditions](<https://leetcode.com/problems/build-a-matrix-with-conditions>) | Hard | Daily | [solution](<../_2392. Build a Matrix With Conditions.md>) | py | Jul 21, 2024 |
22-
| 2473 | [Minimum Cost to Buy Apples](<https://leetcode.com/problems/minimum-cost-to-buy-apples>) | Medium | | [solution](<../_2473. Minimum Cost to Buy Apples.md>) | py | Jun 29, 2024 |
23-
| 2976 | [Minimum Cost to Convert String I](<https://leetcode.com/problems/minimum-cost-to-convert-string-i>) | Medium | Daily | [solution](<../_2976. Minimum Cost to Convert String I.md>) | py | Jul 27, 2024 |
7+
| # | Title | Level | Cats | Solution | Languages | Date Complete |
8+
|-----:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:--------------------------|:-------------------------------------------------------------------------------------------------------|:-----------------|:----------------|
9+
| 261 | [Graph Valid Tree](<https://leetcode.com/problems/graph-valid-tree>) | Medium | B75, N150, Weekly Premium | [solution](<../_261. Graph Valid Tree.md>) | py | Jan 29, 2025 |
10+
| 684 | [Redundant Connection](<https://leetcode.com/problems/redundant-connection>) | Medium | Daily, N150 | [solution](<../_684. Redundant Connection.md>) | py | Jan 29, 2025 |
11+
| 743 | [Network Delay Time](<https://leetcode.com/problems/network-delay-time>) | Medium | N150 | [solution](<../_743. Network Delay Time.md>) | java, py | Jul 28, 2024 |
12+
| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](<https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance>) | Medium | Daily | [solution](<../_1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance.md>) | py | Jul 26, 2024 |
13+
| 1368 | [Minimum Cost to Make at Least One Valid Path in a Grid](<https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid>) | Hard | Daily | [solution](<../_1368. Minimum Cost to Make at Least One Valid Path in a Grid.md>) | py | Jan 18, 2025 |
14+
| 1462 | [Course Schedule IV](<https://leetcode.com/problems/course-schedule-iv>) | Medium | Daily | [solution](<../_1462. Course Schedule IV.md>) | py | Jan 27, 2025 |
15+
| 1579 | [Remove Max Number of Edges to Keep Graph Fully Traversable](<https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable>) | Hard | Daily | [solution](<../_1579. Remove Max Number of Edges to Keep Graph Fully Traversable.md>) | py | Jun 30, 2024 |
16+
| 1791 | [Find Center of Star Graph](<https://leetcode.com/problems/find-center-of-star-graph>) | Easy | Daily | [solution](<../_1791. Find Center of Star Graph.md>) | c, cpp, java, py | Jun 27, 2024 |
17+
| 2045 | [Second Minimum Time to Reach Destination](<https://leetcode.com/problems/second-minimum-time-to-reach-destination>) | Hard | Daily | [solution](<../_2045. Second Minimum Time to Reach Destination.md>) | py | Jul 28, 2024 |
18+
| 2093 | [Minimum Cost to Reach City With Discounts](<https://leetcode.com/problems/minimum-cost-to-reach-city-with-discounts>) | Medium | Weekly Premium | [solution](<../_2093. Minimum Cost to Reach City With Discounts.md>) | py | Jul 23, 2024 |
19+
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](<https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph>) | Medium | Daily | [solution](<../_2192. All Ancestors of a Node in a Directed Acyclic Graph.md>) | java, py | Jun 29, 2024 |
20+
| 2285 | [Maximum Total Importance of Roads](<https://leetcode.com/problems/maximum-total-importance-of-roads>) | Medium | Daily | [solution](<../_2285. Maximum Total Importance of Roads.md>) | c, cpp, java, py | Jun 28, 2024 |
21+
| 2374 | [Node With Highest Edge Score](<https://leetcode.com/problems/node-with-highest-edge-score>) | Medium | | [solution](<../_2374. Node With Highest Edge Score.md>) | py | Jun 18, 2024 |
22+
| 2392 | [Build a Matrix With Conditions](<https://leetcode.com/problems/build-a-matrix-with-conditions>) | Hard | Daily | [solution](<../_2392. Build a Matrix With Conditions.md>) | py | Jul 21, 2024 |
23+
| 2473 | [Minimum Cost to Buy Apples](<https://leetcode.com/problems/minimum-cost-to-buy-apples>) | Medium | | [solution](<../_2473. Minimum Cost to Buy Apples.md>) | py | Jun 29, 2024 |
24+
| 2976 | [Minimum Cost to Convert String I](<https://leetcode.com/problems/minimum-cost-to-convert-string-i>) | Medium | Daily | [solution](<../_2976. Minimum Cost to Convert String I.md>) | py | Jul 27, 2024 |

0 commit comments

Comments
 (0)