diff --git a/best-time-to-buy-and-sell-stock/anniemon.js b/best-time-to-buy-and-sell-stock/anniemon.js new file mode 100644 index 000000000..4247a6e1f --- /dev/null +++ b/best-time-to-buy-and-sell-stock/anniemon.js @@ -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; +}; diff --git a/encode-and-decode-strings/anniemon.js b/encode-and-decode-strings/anniemon.js new file mode 100644 index 000000000..972311b4b --- /dev/null +++ b/encode-and-decode-strings/anniemon.js @@ -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; + } +} diff --git a/group-anagrams/anniemon.js b/group-anagrams/anniemon.js new file mode 100644 index 000000000..06e03b731 --- /dev/null +++ b/group-anagrams/anniemon.js @@ -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()); +}; diff --git a/implement-trie-prefix-tree/anniemon.js b/implement-trie-prefix-tree/anniemon.js new file mode 100644 index 000000000..99a77f244 --- /dev/null +++ b/implement-trie-prefix-tree/anniemon.js @@ -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) +*/