-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #876 from yeeZinu/main
[호돌이] Week5
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
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,21 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function (prices) { | ||
// 초기 값 | ||
let buy = prices[0]; | ||
// 차액 | ||
let diff = 0; | ||
|
||
for (let i = 1; i < prices.length; i++) { | ||
// 구매가보다 현재가격이 더 싸면 구매가로 변경 | ||
if (buy > prices[i]) { | ||
buy = prices[i]; | ||
} | ||
// 차액 계산, 누가더 큰지 비교 | ||
diff = Math.max(diff, (prices[i] - buy)); | ||
|
||
} | ||
return diff | ||
}; |
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,23 @@ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function (strs) { | ||
// 정답 객체 | ||
let ans = {}; | ||
|
||
for (let s of strs) { | ||
// strs 배열에서 받아온 s를 하나씩 쪼개서 정렬, 다시 하나로 뭉침 | ||
let key = s.split('').sort().join(''); | ||
|
||
// 만약 정답 객체에 현재 단어가 없다? | ||
if (!ans[key]) { | ||
// 해당 값을 빈 배열로 초기화 | ||
ans[key] = []; | ||
} | ||
// 해당 값 배열에 초기 단어 추가. | ||
ans[key].push(s); | ||
} | ||
// 객체에 값을 추가한 것이기 때문에 Object.value()를 사용해서 열거 가능한 배열로 리턴 | ||
return Object.values(ans); | ||
}; |
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,82 @@ | ||
// trieNode 클래스 선언 | ||
// child = {}, end = false 로 초기화 | ||
class TrieNode { | ||
constructor(child = {}, end = false) { | ||
this.child = child; | ||
this.end = end; | ||
} | ||
} | ||
|
||
// Trie함수의 root는 TrieNode의 객체 | ||
var Trie = function() { | ||
this.root = new TrieNode(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
// 단어 삽입 | ||
Trie.prototype.insert = function(word) { | ||
// 현재 = 최상단으로 초기화 | ||
let current = this.root; | ||
|
||
// 단어를 반복하면서 없으면 TrieNode에 추가 | ||
for (const char of word) { | ||
if (!current.child[char]) { | ||
current.child[char] = new TrieNode(); | ||
} | ||
current = current.child[char]; | ||
} | ||
|
||
// 반복이 끝나면 end true | ||
current.end = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
// 단어 탐색 | ||
Trie.prototype.search = function(word) { | ||
// 현재위치 = 최상단으로 | ||
let current = this.root; | ||
|
||
// 반복하면서 단어찾기 | ||
for(const char of word) { | ||
if(current.child[char]) { | ||
current = current.child[char]; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
return current.end; | ||
|
||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
let current = this.root; | ||
for (const char of prefix) { | ||
if (current.child[char]) { | ||
current = current.child[char]; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
|
||
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) | ||
*/ |