Skip to content

Commit e6255ee

Browse files
authored
Merge pull request #1613 from byol-han/main
[byol-han] WEEK 13 solutions
2 parents b185c95 + fbcc68a commit e6255ee

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

β€Žinsert-interval/byol-han.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* https://leetcode.com/problems/insert-interval/submissions/1678158074/
3+
* @param {number[][]} intervals
4+
* @param {number[]} newInterval
5+
* @return {number[][]}
6+
*/
7+
var insert = function (intervals, newInterval) {
8+
const result = [];
9+
let i = 0;
10+
const n = intervals.length;
11+
12+
// 1. newInterval보닀 λλ‚˜λŠ” μ‹œμ μ΄ 먼저인 interval은 κ·Έλƒ₯ μΆ”κ°€
13+
while (i < n && intervals[i][1] < newInterval[0]) {
14+
result.push(intervals[i]);
15+
i++;
16+
}
17+
18+
// 2. newIntervalκ³Ό κ²ΉμΉ˜λŠ” interval은 병합
19+
while (i < n && intervals[i][0] <= newInterval[1]) {
20+
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
21+
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
22+
i++;
23+
}
24+
result.push(newInterval);
25+
26+
// 3. λ‚˜λ¨Έμ§€ interval은 κ·ΈλŒ€λ‘œ μΆ”κ°€
27+
while (i < n) {
28+
result.push(intervals[i]);
29+
i++;
30+
}
31+
32+
return result;
33+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @param {number} k
13+
* @return {number}
14+
*/
15+
var kthSmallest = function (root, k) {
16+
// μ€‘μœ„ 순회 κ²°κ³Όλ₯Ό μ €μž₯ν•  λ°°μ—΄
17+
const inorder = [];
18+
19+
// μ€‘μœ„ 순회 ν•¨μˆ˜ μ •μ˜
20+
function traverse(node) {
21+
if (!node) return;
22+
traverse(node.left); // μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬ λ°©λ¬Έ
23+
inorder.push(node.val); // ν˜„μž¬ λ…Έλ“œ κ°’ μΆ”κ°€
24+
traverse(node.right); // 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬ λ°©λ¬Έ
25+
}
26+
27+
// 트리 순회 μ‹œμž‘
28+
traverse(root);
29+
30+
// k번째둜 μž‘μ€ κ°’ λ°˜ν™˜ (1-indexed μ΄λ―€λ‘œ k-1)
31+
return inorder[k - 1];
32+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val) {
5+
* this.val = val;
6+
* this.left = this.right = null;
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @param {TreeNode} p
13+
* @param {TreeNode} q
14+
* @return {TreeNode}
15+
*/
16+
var lowestCommonAncestor = function (root, p, q) {
17+
let current = root;
18+
19+
while (current) {
20+
// 두 λ…Έλ“œμ˜ 값이 ν˜„μž¬ λ…Έλ“œλ³΄λ‹€ λͺ¨λ‘ μž‘μœΌλ©΄ μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬λ‘œ 이동
21+
if (p.val < current.val && q.val < current.val) {
22+
current = current.left;
23+
}
24+
// 두 λ…Έλ“œμ˜ 값이 ν˜„μž¬ λ…Έλ“œλ³΄λ‹€ λͺ¨λ‘ 크면 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬λ‘œ 이동
25+
else if (p.val > current.val && q.val > current.val) {
26+
current = current.right;
27+
}
28+
// ν˜„μž¬ λ…Έλ“œκ°€ 뢄기점이면 그게 LCA!
29+
else {
30+
return current;
31+
}
32+
}
33+
34+
return null; // λ§Œμ•½ νŠΈλ¦¬μ— μ—†λ‹€λ©΄ null λ°˜ν™˜ (일반적으둜 BSTμ—λŠ” 항상 μ‘΄μž¬ν•œλ‹€κ³  κ°€μ •)
35+
};

β€Žmeeting-rooms/byol-han.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Interval } from '/opt/node/lib/lintcode/index.js';
2+
3+
/**
4+
* https://www.lintcode.com/problem/920/
5+
* Definition of Interval:
6+
* class Interval {
7+
* constructor(start, end) {
8+
* this.start = start;
9+
* this.end = end;
10+
* }
11+
* }
12+
*/
13+
14+
export class Solution {
15+
/**
16+
* @param intervals: an array of meeting time intervals
17+
* @return: if a person could attend all meetings
18+
*/
19+
canAttendMeetings(intervals) {
20+
// λ¨Όμ € 회의λ₯Ό μ‹œμž‘ μ‹œκ°„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•©λ‹ˆλ‹€.
21+
intervals.sort((a, b) => a[0] - b[0]);
22+
23+
// μ •λ ¬λœ νšŒμ˜λ“€μ„ 순차적으둜 λΉ„κ΅ν•˜λ©° κ²ΉμΉ˜λŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.
24+
for (let i = 1; i < intervals.length; i++) {
25+
const prevEnd = intervals[i - 1][1]; // 이전 회의의 끝 μ‹œκ°„
26+
const currStart = intervals[i][0]; // ν˜„μž¬ 회의의 μ‹œμž‘ μ‹œκ°„
27+
28+
// 이전 회의의 끝 μ‹œκ°„λ³΄λ‹€ ν˜„μž¬ 회의의 μ‹œμž‘ μ‹œκ°„μ΄ λΉ λ₯΄λ©΄ κ²ΉμΉ©λ‹ˆλ‹€.
29+
if (currStart < prevEnd) {
30+
return false;
31+
}
32+
}
33+
34+
// κ²ΉμΉ˜λŠ” νšŒμ˜κ°€ μ—†μœΌλ©΄ trueλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
35+
return true;
36+
}
37+
}

0 commit comments

Comments
Β (0)