-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK 03] 추슬기 #9
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfba5cc
feat: ImplementQueueUsingStacks 문제 풀이 작성
doitchuu 85ecdcf
feat: LinkedListCycle 문제 풀이 작성
doitchuu a7b8580
feat: LowestCommonAncestorOfABinarySearchTree 풀이 추가
doitchuu 3049bf7
add: BalancedBinaryTree 문제 추가
doitchuu d1d0c40
feat: BalancedBinaryTree 문제 풀이 작성
doitchuu 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,29 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @return {boolean} | ||
| */ | ||
| var isBalanced = function(root) { | ||
| function dfs(node) { | ||
| if (node === null) return 0; | ||
|
|
||
| const left = dfs(node.left); | ||
| if (left === -1) return -1; | ||
|
|
||
| const right = dfs(node.right); | ||
| if (right === -1) return -1; | ||
|
|
||
| if (Math.abs(left - right) > 1) return -1; | ||
|
|
||
| return Math.max(left, right) + 1; | ||
| } | ||
|
|
||
| return dfs(root) !== -1; | ||
| }; |
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,58 @@ | ||
| var MyQueue = function () { | ||
| this.inStack = []; | ||
| this.outStack = []; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {number} x | ||
| * @return {void} | ||
| */ | ||
| MyQueue.prototype.push = function (x) { | ||
| this.inStack.push(x); | ||
| }; | ||
|
|
||
| /** | ||
| * @return {number} | ||
| */ | ||
| MyQueue.prototype.pop = function () { | ||
| if (this.outStack.length === 0) { | ||
| while (this.inStack.length) { | ||
| this.outStack.push(this.inStack.pop()); | ||
| } | ||
| } | ||
|
|
||
| return this.outStack.pop(); | ||
| }; | ||
|
|
||
| /** | ||
| * @return {number} | ||
| */ | ||
| MyQueue.prototype.peek = function () { | ||
| if (this.outStack.length === 0) { | ||
| while (this.inStack.length) { | ||
| this.outStack.push(this.inStack.pop()); | ||
| } | ||
| } | ||
|
|
||
| const lastIndex = this.outStack.length - 1; | ||
| return this.outStack[lastIndex]; | ||
| }; | ||
|
|
||
| /** | ||
| * @return {boolean} | ||
| */ | ||
| MyQueue.prototype.empty = function () { | ||
| const hasInStackValues = this.inStack.length !== 0; | ||
| const hasOutStackValues = this.outStack.length !== 0; | ||
|
|
||
| return !hasInStackValues && !hasOutStackValues ? true : false; | ||
| }; | ||
|
|
||
| /** | ||
| * Your MyQueue object will be instantiated and called as such: | ||
| * var obj = new MyQueue() | ||
| * obj.push(x) | ||
| * var param_2 = obj.pop() | ||
| * var param_3 = obj.peek() | ||
| * var param_4 = obj.empty() | ||
| */ | ||
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,27 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val) { | ||
| * this.val = val; | ||
| * this.next = null; | ||
| * } | ||
| */ | ||
|
|
||
| /** | ||
| * @param {ListNode} head | ||
| * @return {boolean} | ||
| */ | ||
| var hasCycle = function (head) { | ||
| const isVisited = new Set(); | ||
| let current = head; | ||
|
|
||
| while (current) { | ||
| if (isVisited.has(current)) { | ||
| return true; | ||
| } | ||
|
|
||
| isVisited.add(current); | ||
| current = current.next; | ||
| } | ||
|
|
||
| return false; | ||
| }; |
|
Collaborator
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 for a binary tree node. | ||
| * function TreeNode(val) { | ||
| * this.val = val; | ||
| * this.left = this.right = null; | ||
| * } | ||
| */ | ||
|
|
||
| /** | ||
| * @param {TreeNode} root | ||
| * @param {TreeNode} p | ||
| * @param {TreeNode} q | ||
| * @return {TreeNode} | ||
| */ | ||
| var lowestCommonAncestor = function(root, p, q) { | ||
| const small = Math.min(p.val, q.val); | ||
| const big = Math.max(p.val, q.val); | ||
|
|
||
| while (root) { | ||
| if (big < root.val) { | ||
| root = root.left; | ||
| } else if (small > root.val) { | ||
| root = root.right; | ||
| } else { | ||
| return root; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }; |
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.
pop과 peek에서 중복으로 사용되는 로직 분리해도 좋을것 같군용! 다른건 완벽!!