Skip to content

Commit 477d47c

Browse files
committed
2 parents d3ef7fd + 727fb27 commit 477d47c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
// ์ตœ๋Œ€ Path ๋ˆ„์  ํ•ฉ
19+
int maxPathSumVal = Integer.MIN_VALUE;
20+
21+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(h) h as height of tree
22+
public int maxPathSum(TreeNode root) {
23+
maxPathSumChecker(root);
24+
return maxPathSumVal;
25+
}
26+
27+
// ์žฌ๊ท€
28+
private int maxPathSumChecker(TreeNode node) {
29+
30+
if (node == null) {
31+
return 0;
32+
}
33+
34+
int leftMax = Math.max(maxPathSumChecker(node.left), 0);
35+
int rightMax = Math.max(maxPathSumChecker(node.right), 0);
36+
37+
maxPathSumVal = Math.max(node.val + leftMax + rightMax, maxPathSumVal);
38+
39+
return node.val + Math.max(leftMax, rightMax);
40+
}
41+
}
42+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Solution {
2+
/**
3+
* @param n: An integer
4+
* @param edges: a list of undirected edges
5+
* @return: true if it's a valid tree, or false
6+
*/
7+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
8+
public boolean validTree(int n, int[][] edges) {
9+
10+
if (edges.length != (n - 1)) {
11+
return false;
12+
}
13+
14+
List<Integer>[] graph = new ArrayList[n];
15+
for (int i = 0; i < n; i++) {
16+
graph[i] = new ArrayList<>();
17+
}
18+
19+
for (int[] edge : edges) {
20+
graph[edge[0]].add(edge[1]);
21+
graph[edge[1]].add(edge[0]);
22+
}
23+
24+
boolean[] visited = new boolean[n];
25+
26+
if (!dfs(0, -1, visited, graph)) {
27+
return false;
28+
}
29+
30+
for (boolean v : visited) {
31+
if (!v) {
32+
return false; // Not fully connected
33+
}
34+
}
35+
36+
return true;
37+
}
38+
39+
private boolean dfs(int node, int parent, boolean[] visited, List<Integer>[] graph) {
40+
if (visited[node]) {
41+
return false; // Found a cycle
42+
}
43+
44+
visited[node] = true;
45+
46+
for (int neighbor : graph[node]) {
47+
if (neighbor != parent) {
48+
if (!dfs(neighbor, node, visited, graph)) {
49+
return false;
50+
}
51+
}
52+
}
53+
54+
return true;
55+
}
56+
}
57+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
4+
if (intervals.length <= 1) {
5+
return intervals;
6+
}
7+
8+
List<int[]> answer = new ArrayList<>();
9+
10+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
11+
12+
int start = intervals[0][0];
13+
int end = intervals[0][1];
14+
15+
for (int i = 1; i < intervals.length; i++) {
16+
17+
// ํ˜„์žฌ ์ธํ„ฐ๋ฒŒ ์‹œ์ž‘์ 
18+
int currStart = intervals[i][0];
19+
// ํ˜„์žฌ ์ธํ„ฐ๋ฒŒ ๋์ 
20+
int currEnd = intervals[i][1];
21+
22+
if (end >= currStart) {
23+
end = Math.max(end, currEnd);
24+
} else {
25+
answer.add(new int[]{start, end});
26+
start = currStart;
27+
end = currEnd;
28+
}
29+
}
30+
31+
answer.add(new int[]{start, end});
32+
33+
return answer.stream()
34+
.toArray(int[][]::new);
35+
}
36+
}
37+

0 commit comments

Comments
ย (0)