Skip to content

[박다희] 3주차 제출합니다. #33

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions valid-parentheses/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Intuition
열린 괄호를 stack에 넣고 닫힌 괄호가 오면 stack에서 꺼낸 후 비교한다.

# Approach
```java
1. 문자 s 한글자씩 for문을 돌리며 확인한다.
2. 열린 괄호일 경우 stack에 넣는다.
3. 닫힌 괄호일 경우 stack이 비어있거나 stack에서 꺼낸 괄호가 짝이 맞는 괄호가 아니라면 false를 반환한다.
4. for문을 다 돌았을 때 stack에 괄호가 남아있으면 false, 비어있으면 true를 반환한다.
```

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(n)

# Code
```java
class Solution {
public boolean isValid(String s) {
String[] brackets = s.split("");
Stack<String> stack = new Stack<>();

for (String bracket: brackets) {
if (isOpenBracket(bracket)) {
stack.push(bracket);
} else {
if (stack.isEmpty() || !isSameBracket(stack.pop(), bracket)) {
return false;
}
}
}

if (!stack.isEmpty()) {
return false;
}
return true;
}

public boolean isOpenBracket(String bracket) {
return bracket.equals("(") || bracket.equals("{") || bracket.equals("[");
}

public boolean isSameBracket(String openBracket, String closeBracket) {
return (openBracket.equals("(")&&closeBracket.equals(")")) || (openBracket.equals("{")&&closeBracket.equals("}")) || (openBracket.equals("[")&&closeBracket.equals("]"));
}
}
```
37 changes: 37 additions & 0 deletions week-03/climbing-stairs/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[# Intuition
n-1일 경우와 n-2일 경우의 수를 더한 값을 반환한다.

# Approach
```java
1. n이 1이면 1을 반환한다.
2. 배열의 index 1에는 1을, index 2에는 2를 저장한다. (= 방법의 수 저장)
3. 3부터 n이 될 때까지 for문을 돌린다.
3-1. i개일 때 배열에 i-1과 i-2의 방법의 수를 더한 값을 저장한다.
4. 배열의 index가 n일 때 값을 반환한다.
```

# Complexity
- Time complexity: O(n)

- Space complexity: O(n)

# Code
```java
class Solution {
public int climbStairs(int n) {
Copy link
Contributor

Choose a reason for hiding this comment

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

dp로 해결하셨군요 👍

int[] dp = new int[n+1];

if (n == 1) {
return 1;
}
dp[1] = 1;
dp[2] = 2;

for (int i=3; i<=n; i++) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
}
```
]()
47 changes: 47 additions & 0 deletions week-03/maximum-depth-of-binary-tree/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Intuition
재귀함수로 모든 노드를 가보면서 가장 깊은 depth를 찾는다.

# Approach
```java
1. count를 0부터하고 root node부터 재귀한다.
2. node가 null이면 count를 반환한다.
3. node가 null이 아니면 왼쪽 노드와 count+1을 재귀하고, 오른쪽 노드와 count+1을 재귀한다.
4. 두 개의 결과 중 더 큰 값을 반환한다.
```

# Complexity
- Time complexity: O(n)

- Space complexity: O(n)

# Code
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

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

public int countDepth(TreeNode node, int count) {
if (node == null) {
return count;
}
return Math.max(countDepth(node.left, count+1), countDepth(node.right, count+1));
Copy link
Contributor

Choose a reason for hiding this comment

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

👍👍

}
}
```
40 changes: 40 additions & 0 deletions week-03/meeting-rooms/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Intuition
start부터 end까지 체크해 놓고, 체크한 곳을 또 오면 false를 반환한다.

# Approach
```java
1. 시간에 따른 방문 여부를 기록할 배열을 만든다.
2. 시작과 끝을 모두 방문한적 있다면 false를 반환한다.
3. 시작시간부터 끝시간까지 for문을 돌린다.
3-1. 방문한적이 있고, i가 시작시간이나 끝시간이 아니면 false를 반환한다.
3-2. 방문한적이 없으면 true를 기록한다.
4. true를 반환한다.
```

# Complexity
- Time complexity: O(n^2)

- Space complexity: O(n)

# Code
```java
class Solution {
public int climbStairs(int n) {
boolean[] check = new boolean[1000001];
for (Interval interval : intervals) {
Copy link
Contributor

Choose a reason for hiding this comment

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

intervals가 뭐죠!?

int start = interval.start;
int end = interval.end;
if (check[start] && check[end]) {
return false;
}
for (int i=start; i<=end; i++) {
if (check[i] && (i != start && i != end)) {
return false;
}
check[i] = true;
}
}
return true;
}
}
```
50 changes: 50 additions & 0 deletions week-03/same-tree/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Intuition
q와 p의 모든 노드를 동시에 보면서 값을 비교한다.

# Approach
```java
1. p와 q 둘 중에 한 개만 null일 경우 false를 반환한다.
2. p와 q 모두 null일 경우 true를 반환한다.
3. p와 q의 val이 다를 경우 false를 반환한다.
4. p와 q의 left node, right node 각각을 재귀하여 둘 다 반환값이 true이면 true를 반환한다.
```

# Complexity
- Time complexity: O(n)

- Space complexity: O(n)


# Code
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if ((p == null && q != null) || (p != null && q == null)) {
return false;
}
if (p == null && q == null) {
return true;
}

if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
```
66 changes: 66 additions & 0 deletions week-03/subtree-of-another-tree/daheeParkk.java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Intuition
root node를 재귀하면서 subRoot node의 val과 값이 같으면 동일한 tree인지 확인한다.

# Approach
```java
1. root나 subRoot가 null이면 false를 반환한다.
2. root와 subRoot가 null이면 true를 반환한다.
3. root의 val과 subRoot의 val이 같으면 해당 노드부터 같은 트리인지 확인한다.
4. val이 다르면 왼쪽 노드와 subRoot 또는 오른쪽 노드와 subRoot를 비교해 true가 있으면 true를 반환한다.
```

# Complexity
- Time complexity: O(n^2)

- Space complexity: O(n)

# Code
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if ((root != null && subRoot == null) || (root == null && subRoot != null)) {
return false;
}
if (root == null && subRoot == null) {
return true;
}

if (root.val == subRoot.val) {
if(checkSame(root, subRoot)) {
return true;
}
}
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}

public boolean checkSame(TreeNode root, TreeNode subRoot) {
if ((root != null && subRoot == null) || (root == null && subRoot != null)) {
return false;
}
if (root == null && subRoot == null) {
return true;
}

if (root.val != subRoot.val) {
return false;
}

return checkSame(root.left, subRoot.left) && checkSame(root.right, subRoot.right);
}
}
```