Skip to content

[lhc0506] Week 08 Solutions #1505

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 4 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
38 changes: 38 additions & 0 deletions clone-graph/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* // 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 stack = [node];
const map = new Map();

map.set(node.val, new _Node(node.val));

while (stack.length > 0) {
const currentNode = stack.pop();

for (const neighbor of currentNode.neighbors) {
if (!map.has(neighbor.val)) {
map.set(neighbor.val, new Node(neighbor.val));
stack.push(neighbor);
}
map.get(currentNode.val).neighbors.push(map.get(neighbor.val));
}

}
return map.get(node.val);
};


// 시간복잡도: O(n)
// 공간복잡도: O(n)
32 changes: 32 additions & 0 deletions longest-repeating-character-replacement/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var characterReplacement = function(s, k) {
let start = 0;
let maxFreq = 0;
let result = 0;
const map = new Map();

for (let end = 0; end < s.length; end++) {
const endChar = s[end];
map.set(endChar, 1 + (map.get(endChar) || 0));

maxFreq = Math.max(maxFreq, map.get(endChar));

if (end - start + 1 - maxFreq > k) {
const startChar = s[start];
map.set(startChar, map.get(startChar) - 1);
start++;

}

result = Math.max(result, end - start + 1);
}

return result;
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)
30 changes: 30 additions & 0 deletions palindromic-substrings/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} s
* @return {number}
*/

var countSubstrings = function(s) {
let count = 0;
for (let i = 0; i < s.length; i++) {
let start = i;
let end = i;
while (start >= 0 && end < s.length && s[start] === s[end]) {
count += 1;
start -= 1;
end += 1;
}

start = i;
end = i + 1;
while (start >= 0 && end < s.length && s[start] === s[end]) {
count += 1;
start -= 1;
end += 1;
}
}

return count;
};

// 시간 복잡도: O(n^2)
// 공간 복잡도: O(1)
10 changes: 10 additions & 0 deletions reverse-bits/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function(n) {
return parseInt(n.toString(2).padStart(32, '0').split('').reverse().join(''), 2);
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)