Skip to content

Commit 0db7eba

Browse files
authored
Merge pull request DaleStudy#75 from bky373/main
[bky373] Week 3 Solutions
2 parents 4b7905f + 9b5e3ab commit 0db7eba

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

β€Žclimbing-stairs/bky373.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
3+
public int climbStairs(int n) {
4+
if (n == 1) {
5+
return n;
6+
}
7+
int a1 = 1;
8+
int a2 = 2;
9+
for (int i = 3; i < n + 1; i++) {
10+
int a3 = a1 + a2;
11+
a1 = a2;
12+
a2 = a3;
13+
}
14+
return a2;
15+
}
16+
}
17+
/**
18+
* TC: O(N)
19+
* SC: O(1)
20+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(N)
4+
*/
5+
class Solution {
6+
7+
public int maxDepth(TreeNode root) {
8+
return findMax(root, 0);
9+
}
10+
11+
public int findMax(TreeNode node, int depth) {
12+
if (node == null) {
13+
return depth;
14+
}
15+
return Math.max(findMax(node.left, depth + 1), findMax(node.right, depth + 1));
16+
}
17+
}

β€Žmeeting-rooms/bky373.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
3+
public boolean canAttendMeetings(int[][] intervals) {
4+
if (intervals.length == 0) {
5+
return true;
6+
}
7+
Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
8+
for (int i = 0; i < intervals.length - 1; i++) {
9+
if (intervals[i][1] > intervals[i + 1][0]) {
10+
return false;
11+
}
12+
}
13+
return true;
14+
}
15+
}
16+
/**
17+
* TC: O(nlogn), due to the sorting array.
18+
* SC: O(1)
19+
*/

β€Žsame-tree/bky373.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(N)
4+
*/
5+
class Solution {
6+
public boolean isSameTree(TreeNode p, TreeNode q) {
7+
if (p == null && q == null) return true;
8+
if (p == null || q == null || p.val != q.val) return false;
9+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* TC: O(M * N) - N: 메인 트리(root)의 λ…Έλ“œ 수, M: μ„œλΈŒ 트리(subtree)의 λ…Έλ“œ 수
3+
* SC: O(M + N)
4+
* - isSubtree() 콜 μŠ€νƒμ€ μ΅œλŒ€ N 개 λ°œμƒν•  수 있고,
5+
* - 각각 isSameTree() 콜 μŠ€νƒ(M 개)을 λ”ν•œ κ°’κΉŒμ§€ λŠ˜μ–΄λ‚  수 μžˆλ‹€.
6+
* = isSameTree() 콜 μŠ€νƒμ€ 호좜 이후 μ‚¬λΌμ§€λ―€λ‘œ, 곡간 λ³΅μž‘λ„λŠ” μ΅œλŒ€ M + N 이닀.
7+
*/
8+
class Solution {
9+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
10+
if (root == null) {
11+
return false;
12+
}
13+
if (isSameTree(root, subRoot)) {
14+
return true;
15+
}
16+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
17+
}
18+
19+
public boolean isSameTree(TreeNode n1, TreeNode n2) {
20+
if (n1 == null || n2 == null) {
21+
return n1 == n2;
22+
}
23+
if (n1.val != n2.val) {
24+
return false;
25+
}
26+
return isSameTree(n1.left, n2.left) && isSameTree(n1.right, n2.right);
27+
}
28+
}

0 commit comments

Comments
Β (0)