Skip to content

Commit 8b828a2

Browse files
authored
Merge pull request #1519 from byol-han/main
[byol-han] WEEK 09 solutions
2 parents af8f3cb + 9716591 commit 8b828a2

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

โ€Žclone-graph/byol-han.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* https://leetcode.com/problems/clone-graph/
3+
* // Definition for a _Node.
4+
* function _Node(val, neighbors) {
5+
* this.val = val === undefined ? 0 : val;
6+
* this.neighbors = neighbors === undefined ? [] : neighbors;
7+
* };
8+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N) โ€” ๋…ธ๋“œ ์ˆ˜๋งŒํผ ์ˆœํšŒ
9+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N) โ€” visited ๋งต๊ณผ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ
10+
*/
11+
12+
/**
13+
* @param {_Node} node
14+
* @return {_Node}
15+
*/
16+
var cloneGraph = function (node) {
17+
if (!node) return null;
18+
19+
const visited = new Map();
20+
21+
const dfs = (n) => {
22+
if (visited.has(n)) {
23+
return visited.get(n);
24+
}
25+
26+
const clone = new Node(n.val);
27+
visited.set(n, clone);
28+
29+
for (let neighbor of n.neighbors) {
30+
clone.neighbors.push(dfs(neighbor));
31+
}
32+
33+
return clone;
34+
};
35+
36+
return dfs(node);
37+
};

โ€Žlinked-list-cycle/byol-han.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function (head) {
14+
// ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด ์žˆ๊ฑฐ๋‚˜ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ด๋ฉด ์‚ฌ์ดํด์ด ์žˆ์„ ์ˆ˜ ์—†์Œ
15+
if (!head || !head.next) return false;
16+
17+
// ๋‘ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”: slow๋Š” ํ•œ ์นธ์”ฉ, fast๋Š” ๋‘ ์นธ์”ฉ ์ด๋™
18+
let slow = head;
19+
let fast = head.next;
20+
21+
// fast์™€ slow๊ฐ€ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
22+
while (fast !== slow) {
23+
// fast๊ฐ€ ๋์— ๋„๋‹ฌํ•˜๋ฉด ์‚ฌ์ดํด์ด ์—†์Œ
24+
if (!fast || !fast.next) return false;
25+
26+
// slow๋Š” ํ•œ ์นธ ์ด๋™
27+
slow = slow.next;
28+
// fast๋Š” ๋‘ ์นธ ์ด๋™
29+
fast = fast.next.next;
30+
}
31+
32+
// fast์™€ slow๊ฐ€ ๋งŒ๋‚ฌ๋‹ค๋ฉด ์‚ฌ์ดํด์ด ์žˆ์Œ
33+
return true;
34+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* https://leetcode.com/problems/longest-common-subsequence/submissions/1644426037/
3+
* @param {string} text1
4+
* @param {string} text2
5+
* @return {number}
6+
*/
7+
var longestCommonSubsequence = function (text1, text2) {
8+
const m = text1.length;
9+
const n = text2.length;
10+
11+
// Create 2D array initialized with 0
12+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
13+
14+
// Fill the dp table
15+
for (let i = 1; i <= m; i++) {
16+
for (let j = 1; j <= n; j++) {
17+
if (text1[i - 1] === text2[j - 1]) {
18+
// Characters match
19+
dp[i][j] = dp[i - 1][j - 1] + 1;
20+
} else {
21+
// No match, take the max from left or top cell
22+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
23+
}
24+
}
25+
}
26+
27+
// The length of the longest common subsequence
28+
return dp[m][n];
29+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxProduct = function (nums) {
6+
// ์ตœ๋Œ€ ๊ณฑ์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
7+
let maxProduct = nums[0];
8+
// ํ˜„์žฌ ์œ„์น˜๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ณฑ๊ณผ ์ตœ์†Œ ๊ณฑ์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
9+
let currentMax = nums[0];
10+
let currentMin = nums[0];
11+
12+
// ๋ฐฐ์—ด์˜ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ˆœํšŒ
13+
for (let i = 1; i < nums.length; i++) {
14+
const num = nums[i];
15+
16+
// ์Œ์ˆ˜๋ฅผ ๊ณฑํ•  ๊ฒฝ์šฐ ์ตœ๋Œ€์™€ ์ตœ์†Œ๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ €์žฅ
17+
const tempMax = currentMax;
18+
19+
// ํ˜„์žฌ ์ˆซ์ž์™€ ๊ณฑํ–ˆ์„ ๋•Œ์˜ ์ตœ๋Œ€/์ตœ์†Œ ๊ฐ’์„ ๊ณ„์‚ฐ
20+
currentMax = Math.max(num, num * currentMax, num * currentMin);
21+
currentMin = Math.min(num, num * tempMax, num * currentMin);
22+
23+
// ์ „์ฒด ์ตœ๋Œ€ ๊ณฑ์„ ์—…๋ฐ์ดํŠธ
24+
maxProduct = Math.max(maxProduct, currentMax);
25+
}
26+
27+
return maxProduct;
28+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/palindromic-substrings/submissions/1644425061/
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
var countSubstrings = function (s) {
7+
let count = 0;
8+
9+
// Helper function to expand around the center
10+
function expandAroundCenter(left, right) {
11+
while (left >= 0 && right < s.length && s[left] === s[right]) {
12+
count++; // Found a palindrome
13+
left--;
14+
right++;
15+
}
16+
}
17+
18+
for (let i = 0; i < s.length; i++) {
19+
expandAroundCenter(i, i); // Odd-length palindromes
20+
expandAroundCenter(i, i + 1); // Even-length palindromes
21+
}
22+
23+
return count;
24+
};

โ€Žsum-of-two-integers/byol-han.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/sum-of-two-integers/submissions/1649575939/
3+
* @param {number} a
4+
* @param {number} b
5+
* @return {number}
6+
*/
7+
var getSum = function (a, b) {
8+
while (b !== 0) {
9+
// a์™€ b์˜ ํ•ฉ์—์„œ ์ž๋ฆฌ์˜ฌ๋ฆผ(carry)์„ ์ œ์™ธํ•œ ๊ฐ’ ๊ณ„์‚ฐ (XOR ์—ฐ์‚ฐ)
10+
let sum = a ^ b;
11+
12+
// ์ž๋ฆฌ์˜ฌ๋ฆผ(carry)์„ ๊ณ„์‚ฐ (AND ์—ฐ์‚ฐ ํ›„ ์™ผ์ชฝ์œผ๋กœ ํ•œ ๋น„ํŠธ ์ด๋™)
13+
let carry = (a & b) << 1;
14+
15+
// ์ƒˆ๋กœ์šด a๋Š” sum, ์ƒˆ๋กœ์šด b๋Š” carry๊ฐ€ ๋จ
16+
a = sum;
17+
b = carry;
18+
}
19+
20+
// carry๊ฐ€ 0์ด ๋˜๋ฉด ๋”ํ•  ๊ฒŒ ์—†์œผ๋ฏ€๋กœ ์ตœ์ข… ๊ฒฐ๊ณผ a ๋ฐ˜ํ™˜
21+
return a;
22+
};

0 commit comments

Comments
ย (0)