Skip to content

Commit

Permalink
Merge pull request #76 from dev-jonghoonpark/main
Browse files Browse the repository at this point in the history
[박종훈] 3주차 답안 제출
  • Loading branch information
DaleSeo authored May 16, 2024
2 parents 0db7eba + 7856fe2 commit 9e028aa
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
26 changes: 26 additions & 0 deletions climbing-stairs/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- https://leetcode.com/problems/climbing-stairs
- time complexity : O(n)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/02/09/leetcode-70

```java
class Solution {
public int climbStairs(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 2;
}

int result = 0;
int temp1 = 1, temp2 = 2;
for(int i = 3; i <= n; i++) {
result = temp1 + temp2;
temp1 = temp2;
temp2 = result;
}

return result;
}
}
```
15 changes: 15 additions & 0 deletions maximum-depth-of-binary-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- https://leetcode.com/problems/maximum-depth-of-binary-tree
- time complexity : O(n)
- space complexity : O(n), 단 입력 트리가 balanced되어 있다면 O(logn)
- https://algorithm.jonghoonpark.com/2024/02/18/leetcode-104

```java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
```
19 changes: 19 additions & 0 deletions meeting-rooms/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- https://leetcode.com/problems/meeting-rooms
- https://neetcode.io/problems/meeting-schedule
- time complexity : O(nlogn)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/05/14/leetcode-252

```java
class Solution {
public boolean canAttendMeetings(List<Interval> intervals) {
intervals = intervals.stream().sorted(Comparator.comparingInt(o -> o.start)).toList();
for (int i = 0; i < intervals.size() - 1; i++) {
if(intervals.get(i).end > intervals.get(i+1).start) {
return false;
}
}
return true;
}
}
```
20 changes: 20 additions & 0 deletions same-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- https://leetcode.com/problems/same-tree
- time complexity : O(n)
- space complexity : O(n), 단 입력 트리가 balanced되어 있다면 O(logn)
- https://algorithm.jonghoonpark.com/2024/03/31/leetcode-100

```java
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}

if (p == null || q == null) {
return false;
}

return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
```
37 changes: 37 additions & 0 deletions subtree-of-another-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
- https://leetcode.com/problems/subtree-of-another-tree
- root tree 의 노드 수를 n, sub tree 의 노드 수를 m 이라고 하였을 때
- time complexity : O(n \* m)
- space complexity : O(n + m), 단 입력 트리가 balanced되어 있다면 O(logn + logm)
- https://algorithm.jonghoonpark.com/2024/05/14/leetcode-527

```java
public class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) {
return true;
}

if (root == null || subRoot == null) {
return false;
}

if (isSubtreeStrict(root, subRoot)) {
return true;
}

return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}

public boolean isSubtreeStrict(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) {
return true;
}

if (root == null || subRoot == null) {
return false;
}

return root.val == subRoot.val && isSubtreeStrict(root.left, subRoot.left) && isSubtreeStrict(root.right, subRoot.right);
}
}
```

0 comments on commit 9e028aa

Please sign in to comment.