Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions clone-graph/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* https://leetcode.com/problems/clone-graph/
* // Definition for a _Node.
* function _Node(val, neighbors) {
* this.val = val === undefined ? 0 : val;
* this.neighbors = neighbors === undefined ? [] : neighbors;
* };
* 시간 복잡도: O(N) — 노드 수만큼 순회
* 공간 복잡도: O(N) — visited 맵과 재귀 호출 스택
*/

/**
* @param {_Node} node
* @return {_Node}
*/
var cloneGraph = function (node) {
if (!node) return null;

const visited = new Map();

const dfs = (n) => {
if (visited.has(n)) {
return visited.get(n);
}

const clone = new Node(n.val);
visited.set(n, clone);

for (let neighbor of n.neighbors) {
clone.neighbors.push(dfs(neighbor));
}

return clone;
};

return dfs(node);
};
29 changes: 29 additions & 0 deletions longest-common-subsequence/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* https://leetcode.com/problems/longest-common-subsequence/submissions/1644426037/
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function (text1, text2) {
const m = text1.length;
const n = text2.length;

// Create 2D array initialized with 0
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

// Fill the dp table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (text1[i - 1] === text2[j - 1]) {
// Characters match
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
// No match, take the max from left or top cell
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

// The length of the longest common subsequence
return dp[m][n];
};
24 changes: 24 additions & 0 deletions palindromic-substrings/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* https://leetcode.com/problems/palindromic-substrings/submissions/1644425061/
* @param {string} s
* @return {number}
*/
var countSubstrings = function (s) {
let count = 0;

// Helper function to expand around the center
function expandAroundCenter(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++; // Found a palindrome
left--;
right++;
}
}

for (let i = 0; i < s.length; i++) {
expandAroundCenter(i, i); // Odd-length palindromes
expandAroundCenter(i, i + 1); // Even-length palindromes
}

return count;
};
Loading