-
-
Notifications
You must be signed in to change notification settings - Fork 244
[njngwn] Week 13 Solutions #1947
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,37 @@ | ||||||
/** | ||||||
* 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 { | ||||||
int cnt = 0; | ||||||
int result = 0; | ||||||
|
||||||
public int kthSmallest(TreeNode root, int k) { | ||||||
cnt = k; | ||||||
traverseInOrder(root); | ||||||
return result; | ||||||
} | ||||||
|
||||||
public void traverseInOrder (TreeNode node) { | ||||||
if (node == null) return; | ||||||
|
||||||
if (node.left != null) traverseInOrder(node.left); | ||||||
cnt--; | ||||||
if (cnt == 0) { | ||||||
result = node.val; | ||||||
return; | ||||||
} | ||||||
if (node.right != null) traverseInOrder(node.right); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 하면 가지치기 가능할 것 같아요!
Suggested change
|
||||||
} | ||||||
} |
23 changes: 23 additions & 0 deletions
23
lowest-common-ancestor-of-a-binary-search-tree/njngwn.java
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,23 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
|
||
class Solution { | ||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
if (root.val == p.val || root.val == q.val) return root; | ||
|
||
if (p.val < root.val && q.val < root.val) { | ||
return lowestCommonAncestor(root.left, p, q); | ||
} else if (p.val > root.val && q.val > root.val) { | ||
return lowestCommonAncestor(root.right, p, q); | ||
} else { | ||
return root; | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정렬 후 바로 전,후 시간만 간단하게 비교하는 부분이 너무 잘 구현된것 같아요! |
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,30 @@ | ||
/** | ||
* Definition of Interval: | ||
* public class Interval { | ||
* int start, end; | ||
* Interval(int start, int end) { | ||
* this.start = start; | ||
* this.end = end; | ||
* } | ||
* } | ||
*/ | ||
|
||
public class Solution { | ||
/** | ||
* @param intervals: an array of meeting time intervals | ||
* @return: if a person could attend all meetings | ||
*/ | ||
public boolean canAttendMeetings(List<Interval> intervals) { | ||
// Write your code here | ||
intervals.sort((a, b) -> a.start - b.start); // sort by start time | ||
|
||
int prevEndTime = 0; | ||
for (Interval meeting : intervals) { | ||
if (meeting.start < prevEndTime) { | ||
return false; | ||
} | ||
prevEndTime = meeting.end; | ||
} | ||
return true; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다음에 제안한 조기 종료를 위해 Sentinel Value로 변경합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 그렇군요!! 하나 배워갑니다! 피드백 감사드려요☺️ ☺️