Skip to content

Commit dcd8373

Browse files
authored
Added tasks 206-238
1 parent fd00de6 commit dcd8373

File tree

21 files changed

+630
-0
lines changed

21 files changed

+630
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
9393
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9494
|-|-|-|-|-|-
9595
| 0021 |[Merge Two Sorted Lists](src/main/python/g0001_0100/s0021_merge_two_sorted_lists/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 31 | 92.02
96+
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
9697

9798
#### Day 4 Linked List
9899

@@ -176,6 +177,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
176177
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
177178
|-|-|-|-|-|-
178179
| 0019 |[Remove Nth Node From End of List](src/main/python/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 21 | 99.70
180+
| 0234 |[Palindrome Linked List](src/main/python/g0201_0300/s0234_palindrome_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 282 | 75.97
179181

180182
#### Day 4 Linked List
181183

@@ -191,6 +193,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
191193

192194
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
193195
|-|-|-|-|-|-
196+
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 30 | 87.85
194197

195198
#### Day 7 Tree
196199

@@ -209,6 +212,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
209212

210213
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
211214
|-|-|-|-|-|-
215+
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 44 | 61.91
212216

213217
#### Day 10 Graph/BFS/DFS
214218

@@ -247,6 +251,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
247251

248252
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
249253
|-|-|-|-|-|-
254+
| 0208 |[Implement Trie (Prefix Tree)](src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 140 | 28.87
250255

251256
#### Day 17 Interval
252257

@@ -303,6 +308,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
303308
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 113 | 97.70
304309
| 0001 |[Two Sum](src/main/python/g0001_0100/s0001_two_sum/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 62 | 53.52
305310
| 0055 |[Jump Game](src/main/python/g0001_0100/s0055_jump_game/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 333 | 97.58
311+
| 0238 |[Product of Array Except Self](src/main/python/g0201_0300/s0238_product_of_array_except_self/Solution.py)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 241 | 99.65
306312
| 0041 |[First Missing Positive](src/main/python/g0001_0100/s0041_first_missing_positive/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 334 | 98.05
307313
| 0239 |[Sliding Window Maximum](src/main/python/g0201_0300/s0239_sliding_window_maximum/Solution.py)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 1093 | 66.83
308314

@@ -336,19 +342,24 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
336342
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
337343
|-|-|-|-|-|-
338344
| 0024 |[Swap Nodes in Pairs](src/main/python/g0001_0100/s0024_swap_nodes_in_pairs/Solution.py)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 27 | 93.88
345+
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
339346
| 0021 |[Merge Two Sorted Lists](src/main/python/g0001_0100/s0021_merge_two_sorted_lists/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 31 | 92.02
347+
| 0234 |[Palindrome Linked List](src/main/python/g0201_0300/s0234_palindrome_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 282 | 75.97
340348
| 0025 |[Reverse Nodes in k-Group](src/main/python/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.py)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 36 | 90.51
341349

342350
#### Udemy Tree Stack Queue
343351

344352
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
345353
|-|-|-|-|-|-
346354
| 0543 |[Diameter of Binary Tree](src/main/python/g0501_0600/s0543_diameter_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 51 | 36.84
355+
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 30 | 87.85
356+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 43 | 89.34
347357

348358
#### Udemy Trie and Heap
349359

350360
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
351361
|-|-|-|-|-|-
362+
| 0208 |[Implement Trie (Prefix Tree)](src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 140 | 28.87
352363

353364
#### Udemy Graph
354365

@@ -430,6 +441,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
430441

431442
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
432443
|-|-|-|-|-|-
444+
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
433445

434446
#### Day 9 Stack Queue
435447

@@ -451,6 +463,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
451463

452464
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
453465
|-|-|-|-|-|-
466+
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 30 | 87.85
454467

455468
#### Day 13 Tree
456469

@@ -492,6 +505,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
492505

493506
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
494507
|-|-|-|-|-|-
508+
| 0238 |[Product of Array Except Self](src/main/python/g0201_0300/s0238_product_of_array_except_self/Solution.py)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 241 | 99.65
495509
| 0560 |[Subarray Sum Equals K](src/main/python/g0501_0600/s0560_subarray_sum_equals_k/Solution.py)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Big_O_Time_O(n)_Space_O(n) | 224 | 72.29
496510

497511
#### Day 6 String
@@ -559,11 +573,13 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
559573

560574
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
561575
|-|-|-|-|-|-
576+
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 44 | 61.91
562577

563578
#### Day 18 Tree
564579

565580
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
566581
|-|-|-|-|-|-
582+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 43 | 89.34
567583

568584
#### Day 19 Graph
569585

@@ -574,6 +590,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
574590

575591
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
576592
|-|-|-|-|-|-
593+
| 0215 |[Kth Largest Element in an Array](src/main/python/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 446 | 79.92
577594
| 0347 |[Top K Frequent Elements](src/main/python/g0301_0400/s0347_top_k_frequent_elements/Solution.py)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n\*log(n))_Space_O(k) | 86 | 71.64
578595

579596
#### Day 21 Heap Priority Queue
@@ -637,6 +654,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
637654
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
638655
|-|-|-|-|-|-
639656
| 0021 |[Merge Two Sorted Lists](src/main/python/g0001_0100/s0021_merge_two_sorted_lists/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 31 | 92.02
657+
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
640658

641659
#### Day 11 Recursion Backtracking
642660

@@ -1041,6 +1059,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10411059
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10421060
|-|-|-|-|-|-
10431061
| 0064 |[Minimum Path Sum](src/main/python/g0001_0100/s0064_minimum_path_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 72 | 97.25
1062+
| 0221 |[Maximal Square](src/main/python/g0201_0300/s0221_maximal_square/Solution.py)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 510 | 82.54
10441063

10451064
#### Day 17
10461065

@@ -1338,6 +1357,16 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
13381357
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 113 | 97.70
13391358
| 0240 |[Search a 2D Matrix II](src/main/python/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 130 | 91.49
13401359
| 0239 |[Sliding Window Maximum](src/main/python/g0201_0300/s0239_sliding_window_maximum/Solution.py)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 1093 | 66.83
1360+
| 0238 |[Product of Array Except Self](src/main/python/g0201_0300/s0238_product_of_array_except_self/Solution.py)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 241 | 99.65
1361+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 43 | 89.34
1362+
| 0234 |[Palindrome Linked List](src/main/python/g0201_0300/s0234_palindrome_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 282 | 75.97
1363+
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 44 | 61.91
1364+
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 30 | 87.85
1365+
| 0221 |[Maximal Square](src/main/python/g0201_0300/s0221_maximal_square/Solution.py)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Big_O_Time_O(m\*n)_Space_O(m\*n) | 510 | 82.54
1366+
| 0215 |[Kth Largest Element in an Array](src/main/python/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 446 | 79.92
1367+
| 0208 |[Implement Trie (Prefix Tree)](src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 140 | 28.87
1368+
| 0207 |[Course Schedule](src/main/python/g0201_0300/s0207_course_schedule/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 86 | 76.64
1369+
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
13411370
| 0072 |[Edit Distance](src/main/python/g0001_0100/s0072_edit_distance/Solution.py)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 60 | 98.17
13421371
| 0070 |[Climbing Stairs](src/main/python/g0001_0100/s0070_climbing_stairs/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 29 | 87.37
13431372
| 0064 |[Minimum Path Sum](src/main/python/g0001_0100/s0064_minimum_path_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 72 | 97.25
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
2+
# #Data_Structure_I_Day_8_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking
3+
# #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
4+
# #2024_06_09_Time_35_ms_(72.56%)_Space_17.6_MB_(68.68%)
5+
6+
# Definition for singly-linked list.
7+
# class ListNode:
8+
# def __init__(self, val=0, next=None):
9+
# self.val = val
10+
# self.next = next
11+
class Solution:
12+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
13+
prev = None
14+
curr = head
15+
while curr is not None:
16+
next_node = curr.next
17+
curr.next = prev
18+
prev = curr
19+
curr = next_node
20+
return prev
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
206\. Reverse Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5]
12+
13+
**Output:** [5,4,3,2,1]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
18+
19+
**Input:** head = [1,2]
20+
21+
**Output:** [2,1]
22+
23+
**Example 3:**
24+
25+
**Input:** head = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is the range `[0, 5000]`.
32+
* `-5000 <= Node.val <= 5000`
33+
34+
**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search
2+
# #Breadth_First_Search #Graph #Topological_Sort #Big_O_Time_O(N)_Space_O(N)
3+
# #2024_06_09_Time_86_ms_(76.64%)_Space_18.4_MB_(48.61%)
4+
5+
class Solution:
6+
WHITE = 0
7+
GRAY = 1
8+
BLACK = 2
9+
10+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
11+
adj = [[] for _ in range(numCourses)]
12+
for pre in prerequisites:
13+
adj[pre[1]].append(pre[0])
14+
15+
colors = [self.WHITE] * numCourses
16+
17+
for i in range(numCourses):
18+
if colors[i] == self.WHITE and adj[i] and self.hasCycle(adj, i, colors):
19+
return False
20+
return True
21+
22+
def hasCycle(self, adj: List[List[int]], node: int, colors: List[int]) -> bool:
23+
colors[node] = self.GRAY
24+
25+
for nei in adj[node]:
26+
if colors[nei] == self.GRAY:
27+
return True
28+
if colors[nei] == self.WHITE and self.hasCycle(adj, nei, colors):
29+
return True
30+
31+
colors[node] = self.BLACK
32+
return False
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
207\. Course Schedule
2+
3+
Medium
4+
5+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.
6+
7+
* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
8+
9+
Return `true` if you can finish all courses. Otherwise, return `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** numCourses = 2, prerequisites = [[1,0]]
14+
15+
**Output:** true
16+
17+
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
18+
19+
**Example 2:**
20+
21+
**Input:** numCourses = 2, prerequisites = [[1,0],[0,1]]
22+
23+
**Output:** false
24+
25+
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= numCourses <= 10<sup>5</sup></code>
30+
* `0 <= prerequisites.length <= 5000`
31+
* `prerequisites[i].length == 2`
32+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
33+
* All the pairs prerequisites[i] are **unique**.

0 commit comments

Comments
 (0)