-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1027 from imsosleepy/main
[Ackku] week 10
- Loading branch information
Showing
5 changed files
with
135 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,40 @@ | ||
// ์ฌ์ดํด์ ์ฐพ๋ ๋ฌธ์ . DFS๋ฅผ ์ด์ฉํด์ ๋ฐฉ๋ฌธํ๋์ง ์ฌ๋ถ๋ฅผ ๊ด๋ฆฌํ๊ณ ์ฒดํฌํ๋ฉด๋๋ค. | ||
// DFS๋ฅผ ์งํ ์ค์ธ ๊ฒ์ ๋ค๊ณ ์์ด์ผ ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ํ๋จํ ์ ์๋ค. | ||
// ๊ฐ์ ์์ ๋ ธ๋์ ์์ ๋ฐ๋ผ ์๊ฐ ๋ณต์ก๋๊ฐ ๊ฒฐ์ ๋จ O(N+L) | ||
class Solution { | ||
public boolean canFinish(int numCourses, int[][] prerequisites) { | ||
List<List<Integer>> graph = new ArrayList<>(); | ||
for (int i = 0; i < numCourses; i++) { | ||
graph.add(new ArrayList<>()); | ||
} | ||
|
||
for (int[] pair : prerequisites) { | ||
graph.get(pair[1]).add(pair[0]); | ||
} | ||
|
||
// 0: ๋ฐฉ๋ฌธ X | ||
// -1: DFS ์งํ ์ค | ||
// 1: ๋ฐฉ๋ฌธ ์๋ฃ | ||
int[] visited = new int[numCourses]; | ||
|
||
// ๋ชจ๋ ๋ ธ๋์์ DFS ์ํ | ||
for (int i = 0; i < numCourses; i++) { | ||
if (dfs(graph, visited, i)) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private boolean dfs(List<List<Integer>> graph, int[] visited, int node) { | ||
if (visited[node] == -1) return true; // ๋ฐฉ๋ฌธ ์ค์ด๋ฉด ์ฌ์ดํด์ด ๋ฐ๊ฒฌ | ||
if (visited[node] == 1) return false; | ||
|
||
visited[node] = -1; // ์งํ ์ค ํ๊ธฐ | ||
for (int next : graph.get(node)) { | ||
if (dfs(graph, visited, next)) return true; | ||
} | ||
visited[node] = 1; | ||
|
||
return 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,13 @@ | ||
// ํธ๋ฆฌ์ ํํ๋ฅผ ๋ฐ๊พธ๋๊ฑด ๋๋ถ๋ถ DFS๋ก ํธ๋๊ฒ ๊ณต๊ฐ๋ณต์ก๋ ์ธก๋ฉด์์ ์ ๋ฆฌํ๋ค. | ||
// BFS๋ก๋ ํ์ด ๊ฐ๋ฅํ์ง๋ง, BFS๋ ๋๋ถ๋ถ ํ๋ฅผ ์ด์ฉํด์ ๊ณต๊ฐ๋ณต์ก๋๊ฐ ๋์์ง๋ค. | ||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) return null; | ||
|
||
TreeNode temp = root.left; | ||
root.left = invertTree(root.right); | ||
root.right = invertTree(temp); | ||
|
||
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,20 @@ | ||
// ํ์ฌ ์์น์์ ์ต๋๋ก ๊ฐ ์ ์๋ ์ธ๋ฑ์ค์ ์ด๋ ฅ์ ๊ฐฑ์ ํ๋ฉด์ ์ต๋ ๋ฒ์๋ฅผ ๋์ง ์์๋๋ฅผ ๊ณ์ ํ๋ณํ๋ฉด ๋จ | ||
// ์ฒ์์ DP์ธ์ค ์์๋๋ฐ DP ๋ฐฐ์ด ์์ด ๊ทธ๋ฅ ํ๋ฆผ | ||
class Solution { | ||
public boolean canJump(int[] nums) { | ||
int maxReach = 0; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (i > maxReach) { | ||
return false; | ||
} | ||
|
||
maxReach = Math.max(maxReach, i + nums[i]); | ||
if (maxReach >= nums.length - 1) { | ||
return 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,27 @@ | ||
// ์ฐ์ ์์ ํ๋ก ๋ค ํฉ์ณ๋ฒ๋ฆฐ ๋ค์์ | ||
// ํ๋ฅผ ์ํ๋๋ฉด์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ฌ์์ฑํ๋ค. ์ฑ๋ฅ์ ๋น๊ต์ ๋ฎ๊ฒ ์กํ | ||
// ๋ถํ ์ ๋ณต๋ฒ์ผ๋ก ํ๋ฉด ๋ ์ข์ ์ฑ๋ฅ์ด ๋์จ๋ค๊ณ ํ๋ค. | ||
class Solution { | ||
public ListNode mergeKLists(ListNode[] lists) { | ||
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val); | ||
|
||
for (ListNode node : lists) { | ||
if (node != null) pq.offer(node); | ||
} | ||
|
||
ListNode newLists = new ListNode(-1); | ||
ListNode curr = newLists; | ||
|
||
while (!pq.isEmpty()) { | ||
ListNode minNode = pq.poll(); | ||
curr.next = minNode; | ||
curr = curr.next; | ||
|
||
if (minNode.next != null) { | ||
pq.offer(minNode.next); | ||
} | ||
} | ||
|
||
return newLists.next; | ||
} | ||
} |
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,35 @@ | ||
// O(log n)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๋ฌธ์ ์์ ์๊ตฌํ๊ณ ์๋ค. | ||
// O(log n)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ๋ ํ์ ๋ฌธ์ ๋ ๋๋ถ๋ถ ๋ฐ์ด๋๋ฆฌ ์์น๋ก ํด๊ฒฐ๋๋ค. | ||
// ์ฃผ์ด์ง ๋ฐฐ์ด์ด ์ ๋ ฌ๋์ด์๋ค๋ ์ ์์ ์ด์งํ์์ ์ด์ฉํ๋ฉด ๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค. | ||
class Solution { | ||
public int search(int[] nums, int target) { | ||
int left = 0, right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if (nums[mid] == target) { | ||
return mid; | ||
} | ||
|
||
// ์ผ์ชฝ์ด ์ ๋ ฌ๋ ๊ฒฝ์ฐ | ||
if (nums[left] <= nums[mid]) { | ||
if (nums[left] <= target && target < nums[mid]) { | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
// ์ค๋ฅธ์ชฝ์ด ์ ๋ ฌ๋ ๊ฒฝ์ฐ | ||
else { | ||
if (nums[mid] < target && target <= nums[right]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |