From 7856fe22a4f4fbf14dfa0a3d905bd47152755563 Mon Sep 17 00:00:00 2001 From: jonghoonpark Date: Thu, 16 May 2024 12:53:49 +0900 Subject: [PATCH] week3 mission done --- climbing-stairs/dev-jonghoonpark.md | 26 +++++++++++++ .../dev-jonghoonpark.md | 15 ++++++++ meeting-rooms/dev-jonghoonpark.md | 19 ++++++++++ same-tree/dev-jonghoonpark.md | 20 ++++++++++ subtree-of-another-tree/dev-jonghoonpark.md | 37 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 climbing-stairs/dev-jonghoonpark.md create mode 100644 maximum-depth-of-binary-tree/dev-jonghoonpark.md create mode 100644 meeting-rooms/dev-jonghoonpark.md create mode 100644 same-tree/dev-jonghoonpark.md create mode 100644 subtree-of-another-tree/dev-jonghoonpark.md diff --git a/climbing-stairs/dev-jonghoonpark.md b/climbing-stairs/dev-jonghoonpark.md new file mode 100644 index 000000000..521ccbc29 --- /dev/null +++ b/climbing-stairs/dev-jonghoonpark.md @@ -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; + } +} +``` diff --git a/maximum-depth-of-binary-tree/dev-jonghoonpark.md b/maximum-depth-of-binary-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..7e1fe314c --- /dev/null +++ b/maximum-depth-of-binary-tree/dev-jonghoonpark.md @@ -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)); + } +} +``` diff --git a/meeting-rooms/dev-jonghoonpark.md b/meeting-rooms/dev-jonghoonpark.md new file mode 100644 index 000000000..e66bb1387 --- /dev/null +++ b/meeting-rooms/dev-jonghoonpark.md @@ -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 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; + } +} +``` diff --git a/same-tree/dev-jonghoonpark.md b/same-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..85c115997 --- /dev/null +++ b/same-tree/dev-jonghoonpark.md @@ -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); + } +} +``` diff --git a/subtree-of-another-tree/dev-jonghoonpark.md b/subtree-of-another-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..05e7f8ee2 --- /dev/null +++ b/subtree-of-another-tree/dev-jonghoonpark.md @@ -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); + } +} +```