Skip to content

Commit 08c504a

Browse files
authored
[Berrnuda] 24.12.03 (#112)
* 217 / Contains Duplicate / Easy / 5m * 267 / Missing Number / Easy / 1m * 121 / Best Time to Buy and Sell Stock / Easy / 8m * 252 / Meeting Rooms / Easy / 10m * 15 / 3Sum / Medium / 11m 28s * 기존 파일들 1wwek 폴더로 이동 * 242 / Valid Anagram / Easy / 2m 50s * 238 / Product of Arrays Except Self / Medium / 21m 22s * 191 / Number of 1 Bits / Easy / 13m 27s * 100 / Same Tree / Easy / 22m 38s * 746 / Min Cost Climbing Stairs / Easy / 41m 1s * 49 / Group Anagrams / Medium / 13m 48s * 190 / Reverse Bits / Easy / 7m 36s" * 125 / Valid Palindrome / Easy / 4m 11s * 322 / Coin Change / Medium / 10m 52s * 206 / Reverse Linked list / Easy / 12m 25s * 338 / Counting Bits / Easy / 14m 28s * 70 / Climbing Stairs / Easy / 3m 11s * 21 / Merge Two Sorted Lists / Easy / 5m 28s * 48 / Rotate Image / Medium / 15m 42s * 1046 / Last Stone Weight / Easy / 8m 2s * 271 / Encode and Decode Strings / Medium / 28m * 198 / House Robber / Medium / 7m 31s * 66 / Plus Onde / Easy / 7m 48s * 704 / Binary Search / Easy / 3m 58s * 703 / Kth Largest Element in a Stream / Easy / 41m 16s" * 102 / Binary Tree Level Order Traversal / Medium / 13m * 572 / Subtree of Another Tree / Easy / 08m 59s * 213 / House Robber II / Medium / 11m 23s * 150 / Evaluate Reverse Polish Notation / Meium / 12m 01s * 56 / Merge Intervals / Medium / 12m 58s * 235 / Lowest Common Ancestor of a Binary Search Tree / Medium / 14m 45s * 139 / Word Break / Medium / 18m 22s * 215 / Kth Largest Element in an Array / Medium / 20m 19s * 오타수정 * 7 / Reverse Interger / Medium / 5m 57s * 743 / Network Delay Time / Medium / 35m 36s * 39 / Combination Sum / Medium / 21m 41s * 2013 / Detect Squares / Medium / 31m 15s * 57 / Insert Interval / Medium / 20m 29s
1 parent abb5839 commit 08c504a

9 files changed

+225
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var DetectSquares = function () {
2+
this.ptsCount = new Map();
3+
};
4+
5+
/**
6+
* @param {number[]} point
7+
* @return {void}
8+
*/
9+
DetectSquares.prototype.add = function (point) {
10+
const key = `${point[0]}/${point[1]}`;
11+
this.ptsCount.set(key, (this.ptsCount.get(key) || 0) + 1);
12+
};
13+
14+
/**
15+
* @param {number[]} point
16+
* @return {number}
17+
*/
18+
DetectSquares.prototype.count = function (point) {
19+
const [qx, qy] = point;
20+
let result = 0;
21+
22+
for (const key of this.ptsCount.keys()) {
23+
const [px, py] = key.split("/").map(Number);
24+
25+
if (Math.abs(px - qx) === Math.abs(py - qy) && px !== qx && py !== qy) {
26+
const key1 = `${px}/${qy}`;
27+
const key2 = `${qx}/${py}`;
28+
result +=
29+
(this.ptsCount.get(key) || 0) *
30+
(this.ptsCount.get(key1) || 0) *
31+
(this.ptsCount.get(key2) || 0);
32+
}
33+
}
34+
35+
return result;
36+
};
37+
38+
/**
39+
* Your DetectSquares object will be instantiated and called as such:
40+
* var obj = new DetectSquares()
41+
* obj.add(point)
42+
* var param_2 = obj.count(point)
43+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function (candidates, target) {
7+
const result = [];
8+
const stack = [[[], 0, 0]];
9+
10+
while (stack.length > 0) {
11+
const [currentCombination, currentSum, startIndex] = stack.pop();
12+
13+
if (currentSum === target) {
14+
result.push(currentCombination);
15+
continue;
16+
}
17+
18+
if (currentSum > target) continue;
19+
20+
for (let i = startIndex; i < candidates.length; i++) {
21+
stack.push([
22+
[...currentCombination, candidates[i]],
23+
currentSum + candidates[i],
24+
i,
25+
]);
26+
}
27+
}
28+
29+
return result;
30+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @param {number[]} newInterval
4+
* @return {number[][]}
5+
*/
6+
var insert = function (intervals, newInterval) {
7+
intervals.push(newInterval);
8+
intervals.sort((a, b) => a[0] - b[0]);
9+
10+
let res = [intervals[0]];
11+
12+
for (let i = 1; i < intervals.length; i++) {
13+
if (res[res.length - 1][1] >= intervals[i][0]) {
14+
res[res.length - 1][1] = Math.max(
15+
res[res.length - 1][1],
16+
intervals[i][1]
17+
);
18+
} else {
19+
res.push(intervals[i]);
20+
}
21+
}
22+
23+
return res;
24+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[][]} times
3+
* @param {number} n
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var networkDelayTime = function (times, n, k) {
8+
const node = Array.from({ length: n + 1 }, () => []);
9+
for (const [u, v, w] of times) node[u].push([v, w]);
10+
11+
const dist = new Array(n + 1).fill(Infinity);
12+
dist[k] = 0;
13+
14+
const visited = new Array(n + 1).fill(false);
15+
16+
for (let i = 1; i <= n; i++) {
17+
let minNode = -1;
18+
for (let j = 1; j <= n; j++) {
19+
if (!visited[j] && (minNode === -1 || dist[j] < dist[minNode]))
20+
minNode = j;
21+
}
22+
23+
if (dist[minNode] === Infinity) break;
24+
25+
visited[minNode] = true;
26+
27+
for (const [neighbor, weight] of node[minNode]) {
28+
const newDist = dist[minNode] + weight;
29+
if (newDist < dist[neighbor]) dist[neighbor] = newDist;
30+
}
31+
}
32+
33+
const maxDist = Math.max(...dist.slice(1));
34+
return maxDist === Infinity ? -1 : maxDist;
35+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
var reverse = function (x) {
6+
const sign = x < 0;
7+
const rev = parseInt(String(Math.abs(x)).split("").reverse().join(""));
8+
9+
if (rev < -Math.pow(2, 31) || rev > Math.pow(2, 31) - 1) return 0;
10+
11+
return sign ? -rev : rev;
12+
};

berrnuda/6week/198_House_Robber.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function (nums) {
6+
let rob1 = 0;
7+
let rob2 = 0;
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
let rob = Math.max(rob2 + nums[i], rob1);
11+
rob2 = rob1;
12+
rob1 = rob;
13+
}
14+
15+
return rob1;
16+
};

berrnuda/6week/66_Plus_One.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} digits
3+
* @return {number[]}
4+
*/
5+
var plusOne = function (digits) {
6+
let l = digits.length;
7+
8+
for (let i = l - 1; i >= 0; i--) {
9+
digits[i]++;
10+
if (digits[i] == 10) digits[i] = 0;
11+
else return digits;
12+
}
13+
14+
digits.unshift(1);
15+
16+
return digits;
17+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} k
3+
* @param {number[]} nums
4+
*/
5+
var KthLargest = function (k, nums) {
6+
this.k = k;
7+
this.score = [];
8+
9+
for (let num of nums) this.add(num);
10+
};
11+
12+
/**
13+
* @param {number} val
14+
* @return {number}
15+
*/
16+
KthLargest.prototype.add = function (val) {
17+
if (this.score.length < this.k) this.score.push(val);
18+
else if (val > this.score[this.k - 1]) this.score[this.k - 1] = val;
19+
20+
this.score.sort((a, b) => b - a);
21+
22+
return this.score[this.k - 1];
23+
};
24+
25+
/**
26+
* Your KthLargest object will be instantiated and called as such:
27+
* var obj = new KthLargest(k, nums)
28+
* var param_1 = obj.add(val)
29+
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var search = function (nums, target) {
7+
let first = 0;
8+
let last = nums.length - 1;
9+
10+
for (; first <= last; ) {
11+
let curr = Math.floor((first + last) / 2);
12+
13+
if (nums[curr] === target) return curr;
14+
else if (nums[curr] < target) first = curr + 1;
15+
else last = curr - 1;
16+
}
17+
18+
return -1;
19+
};

0 commit comments

Comments
 (0)