From 9f5780f02e129f938c11ebec6aae8336ad12a782 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Mon, 28 Apr 2025 14:18:47 +0900 Subject: [PATCH 1/4] add: #221 Best Time to Buy And Sell Stock --- .../sukyoungshin.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/sukyoungshin.ts 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 From e82a5cc871ecfd95cc55caefea032f1494ef6bd4 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Wed, 30 Apr 2025 15:03:08 +0900 Subject: [PATCH 2/4] add: #236 group anagrams --- group-anagrams/sukyoungshin.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 group-anagrams/sukyoungshin.ts 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()]; +}; From 878719359f922aef4542347e17761b9360c1c4a0 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Thu, 1 May 2025 13:53:01 +0900 Subject: [PATCH 3/4] add: #271 Word Break --- word-break/sukyoungshin.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 word-break/sukyoungshin.ts 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); +}; From db5924b37f0a7088adf448ea0643a8eecab72914 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Fri, 2 May 2025 22:38:14 +0900 Subject: [PATCH 4/4] add : #238 Encode and Decode Strings --- encode-and-decode-strings/sukyoungshin.ts | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 encode-and-decode-strings/sukyoungshin.ts 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; + } +};