Skip to content

Commit b5ea5e0

Browse files
CitrusSodaJooKangsan
authored andcommitted
[citrussoda] 24.11.26 (#106)
* 56 / Merge Intervals / Medium / 22m 31s * 1584 / Min Cost to Connect All Points / Medium / >25m * 235 / Lowest Common Ancestor of a Binary Search Tree / Medium / 13m 12s * 139 / Word Break / Medium / 23m 19s * 215 / Kth Largest Element in an Array / Medium / 2m
1 parent 64af8a9 commit b5ea5e0

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
var wordBreak = function (s, wordDict) {
7+
const dp = new Array(s.length + 1).fill(false);
8+
dp[0] = true;
9+
10+
for (let i = 1; i <= s.length; i++) {
11+
for (let word of wordDict) {
12+
if (i >= word.length && dp[i - word.length]) {
13+
const sub = s.slice(i - word.length, i);
14+
if (sub === word) {
15+
dp[i] = true;
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
22+
return dp[s.length];
23+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number[][]} points
3+
* @return {number}
4+
*/
5+
// Prim's Algorithm
6+
var minCostConnectPoints = function (points) {
7+
const n = points.length;
8+
9+
// 방문 여부를 체크하는 배열
10+
const visited = new Set();
11+
12+
// 각 점까지의 최소 거리를 저장하는 배열
13+
const distances = new Array(n).fill(Infinity);
14+
distances[0] = 0;
15+
16+
let totalCost = 0;
17+
18+
for (let i = 0; i < n; i++) {
19+
let minDist = Infinity;
20+
let minIndex = -1;
21+
22+
// 아직 방문하지 않은 점들 중에서 최소 거리를 가진 점을 찾음
23+
for (let j = 0; j < n; j++) {
24+
if (!visited.has(j) && distances[j] < minDist) {
25+
minDist = distances[j];
26+
minIndex = j;
27+
}
28+
}
29+
30+
visited.add(minIndex);
31+
totalCost += minDist;
32+
33+
// 선택된 점에서 다른 모든 점까지의 거리를 업데이트
34+
for (let j = 0; j < n; j++) {
35+
if (!visited.has(j)) {
36+
const distance =
37+
Math.abs(points[minIndex][0] - points[j][0]) +
38+
Math.abs(points[minIndex][1] - points[j][1]);
39+
distances[j] = Math.min(distances[j], distance);
40+
}
41+
}
42+
}
43+
44+
return totalCost;
45+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findKthLargest = function (nums, k) {
7+
nums.sort((a, b) => b - a);
8+
9+
return nums[k - 1];
10+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
var lowestCommonAncestor = function (root, p, q) {
16+
if (root.val > p.val && root.val < q.val) {
17+
return root;
18+
}
19+
20+
if (root.val > p.val && root.val > q.val) {
21+
return lowestCommonAncestor(root.left, p, q);
22+
}
23+
24+
if (root.val < p.val && root.val < q.val) {
25+
return lowestCommonAncestor(root.right, p, q);
26+
}
27+
28+
return root;
29+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function (intervals) {
6+
intervals.sort((a, b) => a[0] - b[0]);
7+
8+
const ans = [];
9+
let prev = intervals[0];
10+
11+
for (let i = 1; i < intervals.length; i++) {
12+
let interval = intervals[i];
13+
14+
if (interval[0] <= prev[1]) {
15+
prev[1] = Math.max(prev[1], interval[1]);
16+
} else {
17+
ans.push(prev);
18+
prev = interval;
19+
}
20+
}
21+
22+
ans.push(prev);
23+
return ans;
24+
};

0 commit comments

Comments
 (0)