Skip to content

[moonjonghoo] WEEK 08 solutions #1508

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions clone-graph/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* // 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) {
if (!node) return null;

const visited = new Map();

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

// 노드 복사
const clone = new Node(currNode.val);
visited.set(currNode, clone);

// 이웃 노드들도 복사해서 연결
for (let neighbor of currNode.neighbors) {
clone.neighbors.push(dfs(neighbor));
}

return clone;
};

return dfs(node);
};
22 changes: 22 additions & 0 deletions longest-common-subsequence/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string} text1
* @param {string} text2
* @return {number}
*/
var longestCommonSubsequence = function (text1, text2) {
const m = text1.length,
n = text2.length;
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (text1[i - 1] === text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

return dp[m][n];
};
30 changes: 30 additions & 0 deletions longest-repeating-character-replacement/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var characterReplacement = function (s, k) {
let left = 0;
let maxCount = 0;
let freq = new Array(26).fill(0); // A~Z

let maxLength = 0;

for (let right = 0; right < s.length; right++) {
const idx = s.charCodeAt(right) - "A".charCodeAt(0);
freq[idx]++;
maxCount = Math.max(maxCount, freq[idx]);

let windowSize = right - left + 1;

if (windowSize - maxCount > k) {
const leftIdx = s.charCodeAt(left) - "A".charCodeAt(0);
freq[leftIdx]--;
left++;
}

maxLength = Math.max(maxLength, right - left + 1);
}

return maxLength;
};
37 changes: 37 additions & 0 deletions number-of-islands/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {character[][]} grid
* @return {number}
*/
const numIslands = function (grid) {
let count = 0;

const dfs = (i, j) => {
if (
i < 0 ||
i >= grid.length ||
j < 0 ||
j >= grid[i].length ||
grid[i][j] === "0"
) {
return;
}

grid[i][j] = "0";

dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
};

for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === "1") {
dfs(i, j);
count++;
}
}
}

return count;
};
22 changes: 22 additions & 0 deletions palindromic-substrings/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function (s) {
let count = 0;

const expand = (left, right) => {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++;
left--;
right++;
}
};

for (let i = 0; i < s.length; i++) {
expand(i, i); // 홀수 길이 중심 (예: "aba")
expand(i, i + 1); // 짝수 길이 중심 (예: "aa")
}

return count;
};
13 changes: 13 additions & 0 deletions reverse-bits/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {number} n - a positive integer
* @return {number} - reversed bits
*/
var reverseBits = function (n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result <<= 1; // 왼쪽으로 1비트 이동
result |= n & 1; // 마지막 비트 추출해서 결과에 추가
n >>>= 1; // 부호 없는 우측 시프트 (>>>)
}
return result >>> 0; // unsigned 32비트 정수로 변환
};