Skip to content

[krokerdile] Week 05 Solution #1407

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
14 changes: 14 additions & 0 deletions best-time-to-buy-and-sell-stock/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var maxProfit = function(prices) {
let minPrice = Infinity;
let maxProfit = 0;

for (let price of prices) {
if (price < minPrice) {
minPrice = price; // 더 싼 가격이 나타나면 갱신
} else {
maxProfit = Math.max(maxProfit, price - minPrice); // 이익 갱신
}
}

return maxProfit;
};
35 changes: 35 additions & 0 deletions encode-and-decode-strings/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
let result = "";

for (const str of strs) {
result += `${str.length}#${str}`;
}

return result;
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(s) {
let result = [];
let i = 0;
// 5#hello5#world
while (i < s.length) {
const pos = s.indexOf("#", i);
const len = parseInt(s.slice(i, pos));// 5
i = pos + 1;
const str = s.slice(i, i + len);
result.push(str);
i += len;
}
return result;
}
}

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

strs.forEach(str => {
const sorted = str.split('').sort().join('');
if (!dict.has(sorted)) {
dict.set(sorted, [str]);
} else {
dict.get(sorted).push(str);
}
});

// value 길이 기준 내림차순 정렬
const result = [...dict.entries()]
.sort((a, b) => b[1].length - a[1].length)
.map(([_, group]) => group); // value (즉, 아나그램 그룹)만 꺼냄

return result;
};
42 changes: 42 additions & 0 deletions implement-trie-prefix-tree/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var Trie = function() {
this.root = {}; // 루트는 빈 객체로 시작
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function(word) {
let node = this.root;
for (let ch of word) {
if (!node[ch]) node[ch] = {};
node = node[ch];
}
node.isEnd = true; // 단어의 끝을 표시
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function(word) {
let node = this.root;
for (let ch of word) {
if (!node[ch]) return false;
node = node[ch];
}
return node.isEnd === true; // 단어의 끝이어야만 true
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
let node = this.root;
for (let ch of prefix) {
if (!node[ch]) return false;
node = node[ch];
}
return true; // 접두사만 매칭되면 true
};
22 changes: 22 additions & 0 deletions word-break/krokerdile.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); // 빠른 검색을 위한 Set
const dp = 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];
};