diff --git a/best-time-to-buy-and-sell-stock/soobing.ts b/best-time-to-buy-and-sell-stock/soobing.ts index b30c33976b..e83e6469f0 100644 --- a/best-time-to-buy-and-sell-stock/soobing.ts +++ b/best-time-to-buy-and-sell-stock/soobing.ts @@ -1,14 +1,16 @@ +// 3rd tried function maxProfit(prices: number[]): number { - let minPrice = Infinity; - let maxProfit = 0; - for (let i = 0; i < prices.length; i++) { - if (prices[i] < minPrice) { - minPrice = prices[i]; - } + let left = 0; + let right = 1; + let max = 0; - if (prices[i] - minPrice > maxProfit) { - maxProfit = prices[i] - minPrice; - } + while(left < right && right < prices.length) { + if(prices[right] - prices[left] > 0) { + max = Math.max(max, prices[right] - prices[left]) + } else { + left = right; + } + right++; } - return maxProfit; -} + return max; +}; diff --git a/encode-and-decode-strings/soobing.ts b/encode-and-decode-strings/soobing.ts index e3b471b12b..89bb0811d6 100644 --- a/encode-and-decode-strings/soobing.ts +++ b/encode-and-decode-strings/soobing.ts @@ -1,34 +1,26 @@ +// 3rd tried class Solution { /** - * 문자열 배열을 하나의 문자열로 인코딩합니다. - * @param strs - 문자열 배열 - * @returns 인코딩된 하나의 문자열 + * @param {string[]} strs + * @returns {string} */ - encode(strs: string[]): string { - return strs.map((str) => str.length + "#" + str).join(""); + encode(strs) { + return strs.map((str) => `${str.length}#${str}`).join(''); } /** - * 인코딩된 문자열을 원래 문자열 배열로 디코딩합니다. - * @param str - 인코딩된 문자열 - * @returns 디코딩된 문자열 배열 + * @param {string} str + * @returns {string[]} */ - decode(str: string): string[] { - const result: string[] = []; - - let i = 0; - while (i < str.length) { - let j = i; - while (str[j] !== "#") { - j++; + decode(str) { + const result: string[] = []; + let i = 0; + while(i < str.length) { + const j = str.indexOf('#', i); + const length = Number(str.slice(i, j)); + result.push(str.slice(j + 1, j + 1 + length)); + i = j + 1 + length; } - - 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; + return result; } } diff --git a/group-anagrams/soobing.ts b/group-anagrams/soobing.ts index c446e01609..ecad1e2a6e 100644 --- a/group-anagrams/soobing.ts +++ b/group-anagrams/soobing.ts @@ -1,15 +1,11 @@ -// idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record 에 맞게 매핑하여 values들만 리턴하면 될것 같음 +// 3rd tried function groupAnagrams(strs: string[]): string[][] { - const map = new Map(); + const map = new Map(); - for (let i = 0; i < strs.length; i++) { - const key = strs[i].split("").sort().join(""); - const group = map.get(key); - if (group) { - group.push(strs[i]); - } else { - map.set(key, [strs[i]]); - } + for(const str of strs) { + const key = str.split('').sort().join(''); + const value = map.get(key); + map.set(key, value ? [...value, str] : [str]) } - return [...map.values()]; -} + return Array.from(map.values()); +}; diff --git a/implement-trie-prefix-tree/soobing.ts b/implement-trie-prefix-tree/soobing.ts index d6a9e59359..6d0a72d12e 100644 --- a/implement-trie-prefix-tree/soobing.ts +++ b/implement-trie-prefix-tree/soobing.ts @@ -1,46 +1,53 @@ +// 3rd tried class TrieNode { children: Map; isEnd: boolean; - constructor() { - this.children = new Map(); - this.isEnd = false; + this.children = new Map(); + this.isEnd = false } } - class Trie { root: TrieNode; constructor() { - this.root = new TrieNode(); + this.root = new TrieNode(); } insert(word: string): void { - let node = this.root; - for (const char of word) { - if (!node.children.has(char)) { - node.children.set(char, new TrieNode()); + let node = this.root; + for(const ch of word) { + if(!node.children.has(ch)) { + node.children.set(ch, new TrieNode()); + } + node = node.children.get(ch)! } - node = node.children.get(char)!; - } - node.isEnd = true; + node.isEnd = true; } search(word: string): boolean { - const node = this._findNode(word); - return node !== null && node.isEnd; + let node = this.root; + for(const ch of word) { + if(!node.children.has(ch)) return false; + node = node.children.get(ch)!; + } + return node.isEnd; } startsWith(prefix: string): boolean { - return this._findNode(prefix) !== null; - } - - private _findNode(word: string): TrieNode | null { - let node = this.root; - for (const char of word) { - if (!node.children.has(char)) return null; - node = node.children.get(char)!; - } - return node; + let node = this.root; + for(const ch of prefix) { + if(!node.children.has(ch)) return false; + node = node.children.get(ch)!; + } + 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) +*/ diff --git a/word-break/soobing.ts b/word-break/soobing.ts index a6ed583068..f6925e063a 100644 --- a/word-break/soobing.ts +++ b/word-break/soobing.ts @@ -1,15 +1,16 @@ +// 3rd tried function wordBreak(s: string, wordDict: string[]): boolean { const dp = new Array(s.length + 1).fill(false); - dp[s.length] = true; + dp[0] = true; - for (let i = s.length - 1; i >= 0; i--) { - for (const word of wordDict) { - if (i + word.length <= s.length && s.slice(i, i + word.length) === word) { - dp[i] = dp[i + word.length]; - } - - if (dp[i]) break; + for (let i = 1; i <= s.length; i++) { + for (let j = 0; j < i; j++) { + if (dp[j] && wordDict.includes(s.slice(j, i))) { + dp[i] = true; + break; + } + } } - } - return dp[0]; -} + + return dp[s.length]; +};