Skip to content

Commit 9e028aa

Browse files
authored
Merge pull request #76 from dev-jonghoonpark/main
[๋ฐ•์ข…ํ›ˆ] 3์ฃผ์ฐจ ๋‹ต์•ˆ ์ œ์ถœ
2 parents 0db7eba + 7856fe2 commit 9e028aa

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- https://leetcode.com/problems/climbing-stairs
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- https://algorithm.jonghoonpark.com/2024/02/09/leetcode-70
5+
6+
```java
7+
class Solution {
8+
public int climbStairs(int n) {
9+
if (n == 1) {
10+
return 1;
11+
} else if (n == 2) {
12+
return 2;
13+
}
14+
15+
int result = 0;
16+
int temp1 = 1, temp2 = 2;
17+
for(int i = 3; i <= n; i++) {
18+
result = temp1 + temp2;
19+
temp1 = temp2;
20+
temp2 = result;
21+
}
22+
23+
return result;
24+
}
25+
}
26+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- https://leetcode.com/problems/maximum-depth-of-binary-tree
2+
- time complexity : O(n)
3+
- space complexity : O(n), ๋‹จ ์ž…๋ ฅ ํŠธ๋ฆฌ๊ฐ€ balanced๋˜์–ด ์žˆ๋‹ค๋ฉด O(logn)
4+
- https://algorithm.jonghoonpark.com/2024/02/18/leetcode-104
5+
6+
```java
7+
class Solution {
8+
public int maxDepth(TreeNode root) {
9+
if (root == null) {
10+
return 0;
11+
}
12+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
13+
}
14+
}
15+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- https://leetcode.com/problems/meeting-rooms
2+
- https://neetcode.io/problems/meeting-schedule
3+
- time complexity : O(nlogn)
4+
- space complexity : O(n)
5+
- https://algorithm.jonghoonpark.com/2024/05/14/leetcode-252
6+
7+
```java
8+
class Solution {
9+
public boolean canAttendMeetings(List<Interval> intervals) {
10+
intervals = intervals.stream().sorted(Comparator.comparingInt(o -> o.start)).toList();
11+
for (int i = 0; i < intervals.size() - 1; i++) {
12+
if(intervals.get(i).end > intervals.get(i+1).start) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}
19+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- https://leetcode.com/problems/same-tree
2+
- time complexity : O(n)
3+
- space complexity : O(n), ๋‹จ ์ž…๋ ฅ ํŠธ๋ฆฌ๊ฐ€ balanced๋˜์–ด ์žˆ๋‹ค๋ฉด O(logn)
4+
- https://algorithm.jonghoonpark.com/2024/03/31/leetcode-100
5+
6+
```java
7+
public class Solution {
8+
public boolean isSameTree(TreeNode p, TreeNode q) {
9+
if (p == null && q == null) {
10+
return true;
11+
}
12+
13+
if (p == null || q == null) {
14+
return false;
15+
}
16+
17+
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
18+
}
19+
}
20+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- https://leetcode.com/problems/subtree-of-another-tree
2+
- root tree ์˜ ๋…ธ๋“œ ์ˆ˜๋ฅผ n, sub tree ์˜ ๋…ธ๋“œ ์ˆ˜๋ฅผ m ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ
3+
- time complexity : O(n \* m)
4+
- space complexity : O(n + m), ๋‹จ ์ž…๋ ฅ ํŠธ๋ฆฌ๊ฐ€ balanced๋˜์–ด ์žˆ๋‹ค๋ฉด O(logn + logm)
5+
- https://algorithm.jonghoonpark.com/2024/05/14/leetcode-527
6+
7+
```java
8+
public class Solution {
9+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
10+
if (root == null && subRoot == null) {
11+
return true;
12+
}
13+
14+
if (root == null || subRoot == null) {
15+
return false;
16+
}
17+
18+
if (isSubtreeStrict(root, subRoot)) {
19+
return true;
20+
}
21+
22+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
23+
}
24+
25+
public boolean isSubtreeStrict(TreeNode root, TreeNode subRoot) {
26+
if (root == null && subRoot == null) {
27+
return true;
28+
}
29+
30+
if (root == null || subRoot == null) {
31+
return false;
32+
}
33+
34+
return root.val == subRoot.val && isSubtreeStrict(root.left, subRoot.left) && isSubtreeStrict(root.right, subRoot.right);
35+
}
36+
}
37+
```

0 commit comments

Comments
ย (0)