diff --git a/oh-chaeyeon/2013_Detect_Squares.js b/oh-chaeyeon/2013_Detect_Squares.js new file mode 100644 index 0000000..69f967c --- /dev/null +++ b/oh-chaeyeon/2013_Detect_Squares.js @@ -0,0 +1,50 @@ +var DetectSquares = function () { + this.pointCount = new Map(); +}; + +/** + * @param {number[]} point + * @return {void} + */ +DetectSquares.prototype.add = function (point) { + const [x, y] = point; + const key = `${x},${y}`; + this.pointCount.set(key, (this.pointCount.get(key) || 0) + 1); +}; + +/** + * @param {number[]} point + * @return {number} + */ +DetectSquares.prototype.count = function (point) { + const [x, y] = point; + let totalSquares = 0; + + for (const key of this.pointCount.keys()) { + const [px, py] = key.split(",").map(Number); + + if (Math.abs(px - x) !== Math.abs(py - y) || px === x || py === y) continue; + + const corner1 = `${px},${y}`; + const corner2 = `${x},${py}`; + + const countCurrent = this.pointCount.get(key) || 0; + const countCorner1 = this.pointCount.get(corner1) || 0; + const countCorner2 = this.pointCount.get(corner2) || 0; + + totalSquares += countCurrent * countCorner1 * countCorner2; + } + + return totalSquares; +}; + +/** + * Your DetectSquares object will be instantiated and called as such: + * var obj = new DetectSquares() + * obj.add(point) + * var param_2 = obj.count(point) + */ + +// Time taken : 57m 48s +// Runtime : 336ms +// Memory : 62.48MB diff --git a/oh-chaeyeon/39_Combination_Sum.js b/oh-chaeyeon/39_Combination_Sum.js new file mode 100644 index 0000000..e5913bb --- /dev/null +++ b/oh-chaeyeon/39_Combination_Sum.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function (candidates, target) { + const dp = Array.from({ length: target + 1 }, () => []); + dp[0] = [[]]; + + for (let i = 0; i < candidates.length; i++) { + for (let j = candidates[i]; j <= target; j++) { + const combinations = dp[j - candidates[i]]; + for (const combination of combinations) { + dp[j].push([...combination, candidates[i]]); + } + } + } + + return dp[target]; +}; + +// Time taken : 33m 36s +// Runtime : 6ms +// Memory : 57.15MB diff --git a/oh-chaeyeon/57_Insert_Interval.JS b/oh-chaeyeon/57_Insert_Interval.JS new file mode 100644 index 0000000..fa7740b --- /dev/null +++ b/oh-chaeyeon/57_Insert_Interval.JS @@ -0,0 +1,32 @@ +/** + * @param {number[][]} intervals + * @param {number[]} newInterval + * @return {number[][]} + */ +var insert = function (intervals, newInterval) { + const result = []; + let i = 0; + + while (i < intervals.length && intervals[i][1] < newInterval[0]) { + result.push(intervals[i]); + i++; + } + + while (i < intervals.length && intervals[i][0] <= newInterval[1]) { + newInterval[0] = Math.min(newInterval[0], intervals[i][0]); + newInterval[1] = Math.max(newInterval[1], intervals[i][1]); + i++; + } + result.push(newInterval); + + while (i < intervals.length) { + result.push(intervals[i]); + i++; + } + + return result; +}; + +// Time taken : 24m 13s +// Runtime : 4ms +// Memory : 54.67MB diff --git a/oh-chaeyeon/743_Network_Delay_Time.js b/oh-chaeyeon/743_Network_Delay_Time.js new file mode 100644 index 0000000..6b99a7d --- /dev/null +++ b/oh-chaeyeon/743_Network_Delay_Time.js @@ -0,0 +1,25 @@ +/** + * @param {number[][]} times + * @param {number} n + * @param {number} k + * @return {number} + */ +var networkDelayTime = function (times, n, k) { + const minTime = new Array(n + 1).fill(Infinity); + minTime[k] = 0; + + for (let i = 1; i < n; i++) { + for (const [u, v, w] of times) { + if (minTime[u] !== Infinity && minTime[u] + w < minTime[v]) { + minTime[v] = minTime[u] + w; + } + } + } + + const maxTime = Math.max(...minTime.slice(1)); + return maxTime === Infinity ? -1 : maxTime; +}; + +// Time taken : 1h 20m +// Runtime : 103ms +// Memory : 57.07MB diff --git a/oh-chaeyeon/7_Reverse_integer.js b/oh-chaeyeon/7_Reverse_integer.js new file mode 100644 index 0000000..6fadf6c --- /dev/null +++ b/oh-chaeyeon/7_Reverse_integer.js @@ -0,0 +1,26 @@ +/** + * @param {number} x + * @return {number} + */ +var reverse = function (x) { + let num = 0; + const INT_MIN = -Math.pow(2, 31); + const INT_MAX = Math.pow(2, 31) - 1; + + while (x !== 0) { + let digit = x % 10; + x = (x - digit) / 10; + + num = num * 10 + digit; + + if (num < INT_MIN || num > INT_MAX) { + return 0; + } + } + + return num; +}; + +// Time taken : 30m+ +// Runtime : 59ms (73ms) +// Memory : 53.55MB