diff --git a/container-with-most-water/hozzijeong.js b/container-with-most-water/hozzijeong.js new file mode 100644 index 0000000000..dd808b7b9d --- /dev/null +++ b/container-with-most-water/hozzijeong.js @@ -0,0 +1,36 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + // x축이 가장 길면서 y축 역시 가장 높은 즉 높이가 가장 높은 값을 찾으라는 의미. + // startIndex값과 그 값에 해당하는 height를 받고 + // endIndex의 값과 그 값에 해당하는 height를 받는다. + // 여기서 가장 큰 점은 startIndex와 endIndex가 멀면 멀수록 좋다 + // 그리고 그 값을 하나씩 줄여가면서 더 큰 값을 찾아 적용한다 + + let max = 0; + + let startIndex = 0; + let endIndex = height.length -1; + + while(startIndex < endIndex){ + const startHeight = height[startIndex]; + const endHeight = height[endIndex]; + + const xLength = endIndex - startIndex; + const minHeight = Math.min(startHeight, endHeight); + + const size = xLength * minHeight; + + max = Math.max(size,max); + + if(startHeight >= endHeight){ + endIndex -=1; + }else{ + startIndex +=1; + } + }; + + return max +}; diff --git a/design-add-and-search-words-data-structure/hozzijeong.js b/design-add-and-search-words-data-structure/hozzijeong.js new file mode 100644 index 0000000000..78223731df --- /dev/null +++ b/design-add-and-search-words-data-structure/hozzijeong.js @@ -0,0 +1,39 @@ + +var WordDictionary = function() { + const dictionary = new Set(''); + + this.dictionary = dictionary +}; + +/** + * @param {string} word + * @return {void} + */ +WordDictionary.prototype.addWord = function(word) { + this.dictionary.add(word); +}; + +/** + * @param {string} word + * @return {boolean} + */ +WordDictionary.prototype.search = function(word) { + if(word.includes('.')){ + const dictionaryList = [...this.dictionary]; + + return dictionaryList.some((dic) => { + if(dic.length !== word.length) return false; + + return [...dic].every((char, index) => word[index] === '.' ? true : word[index] === char) + }) + }; + + return this.dictionary.has(word) +}; + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */ diff --git a/reverse-linked-list/hozzijeong.js b/reverse-linked-list/hozzijeong.js new file mode 100644 index 0000000000..fd812b56fc --- /dev/null +++ b/reverse-linked-list/hozzijeong.js @@ -0,0 +1,47 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + let prev = null; // 1 | + let current = head; // [null, 2] | [] + let next = null // 2 | 3 + + // prev = null + // current = 1 + // next =2 + + // prev = 1 + // current = 2 + // next = 3 + + // prev = 2 + // current = 3 + // next = 4 + + // prev = 3 + // current = 4 + // next = 5 + + // prev = 4 + // current = 5 + // next = null + + while(current != null){ + next = current.next; + current.next = prev + prev = current; + current = next; + } + + head = prev + return head +}; + diff --git a/valid-parentheses/hozzijeong.js b/valid-parentheses/hozzijeong.js new file mode 100644 index 0000000000..caae0603ab --- /dev/null +++ b/valid-parentheses/hozzijeong.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + const smallBracket = ['(',')']; + const middleBracket = ['{','}']; + const largeBracket = ['[',']']; + + + const leftStack = []; + + for(const char of s) { + if(char === smallBracket[0]){ + leftStack.push(smallBracket[1]) + }else if (char === middleBracket[0]){ + leftStack.push(middleBracket[1]) + }else if(char === largeBracket[0]){ + leftStack.push(largeBracket[1]) + }else{ + const last = leftStack.pop(); + if(char === smallBracket[1] && last !== char) return false; + if(char === middleBracket[1] && last !== char) return false; + if(char === largeBracket[1] && last !== char) return false; + } + } + + + return leftStack.length === 0 +};