diff --git a/best-time-to-buy-and-sell-stock/sukyoungshin.ts b/best-time-to-buy-and-sell-stock/sukyoungshin.ts new file mode 100644 index 000000000..d11be6617 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sukyoungshin.ts @@ -0,0 +1,24 @@ +function maxProfit(prices: number[]): number { + let minPrice = Number.MAX_SAFE_INTEGER; + let maxProfit = 0; + + for (let i = 0; i < prices.length; i++) { + const currentPrice = prices[i]; + + if (currentPrice < minPrice) { + minPrice = currentPrice; + } else { + const profit = currentPrice - minPrice; + maxProfit = Math.max(maxProfit, profit); + } + } + + return maxProfit; +}; + +maxProfit([7, 1, 5, 3, 6, 4]); // Output: 5 +maxProfit([7, 6, 4, 3, 1]); // Output: 0 +maxProfit([2, 4, 1]); // Output: 2 +maxProfit([1, 2]); // Output: 1 +maxProfit([2, 1]); // Output: 0 +maxProfit([1, 2, 3, 4, 5]); // Output: 4 diff --git a/encode-and-decode-strings/sukyoungshin.ts b/encode-and-decode-strings/sukyoungshin.ts new file mode 100644 index 000000000..6bc85c4d6 --- /dev/null +++ b/encode-and-decode-strings/sukyoungshin.ts @@ -0,0 +1,41 @@ +// https://neetcode.io/problems/string-encode-and-decode + +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + let encodedStrings: string[] = []; + + for (const word of strs) { + const length = word.length; + encodedStrings.push(`${length}#${word}`); + } + + return encodedStrings.join(""); + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(str) { + const decodedStrings: string[] = []; + let position = 0; + + while (position < str.length) { + const hashIndex = str.indexOf("#", position); + const length = Number(str.slice(position, hashIndex)); + + const start = hashIndex + 1; + const end = start + length; + const word = str.slice(start, end); + decodedStrings.push(word); + + position = end; + } + + return decodedStrings; + } +}; diff --git a/group-anagrams/sukyoungshin.ts b/group-anagrams/sukyoungshin.ts new file mode 100644 index 000000000..acfcc1414 --- /dev/null +++ b/group-anagrams/sukyoungshin.ts @@ -0,0 +1,31 @@ +// 1. 객체사용 +function groupAnagrams1(strs: string[]): string[][] { + let anagramGroups: Record = {}; + for (const word of strs) { + const sortedKey = [...word].sort().join(""); + + if (sortedKey in anagramGroups) { + anagramGroups[sortedKey].push(word); + } else { + anagramGroups[sortedKey] = [word]; + } + } + + return Object.values(anagramGroups); +}; + +// 2. Map 사용 +function groupAnagrams2(strs: string[]): string[][] { + let anagramGroups = new Map(); + for (const word of strs) { + const key = [...word].sort().join(""); + + if (anagramGroups.has(key)) { + anagramGroups.get(key)?.push(word); + } else { + anagramGroups.set(key, [word]); + } + } + + return [...anagramGroups.values()]; +}; diff --git a/word-break/sukyoungshin.ts b/word-break/sukyoungshin.ts new file mode 100644 index 000000000..921b61ada --- /dev/null +++ b/word-break/sukyoungshin.ts @@ -0,0 +1,23 @@ +function wordBreak(s: string, wordDict: string[]): boolean { + const cache: Record = {}; + + function canSplit(str: string): boolean { + if (str === "") return true; + if (cache[str] !== undefined) return cache[str]; + + for (let i = 1; i <= str.length; i++) { + const left = str.slice(0, i); + const right = str.slice(i); + + if (wordDict.includes(left) && canSplit(right)) { + cache[str] = true; + return true; + } + } + + cache[str] = false; + return false; + } + + return canSplit(s); +};