-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jaejeong1] WEEK 12 Solutions #573
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
class Solution { | ||
|
||
public int[][] merge(int[][] intervals) { | ||
// TC: O(N log N) | ||
// SC: O(N) | ||
|
||
// length가 2보다 적으면 그대로 반환 | ||
if (intervals.length < 2) { | ||
return intervals; | ||
} | ||
|
||
List<int[]> output = new ArrayList<>(); | ||
|
||
// intervals 배열을 시작 시간 기준으로 정렬 | ||
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0])); | ||
|
||
for (int[] interval : intervals) { | ||
// output이 비어있거나, 현재 interval이 마지막에 추가된 구간과 겹치지 않으면 추가 | ||
if (output.isEmpty() || output.get(output.size() - 1)[1] < interval[0]) { | ||
output.add(interval); | ||
} else { | ||
// 겹치는 경우, 마지막 구간의 끝 시간을 업데이트 | ||
output.get(output.size() - 1)[1] = Math.max(output.get(output.size() - 1)[1], interval[1]); | ||
} | ||
} | ||
|
||
// List<int[]>를 int[][] 배열로 변환하여 반환 | ||
return output.toArray(new int[output.size()][]); | ||
} | ||
} |
This file contains hidden or 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,43 @@ | ||
import java.util.LinkedList; | ||
|
||
// Definition for singly-linked list. | ||
class ListNode { | ||
int val; | ||
ListNode next; | ||
ListNode() {} | ||
ListNode(int val) { this.val = val; } | ||
ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
} | ||
|
||
class Solution { | ||
|
||
public static void main(String[] args) { | ||
Solution s = new Solution(); | ||
var node = new ListNode(1, new ListNode(2)); | ||
s.removeNthFromEnd(node, 1); | ||
} | ||
|
||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
// 문제: 링크드리스트의 head가 주어지면 끝에서 N번째 노드를 삭제한 결과를 반환하라 | ||
// 풀이: n번째만큼 first 이동, 그 후 second를 1칸씩 함께 이동 시킨다 first가 끝에 도달할 때 까지 | ||
// 전체 길이 L, 주어진 N이 있을때 이렇게 하면 L - N - 1 위치를 구할 수 있다. | ||
// TC: O(N) | ||
// SC: O(1) | ||
var dummy = new ListNode(-1, head); | ||
var first = head; | ||
for (int i=0; i<n; i++) { | ||
first = first.next; | ||
} | ||
|
||
var second = dummy; | ||
|
||
while(first != null) { | ||
first = first.next; | ||
second = second.next; | ||
} | ||
|
||
second.next = second.next.next; | ||
return dummy.next; | ||
} | ||
|
||
} |
This file contains hidden or 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,36 @@ | ||
|
||
// Definition for a binary tree node. | ||
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) { | ||
// 풀이: 재귀로 left와 right를 비교하면서 같은지 확인한다. | ||
// TC: O(N) | ||
// SC: O(N) | ||
return dfs(p, q); | ||
} | ||
|
||
private boolean dfs(TreeNode p, TreeNode q) { | ||
if (p == null || q == null) { | ||
return p == q; // 둘 다 null이면 true, 하나만 null이면 false | ||
} | ||
|
||
if (p.val != q.val) { // 값이 다르면 false | ||
return false; | ||
} | ||
|
||
return dfs(p.left, q.left) && dfs(p.right, q.right); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.