Skip to content

[lhc0506] WEEK 05 solutions #1395

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 5 commits into from
May 4, 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
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let minPrice = Number.MAX_SAFE_INTEGER;
let maxProfit = 0;

for (let i = 0; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}

return maxProfit;
};
17 changes: 17 additions & 0 deletions encode-and-decode-strings/lhc0506.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.stringfy() 문법을 쓰시는 것도 좋지만, 면접관 입장에서는 내부 동작 방식을 설명하거나 내장 함수 사용 없이 풀어보라고 할 수도 있어서요! 내장함수 사용 없이 풀어보시면 또 다른 방법을 찾으실 수있을것 같아요 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 문자 알파벳을 ASCII코드로 바꾸고 분리를 시키는것도 생각해봤긴했는데 다음엔 두개다 써보도록 하겠습니다. 감사합니다~

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return JSON.stringify(strs);
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
return JSON.parse(str);
}
}
33 changes: 33 additions & 0 deletions group-anagrams/lhc0506.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정렬 없이 문제 풀으신 분은 처음봤습니다;
이 경우 외부 for-loop 를 n, 내부 for-loop의 길이를 l로 분다면 시간 복잡도는 O(n * l) 이 되지 않을까요?
공간 복잡도도 같을것 같은데 의견 부탁드립니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문자열 배열내 각 문자열 길이는 정해지지않았으니 평균을 l로 하면 O(n * l) 이 더 정확할 것 같습니다. 감사합니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const A_ASCII = 'a'.charCodeAt();

const anagramMap = new Map();

for (const str of strs) {
const counter = new Array(26).fill(0);

for (let i = 0; i < str.length; i++) {
counter[str.charCodeAt(i) - A_ASCII]++;
}

let key = '';
for (let i = 0; i < 26; i++) {
if (counter[i] > 0) {
key += String.fromCharCode(i + A_ASCII) + counter[i];
}
}

if (!anagramMap.has(key)) {
anagramMap.set(key, []);
}
anagramMap.get(key).push(str);
}

return Array.from(anagramMap.values());
};

// 시간 복잡도: O(n), 공간 복잡도: O(n)
65 changes: 65 additions & 0 deletions implement-trie-prefix-tree/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

var Trie = function() {
this.isEnd = false;
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function(word) {
let currentTrie = this;
word.split('').forEach(alphabet => {
if (!currentTrie[alphabet]) {
currentTrie[alphabet] = new Trie();
}
currentTrie = currentTrie[alphabet];
});

currentTrie.isEnd = true;
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function(word) {
let currentTrie = this;

for (let alphabet of word) {
if (!currentTrie[alphabet]) {
return false;
}
currentTrie = currentTrie[alphabet];
}

return currentTrie.isEnd;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
let currentTrie = this;

for (let alphabet of prefix) {
if (!currentTrie[alphabet]) {
return false;
}
currentTrie = currentTrie[alphabet];
}

return true;
};

/**
* Your Trie object will be instantiated and called as such:
* var obj = new Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/


// insert 시간복잡도: O(n), search 시간복잡도: O(n), startsWith 시간복잡도: O(n)
29 changes: 29 additions & 0 deletions word-break/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function(s, wordDict) {
const wordDictSet = new Set(wordDict);
const visited = new Set();

const indexStack = [0];
while(indexStack.length > 0) {
const startIndex = indexStack.pop();

if (visited.has(startIndex)) continue;
visited.add(startIndex);

if (startIndex === s.length) return true;

for (let endIndex = startIndex + 1; endIndex <= s.length; endIndex++) {
const word = s.substring(startIndex, endIndex);

if (wordDictSet.has(word)) {
indexStack.push(endIndex);
}
}
}

return false;
};