-
-
Notifications
You must be signed in to change notification settings - Fork 340
[soobing] WEEK 05 Solutions #2510
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
base: main
Are you sure you want to change the base?
Changes from all commits
6aadb4b
61e5e54
7161650
58b8612
7f20e49
23df2b7
9a45a0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,11 @@ | ||
| // idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record<string, string[]> 에 맞게 매핑하여 values들만 리턴하면 될것 같음 | ||
| // 3rd tried | ||
| function groupAnagrams(strs: string[]): string[][] { | ||
| const map = new Map<string, string[]>(); | ||
| 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()); | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,53 @@ | ||
| // 3rd tried | ||
| class TrieNode { | ||
| children: Map<string, TrieNode>; | ||
| 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) | ||
| */ |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석