Skip to content
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 3 commits into from
Nov 10, 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
22 changes: 22 additions & 0 deletions house-robber/sunjae95.js
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 lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js
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;
};
32 changes: 32 additions & 0 deletions non-overlapping-intervals/sunjae95.js
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;
});
Comment on lines +12 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

정렬의 기준은 결국 뒤에 값이 최종 범위가 되기 때문에 끝점만 비교 하셔도 괜찮을 거에요 :)
시작점을 같이 비교한것도 좋다고 생각합니다!


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;
};