Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bky373] Week 3 Solutions #75

Merged
merged 7 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions climbing-stairs/bky373.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열에 값들을 담아줄 수도 있지만, 현재 값을 구하기 위해서는 전과 전의 전 값만 필요한 것을 고려하면 공간복잡도를 상수항으로 만들 수도 있을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 피드백이네요 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와.. 이런 접근이 가능했군요! 문제 새로 풀어두었습니다 감사합니다!

[week3] improvement for 70. Climbing Stairs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

져도 처음에는 list에 쌓았는데 피드백 받아서 고쳤습니다ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {

public int climbStairs(int n) {
int[] ans = new int[n + 1];
if (n <= 2) {
return n;
}
ans[1] = 1;
ans[2] = 2;

for (int i = 3; i < n + 1; i++) {
ans[i] = ans[i - 1] + ans[i - 2];
}
return ans[n];
}
}
/**
* TC: O(N)
* SC: O(N)
*/
17 changes: 17 additions & 0 deletions maximum-depth-of-binary-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* TC: O(N)
* SC: O(N)
*/
class Solution {

public int maxDepth(TreeNode root) {
return findMax(root, 0);
}

public int findMax(TreeNode node, int depth) {
if (node == null) {
return depth;
}
return Math.max(findMax(node.left, depth + 1), findMax(node.right, depth + 1));
}
}
19 changes: 19 additions & 0 deletions meeting-rooms/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {

public boolean canAttendMeetings(int[][] intervals) {
if (intervals.length == 0) {
return true;
}
Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
for (int i = 0; i < intervals.length - 1; i++) {
if (intervals[i][1] > intervals[i + 1][0]) {
return false;
}
}
return true;
}
}
/**
* TC: O(nlogn), due to the sorting array.
* SC: O(1)
*/
11 changes: 11 additions & 0 deletions same-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* TC: O(N)
* SC: O(N)
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null || p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
25 changes: 25 additions & 0 deletions subtree-of-another-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* TC: O(M * N) - M: 메인 트리(root)의 노드 수, N: 서브 트리(subtree)의 노드 수
* SC: O(N) - N: 메인 트리(root)의 높이
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved
*/
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) {
return false;
}
if (isSameTree(root, subRoot)) {
return true;
}
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}

public boolean isSameTree(TreeNode n1, TreeNode n2) {
if (n1 == null || n2 == null) {
return n1 == n2;
}
if (n1.val != n2.val) {
return false;
}
return isSameTree(n1.left, n2.left) && isSameTree(n1.right, n2.right);
}
}