-
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 #871 from anniemon/main
[anniemon78] Week5
- Loading branch information
Showing
4 changed files
with
139 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,18 @@ | ||
/** | ||
* 시간 복잡도: prices.length만큼 순회하므로 O(n) | ||
* 공간 복잡도: 상수 크기의 변수만 사용하므로 O(1) | ||
*/ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function (prices) { | ||
let maxProfit = 0; | ||
let min = prices[0]; | ||
|
||
for (let i = 0; i < prices.length; i++) { | ||
maxProfit = Math.max(maxProfit, prices[i] - min); | ||
min = Math.min(min, prices[i]); | ||
} | ||
return maxProfit; | ||
}; |
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,37 @@ | ||
class Solution { | ||
/** | ||
* 시간 복잡도: strs의 길이만큼 순회하므로, O(n) | ||
* 공간 복잡도: 결괏값 제외 추가 변수 사용 없으므로 O(1) | ||
*/ | ||
/** | ||
* @param {string[]} strs | ||
* @returns {string} | ||
*/ | ||
encode(strs) { | ||
return strs.reduce((acc, cur) => acc+ `${cur.length}` + '!' + cur, ''); | ||
} | ||
|
||
/** | ||
* 시간 복잡도: str의 길이만큼 순회하므로 str의 길이가 m이면, O(m) | ||
* 공간 복잡도: 결괏값 제외 상수 크기 변수만 사용하므로, O(1) | ||
*/ | ||
/** | ||
* @param {string} str | ||
* @returns {string[]} | ||
*/ | ||
decode(str) { | ||
const res = []; | ||
let i = 0; | ||
while (i < str.length) { | ||
let j = i; | ||
while (str[j] !== '!') { | ||
j++; | ||
} | ||
const len = Number(str.slice(i, j)); | ||
const s = str.slice(j + 1, j + 1 + len); | ||
res.push(s); | ||
i = j + 1 + len; | ||
} | ||
return res; | ||
} | ||
} |
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 @@ | ||
/** | ||
* 시간 복잡도: | ||
* 정렬 작업은 각 문자열의 길이가 m일 때 O(m logm)이고, 총 strs의 길이만큼 수행되므로 | ||
* 시간 복잡도는 O(n * mlogm) | ||
* 공간 복잡도: | ||
* Map 키는 최대 길이 m인 문자열 strs.length개이다. | ||
* 따라서 공간 복잡도는 O(n * m) | ||
*/ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const map = new Map(); | ||
for(const s of strs) { | ||
const key = s.split('').sort().join(''); | ||
if(!map.has(key)) { | ||
map.set(key, []) | ||
} | ||
map.get(key).push(s); | ||
} | ||
return Array.from(map.values()); | ||
}; |
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,61 @@ | ||
/** | ||
* 시간 복잡도: 주어진 문자열의 길이만큼 순회하므로, O(n) | ||
* 공간 복잡도: 삽입된 모든 문자열의 길이만큼 노드가 만들어지므로, 이를 m이라고 하면 O(m) | ||
*/ | ||
var Trie = function() { | ||
this.isEnd = false; | ||
this.children = {}; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
let cur = this.children; | ||
for(const c of word) { | ||
if(!cur[c]) { | ||
cur[c] = { isEnd: false, children: {} }; | ||
} | ||
cur = cur[c]; | ||
} | ||
cur.isEnd = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
let cur = this.children; | ||
for(const c of word) { | ||
if(!cur[c]) { | ||
return false; | ||
} | ||
cur = cur[c] | ||
} | ||
return cur.isEnd; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
let cur = this.children; | ||
for(const c of prefix) { | ||
if(!cur[c]){ | ||
return false; | ||
} | ||
cur = cur[c]; | ||
} | ||
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) | ||
*/ |