-
-
Notifications
You must be signed in to change notification settings - Fork 247
[soobing] WEEK05 Solutions #1843
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
Open
soobing
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
soobing:week5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* 문제 유형 | ||
* - Array | ||
* | ||
* 문제 설명 | ||
* - 주식을 가장 싸게 사서 비싸게 팔수 있는 경우 찾기 | ||
* | ||
* 아이디어 | ||
* 1) 최소값을 찾고 그 이후의 값 중 최대값을 찾는다. | ||
* | ||
*/ | ||
function maxProfit(prices: number[]): number { | ||
let min = prices[0]; | ||
let maxProfit = 0; | ||
|
||
for (let i = 1; i < prices.length; i++) { | ||
min = Math.min(min, prices[i]); | ||
maxProfit = Math.max(prices[i] - min, maxProfit); | ||
} | ||
return maxProfit; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 문제 유형 | ||
* - String | ||
* | ||
* 문제 설명 | ||
* - 문자열 인코딩과 디코딩 | ||
* | ||
* 아이디어 | ||
* 1) "길이 + # + 문자열" 형태로 인코딩 | ||
* | ||
*/ | ||
class Solution { | ||
/** | ||
* @param {string[]} strs | ||
* @returns {string} | ||
*/ | ||
encode(strs) { | ||
return strs.map((str) => `${str.length}#${str}`).join(""); | ||
} | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string[]} | ||
*/ | ||
decode(str) { | ||
const result = []; | ||
let tempStr = str; | ||
while (tempStr.length) { | ||
let i = 0; | ||
|
||
while (tempStr[i] !== "#") { | ||
i++; | ||
} | ||
|
||
const length = Number(tempStr.slice(0, i)); | ||
const currentStr = tempStr.slice(i + 1, i + 1 + length); | ||
result.push(currentStr); | ||
tempStr = tempStr.slice(length + i + 1); | ||
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. slice로 원본을 잘라내면 불필요한 문자열 복사가 반복될 수 있으니 포인터를 만들어서 전진하는 방식도 고려해보면 좋을 것 같아요 |
||
} | ||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 문제 유형 | ||
* - String | ||
* | ||
* 문제 설명 | ||
* - 애너그램 그룹화 | ||
* | ||
* 아이디어 | ||
* 1) 각 문자열을 정렬하여 키로 사용하고 그 키에 해당하는 문자열 배열을 만들어 리턴 | ||
* | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const map = new Map<string, string[]>(); | ||
|
||
for (let i = 0; i < strs.length; i++) { | ||
const key = strs[i].split("").sort().join(""); | ||
map.set(key, [...(map.get(key) ?? []), strs[i]]); | ||
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. map.set()은 매번 새 배열을 만들어야하니 꺼내서 push 하는 방식은 어떤가요? |
||
} | ||
return [...map.values()]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 문제 유형 | ||
* - Trie 구현 (문자열 검색) | ||
* | ||
* 문제 설명 | ||
* - 문자열 검색/추천/자동완성에서 자주 사용하는 자료구조 Trie 구현 | ||
* | ||
* 아이디어 | ||
* 1) 문자열의 각 문자를 TrieNode 클래스의 인스턴스로 표현s | ||
* | ||
*/ | ||
class TrieNode { | ||
children: Map<string, TrieNode>; | ||
isEnd: boolean; | ||
|
||
constructor() { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
root: TrieNode; | ||
constructor() { | ||
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()); | ||
} | ||
node = node.children.get(char)!; | ||
} | ||
node.isEnd = true; | ||
} | ||
|
||
// isEnd 까지 확인이 필요 | ||
search(word: string): boolean { | ||
const node = this._findNode(word); | ||
return node !== null && node.isEnd; | ||
} | ||
|
||
// 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.get(char)) return null; | ||
node = node.children.get(char)!; | ||
} | ||
|
||
return node; | ||
} | ||
} | ||
|
||
/** | ||
* 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) | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
minPrice 처럼 의미가 분명한 변수명을 사용하면 좋을 것 같습니다.