diff --git a/best-time-to-buy-and-sell-stock/yeeZinu.js b/best-time-to-buy-and-sell-stock/yeeZinu.js new file mode 100644 index 000000000..4a9224a87 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/yeeZinu.js @@ -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 +}; diff --git a/group-anagrams/yeeZinu.js b/group-anagrams/yeeZinu.js new file mode 100644 index 000000000..ff36ea942 --- /dev/null +++ b/group-anagrams/yeeZinu.js @@ -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); +}; diff --git a/implement-trie-prefix-tree/yeeZinu.js b/implement-trie-prefix-tree/yeeZinu.js new file mode 100644 index 000000000..9daaf5f3b --- /dev/null +++ b/implement-trie-prefix-tree/yeeZinu.js @@ -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) +*/