Skip to content

[bky373] Solutions for week 2 problems #50

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

Merged
merged 5 commits into from
May 11, 2024
Merged
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
16 changes: 16 additions & 0 deletions invert-binary-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* - 문제: https://leetcode.com/problems/linked-list-cycle/
* - TC: O(N)
* - SC: O(N)
*/
public class Solution_226 {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode tmp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(tmp);
return root;
}
}
22 changes: 22 additions & 0 deletions linked-list-cycle/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* - 문제: https://leetcode.com/problems/linked-list-cycle/
* - TC: O(N)
* - SC: O(1)
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
int min = -10001;

while (head.next != null) {
if (head.next.val == min) {
return true;
}
head.val = min;
head = head.next;
}
Comment on lines +11 to +19
Copy link
Member

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.

네네 저걸로 방문 기록을 대신 했습니다 ㅋㅋ 다른 분들 풀이 보니 어떻게 저렇게 생각하셨을까.. 감탄했습니다

return false;
}
}
33 changes: 33 additions & 0 deletions merge-two-sorted-lists/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* TC: O(N)
* SC: O(N)
*/
class Solution_21 {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null && list2 == null) {
return null;
}

List<Integer> nums = new ArrayList<>();

while (list1 != null) {
nums.add(list1.val);
list1 = list1.next;
}
while (list2 != null) {
nums.add(list2.val);
list2 = list2.next;
}

Object[] arr = nums.toArray();
Arrays.sort(arr);
Copy link
Member

Choose a reason for hiding this comment

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

여기서 정렬을 하는데... 시간 복잡도가 O(N)이 될까요? 🤔

Copy link
Contributor Author

@bky373 bky373 May 11, 2024

Choose a reason for hiding this comment

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

아 제 풀이에서는 처음에 모든 node 를 돌아야 하기 때문에 O(N) 으로 작성하였습니다
찾아보니 Arrays.sort() 는 평균 시간 복잡도가 O(nlog(n)) 이고, 최악의 경우 O(n^2) 까지도 나오는군요..
현재 풀이가 만족스러운 풀이는 아니어서 한 번 더 풀게 되면 좀더 개선해보려 합니다!


ListNode head = new ListNode((int) arr[0]);
ListNode curr = head;
for (int i=1; i<arr.length; i++) {
curr.next = new ListNode((int) arr[i]);
curr = curr.next;
}
return head;
}
}
21 changes: 21 additions & 0 deletions reverse-linked-list/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* https://leetcode.com/problems/reverse-linked-list/
* TC: O(N)
* SC: O(N)
*/
class Solution_206 {

public ListNode reverseList(ListNode head) {
if (head == null) {
return head;
}
ListNode a = new ListNode(head.val);
ListNode b;
while (head.next != null) {
b = new ListNode(head.next.val, a);
a = b;
head = head.next;
}
return a;
}
}
29 changes: 29 additions & 0 deletions valid-parentheses/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* - 문제: https://leetcode.com/problems/valid-parentheses/
* - TC: O(N)
* - SC: O(1)
*/
class Solution_20 {

public boolean isValid(String s) {
Stack<Character> st = new Stack<>();
Map<Character, Character> pairs = Map.of('(', ')', '{', '}', '[', ']');

for (int i = 0; i < s.length(); i++) {
// 열린 괄호이면 스택에 push
if (pairs.containsKey(s.charAt(i))) {
st.push(s.charAt(i));
} else { // 닫힌 괄호인데
if (st.isEmpty()) { // 스택이 비어있으면(열린 괄호가 없으면) false 반환
return false;
}
// 스택의 최근 요소(열린 괄호)와 짝을 이루지 않으면 false 반환
if (s.charAt(i) != pairs.get(st.pop())) {
return false;
}
}
}
// 열린 괄호가 남아 있을 수 있으므로 목록이 비어있는지 체크
return st.isEmpty();
}
}