diff --git a/clone-graph/gwbaik9717.js b/clone-graph/gwbaik9717.js new file mode 100644 index 000000000..a873e4cb8 --- /dev/null +++ b/clone-graph/gwbaik9717.js @@ -0,0 +1,43 @@ +// v: len(vertexes), e: len(edges) +// Time complexity: O(v + e) +// Space complexity: O(v + e) + +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + const nodes = Array.from({ length: 101 }, (_, i) => null); + + const dfs = (node) => { + if (!node) { + return; + } + + if (nodes[node.val]) { + return nodes[node.val]; + } + + const newNode = new _Node(node.val); + nodes[node.val] = newNode; + + for (const neighbor of node.neighbors) { + const cloned = dfs(neighbor); + newNode.neighbors.push(cloned); + } + + return newNode; + }; + + dfs(node); + + return nodes[1]; +}; diff --git a/longest-common-subsequence/gwbaik9717.js b/longest-common-subsequence/gwbaik9717.js new file mode 100644 index 000000000..12ec00378 --- /dev/null +++ b/longest-common-subsequence/gwbaik9717.js @@ -0,0 +1,30 @@ +// n: len(text1), m: len(text2) +// Time complexity: O(n * m) +// Space complexity: O(n * m) + +/** + * @param {string} text1 + * @param {string} text2 + * @return {number} + */ +var longestCommonSubsequence = function (text1, text2) { + const n = text1.length; + const m = text2.length; + + const dp = Array.from({ length: n + 1 }, () => + Array.from({ length: m + 1 }, () => 0) + ); + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= m; j++) { + if (text1[i - 1] === text2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + continue; + } + + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + + return dp.at(-1).at(-1); +}; diff --git a/longest-repeating-character-replacement/gwbaik9717.js b/longest-repeating-character-replacement/gwbaik9717.js new file mode 100644 index 000000000..d9bc8ddf8 --- /dev/null +++ b/longest-repeating-character-replacement/gwbaik9717.js @@ -0,0 +1,52 @@ +// Time complexity: O(n) +// Space complexity: O(1) + +/** + * @param {string} s + * @param {number} k + * @return {number} + */ +var characterReplacement = function (s, k) { + let i = 0; + let j = 0; + + const counter = new Map(); + counter.set(s[i], 1); + + let answer = 1; + + const getMaxCount = () => { + let maxCount = 0; + + for (const [key, value] of counter) { + maxCount = Math.max(maxCount, value); + } + + return maxCount; + }; + + while (true) { + if (s.length - i <= answer) { + break; + } + + const maxCount = getMaxCount(); + const totalCount = j - i + 1; + + if (totalCount - maxCount <= k) { + j++; + counter.set(s[j], (counter.get(s[j]) || 0) + 1); + answer = Math.max(totalCount, answer); + continue; + } + + counter.set(s[i], counter.get(s[i]) - 1); + if (counter.get(s[i]) === 0) { + counter.delete(s[i]); + } + + i++; + } + + return answer; +}; diff --git a/number-of-1-bits/gwbaik9717.js b/number-of-1-bits/gwbaik9717.js new file mode 100644 index 000000000..466ff2193 --- /dev/null +++ b/number-of-1-bits/gwbaik9717.js @@ -0,0 +1,17 @@ +// Time complexity: O(logn) +// Space complexity: O(1) + +function hammingWeight(n) { + let answer = 1; + let current = n; + + while (current > 1) { + if (current % 2 !== 0) { + answer++; + } + + current = Math.floor(current / 2); + } + + return answer; +}