-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
20 changed files
with
860 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// TC: O(n + p) | ||
// n -> number of courses, p -> the length of prerequisites | ||
// SC: O(n + m) | ||
// n -> the length of the graph size, m -> the length of nested list's size | ||
class Solution { | ||
public boolean canFinish(int numCourses, int[][] prerequisites) { | ||
List<List<Integer>> graph = new ArrayList<>(); | ||
int[] inDegree = new int[numCourses]; | ||
|
||
for (int i = 0; i < numCourses; i++) graph.add(new ArrayList<>()); | ||
|
||
for (int[] prerequisite : prerequisites) { | ||
int course = prerequisite[0]; | ||
int pre = prerequisite[1]; | ||
graph.get(pre).add(course); | ||
inDegree[course] += 1; | ||
} | ||
|
||
Queue<Integer> q = new LinkedList<>(); | ||
for (int i = 0; i < numCourses; i++) { | ||
if (inDegree[i] == 0) q.offer(i); | ||
} | ||
|
||
int visitedCourses = 0; | ||
while (!q.isEmpty()) { | ||
int course = q.poll(); | ||
visitedCourses += 1; | ||
|
||
for (int nextCourse : graph.get(course)) { | ||
inDegree[nextCourse] -= 1; | ||
if (inDegree[nextCourse] == 0) q.offer(nextCourse); | ||
} | ||
} | ||
|
||
return visitedCourses == numCourses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""TC: O(node + edge), SC: O(node + edge) | ||
์ ๋ช ํ ์์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ์ค๋ช ์ ์๋ตํ๋ค. | ||
""" | ||
|
||
|
||
class Solution: | ||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
# ์์ ์ ๋ ฌ. | ||
|
||
# init | ||
adj_list = [[] for _ in range(numCourses)] # SC: O(edge) | ||
in_deg = [0] * numCourses # SC: O(node) | ||
|
||
for edge in prerequisites: | ||
adj_list[edge[0]].append(edge[1]) | ||
in_deg[edge[1]] += 1 | ||
|
||
node_to_search = [i for i, v in enumerate(in_deg) if v == 0] # TC: O(node) | ||
sorted_list = [] | ||
|
||
# process | ||
while node_to_search: | ||
cur = node_to_search.pop() # TC: ์ต์ ์ ๊ฒฝ์ฐ ์ด O(node)๋งํผ ์คํ | ||
sorted_list.append(cur) | ||
for node in adj_list[cur]: | ||
in_deg[node] -= 1 # TC: ์ต์ ์ ๊ฒฝ์ฐ ์ด O(edge)๋งํผ ์คํ | ||
if in_deg[node] == 0: | ||
node_to_search.append(node) | ||
|
||
return len(sorted_list) == numCourses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `course-schedule` { | ||
|
||
/** | ||
* TC: O(node + edge), SC: O(node + edge) | ||
*/ | ||
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean { | ||
if (prerequisites.isEmpty()) return true | ||
|
||
return usingTopologySort(numCourses, prerequisites) | ||
} | ||
|
||
private fun usingTopologySort(numCourses: Int, prerequisites: Array<IntArray>): Boolean { | ||
val adj = List(numCourses) { mutableListOf<Int>() } | ||
val degree = IntArray(numCourses) | ||
for (e in prerequisites) { | ||
val (course, pre) = e[0] to e[1] | ||
adj[pre].add(course) | ||
degree[course]++ | ||
} | ||
|
||
val queue = ArrayDeque<Int>().apply { | ||
degree.forEachIndexed { index, i -> | ||
if (i == 0) { | ||
this.add(index) | ||
} | ||
} | ||
} | ||
|
||
var answer = 0 | ||
while (queue.isNotEmpty()) { | ||
val now = queue.removeFirst() | ||
answer++ | ||
|
||
queue.addAll(adj[now].filter { --degree[it] == 0 }) | ||
} | ||
|
||
return answer == numCourses | ||
} | ||
|
||
@Test | ||
fun `์ฝ์ค์ ๊ฐ์์ ์ฝ์ค ๊ฐ ์์กด์ฑ์ ์ ๋ฌํ๋ฉด ์ฝ์ค๋ฅผ ์๋ฃํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ๋ฐํํ๋ค`() { | ||
canFinish(5, | ||
arrayOf( | ||
intArrayOf(0,1), | ||
intArrayOf(0,2), | ||
intArrayOf(1,3), | ||
intArrayOf(1,4), | ||
intArrayOf(3,4) | ||
) | ||
) shouldBe true | ||
canFinish(5, | ||
arrayOf( | ||
intArrayOf(1,4), | ||
intArrayOf(2,4), | ||
intArrayOf(3,1), | ||
intArrayOf(3,2) | ||
) | ||
) shouldBe true | ||
canFinish(2, arrayOf(intArrayOf(1, 0))) shouldBe true | ||
canFinish(2, arrayOf(intArrayOf(1, 0), intArrayOf(0, 1))) shouldBe false | ||
canFinish(20, | ||
arrayOf( | ||
intArrayOf(0,10), | ||
intArrayOf(3,18), | ||
intArrayOf(5,5), | ||
intArrayOf(6,11), | ||
intArrayOf(11,14), | ||
intArrayOf(13,1), | ||
intArrayOf(15,1), | ||
intArrayOf(17,4) | ||
) | ||
) shouldBe false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @description | ||
* memoization + dfs | ||
* | ||
* n = length of nums | ||
* p = length of prerequisites | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(p) | ||
*/ | ||
var canFinish = function (numCourses, prerequisites) { | ||
const memo = Array.from({ length: numCourses + 1 }, () => false); | ||
const visited = Array.from({ length: numCourses + 1 }, () => false); | ||
// graph setting | ||
const graph = prerequisites.reduce((map, [linkedNode, current]) => { | ||
const list = map.get(current) ?? []; | ||
list.push(linkedNode); | ||
map.set(current, list); | ||
return map; | ||
}, new Map()); | ||
|
||
const dfs = (current) => { | ||
const linkedNode = graph.get(current); | ||
|
||
if (memo[current] || !linkedNode || linkedNode.length === 0) return true; | ||
|
||
for (const node of linkedNode) { | ||
if (visited[node]) return false; | ||
|
||
visited[node] = true; | ||
if (!dfs(node)) return false; | ||
visited[node] = false; | ||
memo[node] = true; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
for (const [current] of graph) { | ||
visited[current] = true; | ||
if (!dfs(current)) return false; | ||
visited[current] = false; | ||
memo[current] = true; | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// TC: O(n) | ||
// -> visit all nodes to invert | ||
// SC: O(n) | ||
// -> create all nodes again to exchange | ||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) return null; | ||
invertTree(root.left); | ||
invertTree(root.right); | ||
TreeNode left = root.left; | ||
TreeNode right = root.right; | ||
root.left = right; | ||
root.right = left; | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""TC: O(n), SC: O(h) | ||
h๋ ์ด์ง ํธ๋ฆฌ์ ๋์ด. | ||
n์ด ์ ์ฒด ๋ ธ๋ ๊ฐ์๋ผ๊ณ ํ ๋ | ||
- ์ต์ ์ ๊ฒฝ์ฐ ํ ์ชฝ ์์ ๋ ธ๋๋ง ์ฑ์์ง. ์ด ๊ฒฝ์ฐ h = n. | ||
- ์ต์ ์ ๊ฒฝ์ฐ ์์ ์ด์ง ํธ๋ฆฌ. h = log(n). | ||
์์ด๋์ด: | ||
์์ชฝ ์์ ๋ ธ๋์ ์ ๊ทผํด์ ์ฌ๊ท์ ์ผ๋ก invert๋ฅผ ์งํํ๊ณ , ๋ ์์ ๋ ธ๋๋ฅผ ๋ฐ๊พผ๋ค. | ||
SC: | ||
- ํธ์ถ ์คํ ๊น์ด๋ ํธ๋ฆฌ์ ๊น์ด๊น์ง ๊น์ด์ง ์ ์๋ค. ์ฆ, O(h). | ||
TC: | ||
- ๋ชจ๋ ๋ ธ๋์ ์ ๊ทผ. O(n). | ||
""" | ||
|
||
|
||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
def invert(node: Optional[TreeNode]) -> None: | ||
if node is not None: | ||
node.left, node.right = invert(node.right), invert(node.left) | ||
return node | ||
|
||
return invert(root) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `invert-binary-tree` { | ||
|
||
fun invertTree(root: TreeNode?): TreeNode? { | ||
if (root == null) return null | ||
|
||
return usingStack(root) | ||
} | ||
|
||
/** | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
private fun usingDFS(node: TreeNode?): TreeNode? { | ||
if (node == null) return null | ||
|
||
val (left, right) = node.left to node.right | ||
node.left = usingDFS(right) | ||
node.right = usingDFS(left) | ||
|
||
return node | ||
} | ||
|
||
/** | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
private fun usingStack(node: TreeNode): TreeNode { | ||
val stack= ArrayDeque<TreeNode>().apply { | ||
this.add(node) | ||
} | ||
|
||
while (stack.isNotEmpty()) { | ||
val now = stack.removeLast() | ||
val tmp = now.left | ||
now.left = now.right | ||
now.right = tmp | ||
|
||
now.left?.let { stack.add(it) } | ||
now.right?.let { stack.add(it) } | ||
} | ||
return node | ||
} | ||
|
||
@Test | ||
fun `์ ๋ฌ๋ ๋ ธ๋์ ํ์ ๋ ธ๋๋ค์ ๋ฐ์ ๋ ๊ฐ์ ๋ฐํํ๋ค`() { | ||
val actual = TreeNode.of(4,2,7,1,3,6,9) | ||
val expect = TreeNode.of(4,7,2,9,6,3,1) | ||
invertTree(actual) shouldBe expect | ||
|
||
val actual1 = TreeNode.of(1,2) | ||
val expect1 = TreeNode.of(1,null,2) | ||
|
||
invertTree(actual1) shouldBe expect1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* preorder traverse | ||
* | ||
* n = length of root | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var invertTree = function (root) { | ||
const preOrder = (tree) => { | ||
if (tree === null) return null; | ||
|
||
const currentNode = new TreeNode(tree.val); | ||
|
||
currentNode.right = preOrder(tree.left); | ||
currentNode.left = preOrder(tree.right); | ||
|
||
return currentNode; | ||
}; | ||
|
||
return preOrder(root); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public boolean canJump(int[] nums) { | ||
int jump = 0; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (i > jump) return false; | ||
jump = Math.max(jump, i + nums[i]); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""TC: O(n), SC: O(1) | ||
n์ ์ฃผ์ด์ง ๋ฆฌ์คํธ์ ๊ธธ์ด | ||
์์ด๋์ด: | ||
- ๋์ ์๋ ์์ดํ ๋ถํฐ ์์ํด์ '์ต์ ์ด๋๊น์ง๋ ๋๋ฌํด์ผ ๋ ์นธ๊น์ง ์ ํ ๊ฐ๋ฅํ์ง'๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค. | ||
- example๋ค๋ก ์ดํดํด๋ณด์. index๋ 0๋ถํฐ ์์. | ||
- example 1: [2,3,1,1,4] | ||
- 4๋ฒ์งธ ์นธ์ ๋๋ฌํ ์ ์์ผ๋ฉด ์ฑ๊ณต์ด๋ค. reach_at_least ๊ฐ์ 4๋ก ์ด๊ธฐํ ํ๋ค. | ||
- 3๋ฒ์งธ ์นธ์์๋ ์ต๋ 4๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฆ, ์ ์ด๋ 3๋ฒ ์นธ๊น์ง ๊ฐ๋ฉด ์ฑ๊ณต์ด๋ฏ๋ก | ||
reach_at_least๋ฅผ 3์ผ๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 2๋ฒ์งธ ์นธ์์๋ ์ต๋ 3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. reach_at_least๋ฅผ 2๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 1๋ฒ์งธ ์นธ์์๋ ์ต๋ 1+3=4๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ด ์นธ์์ ํ reach_at_least ๊ฐ์ธ 2๋ฒ์งธ ์นธ๊น์ง | ||
์ถฉ๋ถํ ๊ฐ ์ ์์ผ๋ฏ๋ก reach_at_least ๊ฐ์ 1๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 0๋ฒ์งธ ์นธ์์๋ ์ต๋ 0+2=2๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ํ reach_at_least ๊ฐ์ธ 1๋ฒ์งธ ์นธ๊น์ง ์ถฉ๋ถํ | ||
๊ฐ ์ ์์ผ๋ฏ๋ก reach_at_least ๊ฐ์ 0์ผ๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 0๋ฒ์งธ ์นธ์์ ๋ ์นธ๊น์ง ๊ฐ ์ ์๋ค. | ||
- example 2: [3,2,1,0,4] | ||
- 4๋ฒ์งธ ์นธ์ ๋๋ฌํ ์ ์์ผ๋ฉด ์ฑ๊ณต์ด๋ค. reach_at_least ๊ฐ์ 4๋ก ์ด๊ธฐํ ํ๋ค. | ||
- 3๋ฒ์งธ ์นธ์์๋ ์ต๋ 3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฌ๊ธฐ์๋ ํ reach_at_least ๊ฐ์ธ 4๊น์ง ๊ฐ ์ ์์ผ๋ | ||
์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 2๋ฒ์งธ ์นธ์์๋ ์ต๋ 2+1=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฌ๊ธฐ์๋ ํ reach_at_least ๊ฐ์ธ 4๊น์ง ๊ฐ ์ ์๊ณ , | ||
์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 1๋ฒ์งธ ์นธ์์๋ ์ต๋ 1+2=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ๋น์ทํ๊ฒ ์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 0๋ฒ์งธ ์นธ์์๋ ์ต๋ 0+3=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ๋น์ทํ๊ฒ ์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- reach_at_least ๊ฐ์ด 0์ด ์๋๋ค. ์ฆ, 0๋ฒ์งธ ์นธ์์๋ ๋ ์นธ๊น์ง ๊ฐ ์ ์๋ค. | ||
SC: | ||
- reach_at_least ๊ฐ์ ์ธ๋ฑ์ค ํ๋๋ง ๊ด๋ฆฌํ๋ค. ์ฆ, O(1). | ||
TC: | ||
- nums์ ๋์์ ๋ ๋ฒ์งธ ์์ดํ ๋ถํฐ ์ฒซ ๋ฒ์งธ ์์ดํ ๊น์ง ์์ฐจ์ ์ผ๋ก ์ ๊ทผํ๋ฉด์ reach_at_least๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ค. O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def canJump(self, nums: List[int]) -> bool: | ||
reach_at_least = len(nums) - 1 | ||
|
||
for i in range(len(nums) - 2, -1, -1): | ||
if nums[i] + i >= reach_at_least: | ||
reach_at_least = i | ||
|
||
return reach_at_least == 0 |
Oops, something went wrong.