-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from dev-jonghoonpark/main
[박종훈] 3주차 답안 제출
- Loading branch information
Showing
5 changed files
with
117 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,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; | ||
} | ||
} | ||
``` |
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,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)); | ||
} | ||
} | ||
``` |
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,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; | ||
} | ||
} | ||
``` |
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,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); | ||
} | ||
} | ||
``` |
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,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); | ||
} | ||
} | ||
``` |