Skip to content

[ganu] Week 8 #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions clone-graph/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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];
};
30 changes: 30 additions & 0 deletions longest-common-subsequence/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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);
};
52 changes: 52 additions & 0 deletions longest-repeating-character-replacement/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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;
};
17 changes: 17 additions & 0 deletions number-of-1-bits/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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;
}