-
Notifications
You must be signed in to change notification settings - Fork 126
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
[YeomChaeeun] Week 5 #869
Merged
Merged
[YeomChaeeun] Week 5 #869
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da5a7d5
feat: best-time-to-buy-and-sell-stock solution
YeomChaeeun ee2c434
feat: group-anagrams solution
YeomChaeeun 88796e9
feat: word-break solution
YeomChaeeun 061b78c
feat: implement-trie-prefix-tree solution
YeomChaeeun 7a9220a
fix: log 주석 처리
YeomChaeeun 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 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,19 @@ | ||
/** | ||
* 최대 수익을 구하는 알고리즘 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(1) | ||
* @param prices | ||
*/ | ||
function maxProfit(prices: number[]): number { | ||
let min = prices[0] | ||
let total = 0 | ||
|
||
for(let i = 1 ; i < prices.length ; i++) { | ||
min = Math.min(min, prices[i]) | ||
// console.log(dp[i],'===', dp[i-1], '===', prices[i]) | ||
total = Math.max(total, prices[i] - min) | ||
} | ||
|
||
return total | ||
} |
This file contains 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,24 @@ | ||
/** | ||
* 애너그램 그룹화하기 | ||
* n - 입력 문자열 배열의 길이 | ||
* k - 가장 긴 문자열의 길이 | ||
* | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: n * k * logk (sort가 klogk 시간 소요) | ||
* - 공간 복잡도: n * k | ||
* @param strs | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
let group = {} | ||
|
||
for(const s of strs) { | ||
let key = s.split('').sort().join(''); | ||
if(!group[key]) { | ||
group[key] = [] | ||
} | ||
group[key].push(s) | ||
} | ||
console.log(group) | ||
|
||
return Object.values(group) | ||
} |
This file contains 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,54 @@ | ||
/** | ||
* 트라이 알고리즘 | ||
* 달레 해설을 보고 참고하여 TypeScript로 작성 | ||
*/ | ||
class Node { | ||
children: { [key: string]: Node }; | ||
ending: boolean; | ||
|
||
constructor(ending: boolean = false) { | ||
this.children = {}; | ||
this.ending = ending; | ||
} | ||
} | ||
|
||
class Trie { | ||
private root: Node; | ||
|
||
constructor() { | ||
this.root = new Node(true); | ||
} | ||
|
||
insert(word: string): void { | ||
let node = this.root; | ||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
node.children[ch] = new Node(); | ||
} | ||
node = node.children[ch]; | ||
} | ||
node.ending = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let node = this.root; | ||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
node = node.children[ch]; | ||
} | ||
return node.ending; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
let node = this.root; | ||
for (const ch of prefix) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
node = node.children[ch]; | ||
} | ||
return true; | ||
} | ||
} |
This file contains 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,46 @@ | ||
/** | ||
* 주어진 단어가 wordDict에 있는 단어들로 분할 가능한지 확인하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(nmk) => 문자열 s의 길이 * wordDict 단어 개수 * wordDict 내 가장 긴 단어의 길이 | ||
* - 공간 복잡도: O(n) | ||
* @param s | ||
* @param wordDict | ||
*/ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
// 접근 1 - 단어 포함 여부만 고려하고 여러번 사용될 있고 연속성 검사를 하지 않았음 | ||
// let len = 0; | ||
// for(const word of wordDict) { | ||
// len += word.length | ||
// if(!s.includes(word)) return false | ||
// } | ||
// if(s.length < len) return false | ||
// return true | ||
|
||
/* 해당 케이스에서 에러 발생함 | ||
* - 단어가 여러번 사용될 수 있기 때문 | ||
* s="bb" | ||
* wordDict = ["a","b","bbb","bbbb"] | ||
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. 👍 |
||
*/ | ||
|
||
// 접근 2- dp 알고리즘 적용 | ||
// 각 위치까지의 문자열이 분할 가능한지 저장함 | ||
let dp: boolean[] = new Array(s.length).fill(false) | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
for (let word of wordDict) { | ||
// 조건 처리 - 현재 위치가 단어 길이보다 작으면 스킵함 | ||
if (i < word.length - 1) { | ||
continue | ||
} | ||
// 현재 위치의 유효성 체크 - 첫번째 단어이거나 이전 위치까지 분할 가능한지 | ||
if (i == word.length - 1 || dp[i - word.length]) { | ||
// 현재 위치의 단어 매칭 체크 | ||
if (s.substring(i - word.length + 1, i + 1) == word) { | ||
dp[i] = true | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return dp[s.length - 1] | ||
} |
Oops, something went wrong.
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.
중요하지 않은 사항입니다
요 부분 주석처리만 빠져있네요! 🧹