Skip to content

Commit 9377f2a

Browse files
committed
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
2 parents 968d494 + e5cc679 commit 9377f2a

Some content is hidden

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

90 files changed

+4025
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxPathSum = function(root) {
14+
let max = root.val;
15+
16+
const dfs = (node) => {
17+
if (!node) {
18+
return 0;
19+
}
20+
21+
const left = Math.max(dfs(node.left), 0);
22+
const right = Math.max(dfs(node.right), 0);
23+
const sum = node.val + left + right;
24+
25+
max = Math.max(sum, max);
26+
27+
return node.val + Math.max(left, right);
28+
}
29+
30+
dfs(root);
31+
32+
return max;
33+
};
34+
35+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) -> ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฏ€๋กœ ๋ณต์žก๋„๋Š” ๋…ธ๋“œ์˜ ์ˆ˜์™€ ๋น„๋ก€ํ•จ
36+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(h) -> ์ž…๋ ฅ๋œ ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๋†’์ด๋งŒํผ ์žฌ๊ท€ ์Šคํƒ์ด ์Œ“์ด๋ฏ€๋กœ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด์™€ ๊ฐ™์Œ
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @param {number} numCourses
3+
* @param {number[][]} prerequisites
4+
* @return {boolean}
5+
*/
6+
7+
// โœ… Graph DFS (Depth-First Search) approach
8+
// Time Complexity: O(V + E), where V is the number of courses (numCourses) and E is the number of prerequisites (edges).
9+
// Space Complexity: O(V + E), where V is the number of courses and E is the number of prerequisites.
10+
11+
var canFinish = function (numCourses, prerequisites) {
12+
// prerequisites = [
13+
// [1, 0], // Course 1 depends on Course 0
14+
// [2, 1], // Course 2 depends on Course 1
15+
// [3, 1], // Course 3 depends on Course 1
16+
// [3, 2] // Course 3 depends on Course 2
17+
// ];
18+
19+
// graph = {
20+
// 0: [1], // Course 0 is a prerequisite for Course 1
21+
// 1: [2, 3], // Course 1 is a prerequisite for Courses 2 and 3
22+
// 2: [3], // Course 2 is a prerequisite for Course 3
23+
// 3: [] // Course 3 has no prerequisites
24+
// };
25+
26+
// Build the graph
27+
const graph = {};
28+
29+
// for(let i=0; i<numCourses; i++) {
30+
// graph[i] = []
31+
// }
32+
33+
// Fill in the graph with prerequisites
34+
for (const [course, prereq] of prerequisites) {
35+
if (!graph[prereq]) {
36+
graph[prereq] = [];
37+
}
38+
graph[prereq].push(course);
39+
}
40+
41+
const visited = new Array(numCourses).fill(0);
42+
43+
const dfs = (course) => {
44+
if (visited[course] === 1) return false; // cycle detected!
45+
if (visited[course] === 2) return true; // already visited
46+
47+
visited[course] = 1;
48+
49+
// // Visit all the courses that depend on the current course
50+
for (const nextCourse of graph[course] || []) {
51+
if (!dfs(nextCourse)) {
52+
return false; // cycle detected!
53+
}
54+
}
55+
56+
visited[course] = 2; // fully visited and return true
57+
return true;
58+
};
59+
60+
// Check for each course whether it's possible to finish it
61+
for (let i = 0; i < numCourses; i++) {
62+
if (!dfs(i)) {
63+
return false; // cycle detected!
64+
}
65+
}
66+
67+
return true; // no cycles
68+
};
69+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Constraints:
3+
- 1 <= numCourses <= 2000
4+
- 0 <= prerequisites.length <= 5000
5+
- prerequisites[i].length == 2
6+
- 0 <= ai, bi < numCourses
7+
- All the pairs prerequisites[i] are unique.
8+
9+
Time Complexity: O(N + P)
10+
- N: numCourses, P: prerequisites์˜ ๊ธธ์ด
11+
12+
Space Complexity: O(N + P)
13+
- ์„ธํŠธ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด N๊ณผ ๋น„๋ก€ํ•˜๊ณ  ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ P
14+
- ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ O(N)
15+
16+
ํ’€์ด๋ฐฉ๋ฒ•:
17+
1. prerequisites์„ directed graph๋กœ ๋ณ€ํ™˜
18+
- ๊ฐ ์ฝ”์Šค๋ณ„๋กœ ์„ ์ˆ˜๊ณผ๋ชฉ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ €์žฅํ•จ
19+
2. DFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ cycle ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
20+
- visited ๋ฐฐ์—ด: ์ด๋ฏธ ํ™•์ธ์ด ์™„๋ฃŒ๋œ ๋…ธ๋“œ ์ฒดํฌ
21+
- path ๋ฐฐ์—ด: ํ˜„์žฌ DFS ๊ฒฝ๋กœ์—์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ์ฒดํฌ
22+
3. cycle์ด ๋ฐœ๊ฒฌ๋˜๋ฉด false, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด true ๋ฐ˜ํ™˜
23+
- ํ˜„์žฌ ๊ฒฝ๋กœ์—์„œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ๋งŒ๋‚˜๋ฉด cycle ์žˆ์Œ
24+
- ๋ชจ๋“  ๋…ธ๋“œ ๋ฐฉ๋ฌธ์ด ๊ฐ€๋Šฅํ•˜๋ฉด cycle ์—†์Œ
25+
"""
26+
class Solution:
27+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
28+
graph = [[] for _ in range(numCourses)]
29+
for course, prereq in prerequisites:
30+
graph[course].append(prereq)
31+
32+
visited = [False] * numCourses
33+
path = [False] * numCourses
34+
35+
def dfs(course):
36+
if path[course]:
37+
return False
38+
if visited[course]:
39+
return True
40+
41+
path[course] = True
42+
43+
for prereq in graph[course]:
44+
if not dfs(prereq):
45+
return False
46+
47+
path[course] = False
48+
visited[course] = True
49+
50+
return True
51+
52+
for course in range(numCourses):
53+
if not dfs(course):
54+
return False
55+
56+
return True
57+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* ์„ ์ˆ˜ ๊ณผ๋ชฉ๊ณผ ์ด์ˆ˜ ๊ณผ๋ชฉ ๊ด€๊ณ„๋ฅผ ํ™•์ธํ•˜์—ฌ ๋ชจ๋“  ๊ณผ๋ชฉ์„ ์ด์ˆ˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ.
4+
* @param {number} numCourses - ์ „์ฒด ๊ณผ๋ชฉ ์ˆ˜
5+
* @param {number[][]} prerequisites - ์„ ์ˆ˜ ๊ณผ๋ชฉ๊ณผ ์ด์ˆ˜ ๊ณผ๋ชฉ ๊ด€๊ณ„ ์ •๋ณด
6+
* @returns {boolean} - ๋ชจ๋“  ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
7+
*
8+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(V + E)
9+
* - ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์—์„œ O(E),
10+
* - bfs O(V + E),
11+
*
12+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(V + E)
13+
* - ๊ฐ ๊ณผ๋ชฉ์— ๋Œ€ํ•œ ์„ ์ˆ˜ ๊ณผ๋ชฉ ์ €์žฅ์— O(V + E),
14+
* - prereqCount ์ €์žฅ์— O(V),
15+
* - ํ(queue) ์ €์žฅ์— O(V),
16+
*/
17+
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
18+
// ๊ฐ ๊ณผ๋ชฉ์˜ ์„ ์ˆ˜ ๊ณผ๋ชฉ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ
19+
const graph: number[][] = Array.from({ length: numCourses }, () => []);
20+
// ๊ฐ ๊ณผ๋ชฉ์˜ ์„ ์ˆ˜ ๊ณผ๋ชฉ ์ˆ˜ ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
21+
const prereqCount: number[] = new Array(numCourses).fill(0);
22+
23+
// ๊ทธ๋ž˜ํ”„ ๋ฐ ์„ ์ˆ˜ ๊ณผ๋ชฉ ๊ฐœ์ˆ˜ ์ดˆ๊ธฐํ™”
24+
for (const [course, prereq] of prerequisites) {
25+
graph[prereq].push(course); // ์„ ์ˆ˜ ๊ณผ๋ชฉ์„ ๋“ค์–ด์•ผ ์ˆ˜๊ฐ• ๊ฐ€๋Šฅํ•œ ๊ณผ๋ชฉ ์ถ”๊ฐ€
26+
prereqCount[course]++; // ํ•ด๋‹น ๊ณผ๋ชฉ์˜ ์„ ์ˆ˜ ๊ณผ๋ชฉ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€
27+
}
28+
29+
// ์„ ์ˆ˜ ๊ณผ๋ชฉ์ด ์—†๋Š” ๊ณผ๋ชฉ๋“ค์„ ์ €์žฅํ•  ํ
30+
const queue: number[] = [];
31+
32+
// ์„ ์ˆ˜ ๊ณผ๋ชฉ์ด ์—†๋Š” ๊ณผ๋ชฉ๋“ค์„ ํ์— ์ถ”๊ฐ€ (์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ)
33+
for (let i = 0; i < numCourses; i++) {
34+
if (prereqCount[i] === 0) {
35+
queue.push(i);
36+
}
37+
}
38+
39+
// ์ˆ˜๊ฐ•ํ•œ ๊ณผ๋ชฉ ์ˆ˜
40+
let completedCourses = 0;
41+
42+
// ์„ ์ˆ˜ ๊ณผ๋ชฉ์ด ์—†๋Š” ๊ณผ๋ชฉ์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์Œ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•
43+
while (queue.length > 0) {
44+
const current = queue.shift()!;
45+
completedCourses++; // ๊ณผ๋ชฉ ์ˆ˜๊ฐ• ์™„๋ฃŒ
46+
47+
// ํ˜„์žฌ ๊ณผ๋ชฉ์„ ์„ ์ˆ˜ ๊ณผ๋ชฉ์œผ๋กœ ํ•˜๋Š” ๋‹ค๋ฅธ ๊ณผ๋ชฉ๋“ค์˜ ์„ ์ˆ˜ ๊ณผ๋ชฉ ๊ฐœ์ˆ˜ ๊ฐ์†Œ
48+
for (const nextCourse of graph[current]) {
49+
prereqCount[nextCourse]--;
50+
51+
// ์„ ์ˆ˜ ๊ณผ๋ชฉ ๊ฐœ์ˆ˜๊ฐ€ 0์ด ๋˜๋ฉด ํ์— ์ถ”๊ฐ€ (๋” ์ด์ƒ ๊ธฐ๋‹ค๋ฆด ํ•„์š” ์—†์Œ)
52+
if (prereqCount[nextCourse] === 0) {
53+
queue.push(nextCourse);
54+
}
55+
}
56+
}
57+
58+
// ๋ชจ๋“  ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ• ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
59+
return completedCourses === numCourses;
60+
}
61+

0 commit comments

Comments
ย (0)