forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
884ebec
commit 6f3a0ea
Showing
5 changed files
with
163 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,27 @@ | ||
// dfs์ ๋ถํ ์ ๋ณต๋ฒ์ผ๋ก ํด๊ฒฐ๋๋ ๋ฌธ์ | ||
// dfs์ธ ๊ฒ์ ์์์ผ๋ ๋ชจ๋ ์ซ์๊ฐ ์์์ผ ๊ฒฝ์ฐ๋ฅผ ํ๋ณํ์ง ๋ชปํด GPT์๊ฒ ๋์์ ์ฒํจ | ||
class Solution { | ||
private int maxSum = Integer.MIN_VALUE; // ์ ์ฒด ์ต๋ ๊ฒฝ๋ก ํฉ ์ ์ฅ | ||
|
||
public int maxPathSum(TreeNode root) { | ||
dfs(root); | ||
return maxSum; | ||
} | ||
|
||
private int dfs(TreeNode node) { | ||
if (node == null) return 0; // base case | ||
|
||
// ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ์ต๋ ๊ฒฝ๋ก ํฉ (์์๋ ํฌํจ X โ Math.max(0, value)) | ||
int left = Math.max(0, dfs(node.left)); | ||
int right = Math.max(0, dfs(node.right)); | ||
|
||
// ํ์ฌ ๋ ธ๋๋ฅผ ํฌํจํ ์ต๋ ๊ฒฝ๋ก (์ผ์ชฝ + ๋ฃจํธ + ์ค๋ฅธ์ชฝ) | ||
int pathSum = left + node.val + right; | ||
|
||
// ์ต๋ ๊ฒฝ๋ก ๊ฐ ๊ฐฑ์ | ||
maxSum = Math.max(maxSum, pathSum); | ||
|
||
// โ ํ์ฌ ๋ ธ๋๋ฅผ ํฌํจํ๋ ์ต๋ ๊ฒฝ๋ก ๊ฐ๋ง ๋ฐํํด์ผ ํจ (์ฐ๊ฒฐ ๊ฐ๋ฅํ ๊ฒฝ๋ก) | ||
return node.val + Math.max(left, right); | ||
} | ||
} |
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,34 @@ | ||
// ์ ๋ฒ์ฃผ course-schedule ๋ฌธ์ ์ ์ ์ฌํ ์ฌ์ดํด์ด ์กด์ฌํ์ง ์์์ผํ๋ ํธ๋ฆฌ๋ฅผ ์ฐพ๋ ๋ฌธ์ | ||
// ์ฐจ์ด๊ฐ ์๋ค๋ฉด course-schedule๋ ๋ฐฉํฅ ๊ทธ๋ํ์ง๋ง ์ด ๋ฌธ์ ๋ ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ๋ผ๋ ์ฐจ์ด๊ฐ ์์ | ||
// ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ๋ ์ด์ ๋ ธ๋(๋ถ๋ชจ)๋ฅผ ํตํด ๋ค์ ๋์์จ ํ ์ฐ๊ฒฐ์ ํ๋จํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด๊ฑฐ ๊ตฌํ์ด ์กฐ๊ธ ๋ ์ด๋ ค์ | ||
public class GraphValidTree { | ||
public boolean validTree(int n, int[][] edges) { | ||
if (edges.length != n - 1) return false; // ํธ๋ฆฌ๋ ๋ฐ๋์ (n-1)๊ฐ์ ๊ฐ์ ํ์ | ||
|
||
// ๊ทธ๋ํ ์ธ์ ๋ฆฌ์คํธ ์์ฑ | ||
Map<Integer, List<Integer>> graph = new HashMap<>(); | ||
for (int i = 0; i < n; i++) { | ||
graph.put(i, new ArrayList<>()); | ||
} | ||
for (int[] edge : edges) { | ||
graph.get(edge[0]).add(edge[1]); | ||
graph.get(edge[1]).add(edge[0]); | ||
} | ||
|
||
Set<Integer> visited = new HashSet<>(); | ||
if (!dfs(graph, 0, -1, visited)) return false; | ||
|
||
return visited.size() == n; | ||
} | ||
|
||
private boolean dfs(Map<Integer, List<Integer>> graph, int node, int parent, Set<Integer> visited) { | ||
if (visited.contains(node)) return false; // ์ฌ์ดํด ๋ฐ๊ฒฌ | ||
|
||
visited.add(node); | ||
for (int neighbor : graph.get(node)) { | ||
if (neighbor == parent) continue; | ||
if (!dfs(graph, neighbor, node, visited)) return false; | ||
} | ||
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,8 @@ | ||
// ํธ๋ฆฌ ํ์์ ๊น์ด ์ฐ์ ํ์์ด ๊ณต๊ฐ๋ณต์ก๋๊ฐ ๋ฎ๋ค. | ||
// ๋ชจ๋ ๋ ธ๋(๋ฆฌ์คํธ)๋ฅผ O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ๋๋ค. | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; | ||
} | ||
} |
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 @@ | ||
// ์ ๋ ฌ์ ํ์ง ์์ผ๋ ค๋ฉด O(N^2)์ด ๊ฑธ๋ฆผ | ||
// ์ ๋ ฌ์ ๋๋ฉด ์๋ฌด๋ฆฌ ๋นจ๋ผ๋ O(N log N)์ด์ง๋ง ์ดํ์ ๋ฌธ์ ๊ฐ ๊ฐ๋จํด์ง | ||
// currentEnd >= nextStart โ ๊ฒน์น๋ฉด end ๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ ๊ฐ๋จํ ๋ฐฉ์์ผ๋ก ๊ตฌํ | ||
class Solution { | ||
public int[][] merge(int[][] intervals) { | ||
if (intervals.length <= 1) return intervals; | ||
|
||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
|
||
List<int[]> merged = new ArrayList<>(); | ||
int[] currentInterval = intervals[0]; | ||
merged.add(currentInterval); | ||
|
||
for (int[] interval : intervals) { | ||
int currentEnd = currentInterval[1]; | ||
int nextStart = interval[0]; | ||
int nextEnd = interval[1]; | ||
|
||
if (currentEnd >= nextStart) { | ||
// ๊ฒน์น๋ฉด end ๊ฐ์ ์ ๋ฐ์ดํธ | ||
currentInterval[1] = Math.max(currentEnd, nextEnd); | ||
} else { | ||
// ๊ฒน์น์ง ์์ผ๋ฉด ์๋ก์ด ๊ตฌ๊ฐ์ผ๋ก ์ถ๊ฐ | ||
currentInterval = interval; | ||
merged.add(currentInterval); | ||
} | ||
} | ||
|
||
return merged.toArray(new int[merged.size()][]); | ||
} | ||
} |
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,63 @@ | ||
// GPT์ ๋์์ ๋ฐ์ ์กฐ๊ธ ๋ ๊ณต๊ฐ๋ณต์ก๋๊ฐ ๋ฎ๊ณ ๋น ๋ฅธ ๋ฐฉ์์ผ๋ก ๊ตฌํ | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
if (head == null || head.next == null) return; | ||
|
||
ListNode slow = head, fast = head; | ||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
ListNode second = reverse(slow.next); | ||
slow.next = null; | ||
|
||
ListNode first = head; | ||
while (second != null) { | ||
ListNode tmp1 = first.next; | ||
ListNode tmp2 = second.next; | ||
|
||
first.next = second; | ||
second.next = tmp1; | ||
|
||
first = tmp1; | ||
second = tmp2; | ||
} | ||
} | ||
|
||
private ListNode reverse(ListNode head) { | ||
ListNode prev = null; | ||
while (head != null) { | ||
ListNode next = head.next; | ||
head.next = prev; | ||
prev = head; | ||
head = next; | ||
} | ||
return prev; | ||
} | ||
} | ||
|
||
// ์ฒซ๋ฒ์งธ ์๊ฐํ ํ์ด๋ฐฉ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ ๊ฐ์ ๋ฃ์ด์ ์ฒ๋ฆฌํ๋ ์๊ฐ๋ณต์ก๋๋ O(N)์ผ๋ก ์ด๋ก ์ ๊ฐ์ผ๋ | ||
// ์๋๋ผ ํ๊ท ์๋๋ณด๋ค ํ์ ํ๊ฒ ๋ฎ๊ฒ๋์จ๋ค. | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
if (head == null || head.next == null) return; | ||
|
||
List<ListNode> list = new ArrayList<>(); | ||
ListNode temp = head; | ||
while (temp != null) { | ||
list.add(temp); | ||
temp = temp.next; | ||
} | ||
|
||
int i = 0, j = list.size() - 1; | ||
while (i < j) { | ||
list.get(i).next = list.get(j); | ||
i++; | ||
if (i == j) break; | ||
list.get(j).next = list.get(i); | ||
j--; | ||
} | ||
list.get(i).next = null; | ||
} | ||
} |