-
Notifications
You must be signed in to change notification settings - Fork 126
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
[선재] Week13 #579
Merged
Merged
[선재] Week13 #579
Changes from all commits
Commits
Show all changes
3 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 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,22 @@ | ||
/** | ||
* @description | ||
* 최대한 많은 양의 돈이라는 문구에서 dynamic programming을 연상 | ||
* 연속된 집은 털 수 없다라는 문구에서 점화식을 도출 할 수 있었음 | ||
* | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 1) return nums[0]; | ||
|
||
const dp = Array(nums.length).fill(0); | ||
|
||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[1], dp[0]); | ||
|
||
for (let i = 2; i < nums.length; i++) | ||
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); | ||
|
||
return dp[nums.length - 1]; | ||
}; |
31 changes: 31 additions & 0 deletions
31
lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js
This file contains 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,31 @@ | ||
/** | ||
* @description | ||
* bfs, dfs와 같은 순회 방법과 treeNode 구조에 child가 아닌 parent라는 속성을 부여해 부모찾기를 아이디어로 접근 | ||
* 하지만 모든 노드를 순회해야하고 p와 q가 속한지점과 둘이 포함하는 관계인지를 중점으로 문제에 접근함 | ||
* 그 결과 postOrder를 생각하게 되어 문제 풀이 | ||
* | ||
* n = length of total treeNode | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
let answer = null; | ||
|
||
const postOrder = (tree) => { | ||
if (tree === null) return [false, false]; | ||
|
||
const [hasLeftP, hasLeftQ] = postOrder(tree.left); | ||
const [hasRightP, hasRightQ] = postOrder(tree.right); | ||
|
||
const hasP = hasLeftP || hasRightP || tree.val === p.val; | ||
const hasQ = hasLeftQ || hasRightQ || tree.val === q.val; | ||
|
||
if (hasP && hasQ && answer === null) answer = tree; | ||
|
||
return [hasP, hasQ]; | ||
}; | ||
|
||
postOrder(root); | ||
|
||
return answer; | ||
}; |
This file contains 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,32 @@ | ||
/** | ||
* @description | ||
* overlapping이 안되기위한 기준이 필요함을 느낌 | ||
* 처음에는 시작점, 끝점을 기준으로 정렬했지만 16번 테스트에서 실패 | ||
* 정렬기준이 끝점, 시작점 순으로 정렬해야한다고 깨닫게 됨 | ||
* | ||
* n = length of intervals | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
*/ | ||
var eraseOverlapIntervals = function (intervals) { | ||
intervals.sort((a, b) => { | ||
if (a[1] !== b[1]) return a[1] - b[1]; | ||
if (a[0] !== b[0]) return b[0] - a[0]; | ||
return 0; | ||
}); | ||
|
||
let answer = 0; | ||
const current = intervals[0]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
if (current[1] > start) answer++; | ||
else { | ||
current[0] = start; | ||
current[1] = end; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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.
정렬의 기준은 결국 뒤에 값이 최종 범위가 되기 때문에 끝점만 비교 하셔도 괜찮을 거에요 :)
시작점을 같이 비교한것도 좋다고 생각합니다!