Skip to content

[hsskey] Week 05 Solutions #1389

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 3, 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
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let buy = prices[0]
let maxVal = 0

for(let i = 1; i < prices.length; i++) {
if(prices[i - 1] > prices[i]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

인접한 가격(prices[i-1], prices[i])을 비교하는 방식보다 더 간단하게 접근할 수 있을 것 같아요. 현재 가격이 지금까지의 최소값보다 크면 이익을 계산하고, 아니면 최소값을 갱신하는 방식으로요!!

Copy link
Contributor

Choose a reason for hiding this comment

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

지금 로직도 성능적으로는 O(n) 시간 복잡도와 O(1) 공간 복잡도를 유지해서 효율적이네요 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

직관적으로 생각해서 풀이했던 내용인데 피드백 주신내용으로 다시 풀어봐야겠네요 감사합니다.

buy = Math.min(buy, prices[i])
}

if(prices[i - 1] < prices[i]) {
maxVal = Math.max(maxVal, prices[i] - buy)
}
}
return maxVal
};
33 changes: 33 additions & 0 deletions encode-and-decode-strings/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return strs.map((item) => `${item.length}#${item}`).join('');
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const result = [];
let i = 0;

while (i < str.length) {
let j = i;
while (str[j] !== '#') {
j++;
}

const length = parseInt(str.slice(i, j));
const word = str.slice(j + 1, j + 1 + length);
result.push(word);

i = j + 1 + length;
}

return result;
}
}
18 changes: 18 additions & 0 deletions group-anagrams/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const map = new Map()

for(const str of strs) {
const sortedStr = [...str].sort().join('')
if(map.has(sortedStr)) {
map.get(sortedStr).push(str)
} else {
map.set(sortedStr, [str])
}
}

return Array.from(map.values())
Copy link
Contributor

Choose a reason for hiding this comment

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

간결하고 효율적인 코드네요!
Map 객체 활용해서 애너그램 그룹핑한 방식이 깔끔하다고 생각합니다

정렬 방식으로 애너그램 판별하는 접근법도 문제 특성상 아주 적절한 선택이라고 생각합니다. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

알파벳 빈도수 배열로 그룹화하는 방식으로도 풀 수 있는 방법을 알게 되서 공유드려요

"eat" → [1, 0, 0, ..., 1, ..., 1] → a:1, e:1, t:1
"tea" → 같은 구성 → 같은 배열
이 배열을 string으로 바꿔서 Map의 key로 사용 → 같은 그룹으로 묶임

};
51 changes: 51 additions & 0 deletions implement-trie-prefix-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class TrieNode {
constructor() {
this.children = {};
this.isEndOfWord = false;
}
}

var Trie = function() {
this.root = new TrieNode();
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.isEndOfWord = true;
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}
return node.isEndOfWord;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
let node = this.root;
for (const char of prefix) {
if (!node.children[char]) return false;
node = node.children[char];
}
return true;
};
22 changes: 22 additions & 0 deletions word-break/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function(s, wordDict) {
const wordSet = new Set(wordDict);
const dp = new Array(s.length + 1).fill(false);
dp[0] = true;

for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
const word = s.slice(j, i);
if (dp[j] && wordSet.has(word)) {
dp[i] = true;
break;
}
}
}

return dp[s.length];
};