diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31ca0cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +SeoGeonhyuk/.DS_Store \ No newline at end of file diff --git a/SeoGeonhyuk/dynamic_programming/integertriangle.js b/SeoGeonhyuk/dynamic_programming/integertriangle.js new file mode 100644 index 0000000..131af6c --- /dev/null +++ b/SeoGeonhyuk/dynamic_programming/integertriangle.js @@ -0,0 +1,27 @@ +let answer = 0; +function maxPathCalculator(floorArray, arrays){ + if(arrays.length === 0){ + floorArray.forEach((num) => { + if (answer <= num) + answer = num; + }) + } + else{ + const partArray = arrays.shift(); + const futureFloorArray = new Array(partArray.length).fill(0); + for(let i = 0; i < floorArray.length; i++){ + const possiblePathSum = floorArray[i] + partArray[i]; + if(possiblePathSum > futureFloorArray[i]) + futureFloorArray[i] = possiblePathSum; + if(i + 1 < partArray.length) + futureFloorArray[i + 1] = (floorArray[i] + partArray[i + 1]); + } + maxPathCalculator(futureFloorArray, arrays); + } +} +function solution(triangle) { + const oldSum = triangle[0][0]; + triangle.shift(); + maxPathCalculator([oldSum], triangle); + return answer; +} diff --git a/SeoGeonhyuk/heap/morehot.js b/SeoGeonhyuk/heap/morehot.js new file mode 100644 index 0000000..e67c71a --- /dev/null +++ b/SeoGeonhyuk/heap/morehot.js @@ -0,0 +1,88 @@ +class MinHeap { + constructor() { + this.heap = []; + } + + getParentIndex(index) { + return Math.floor((index - 1) / 2); + } + + getLeftChildIndex(index) { + return index * 2 + 1; + } + + getRightChildIndex(index) { + return index * 2 + 2; + } + + insert(value) { + this.heap.push(value); + this.heapifyUp(); + } + + heapifyUp() { + let index = this.heap.length - 1; + while (index > 0) { + let parentIndex = this.getParentIndex(index); + if (this.heap[parentIndex] > this.heap[index]) { + [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]; + index = parentIndex; + } else { + break; + } + } + } + + extractMin() { + if (this.heap.length === 1) return this.heap.pop(); + const min = this.heap[0]; + this.heap[0] = this.heap.pop(); + this.heapifyDown(); + return min; + } + + heapifyDown() { + let index = 0; + while (this.getLeftChildIndex(index) < this.heap.length) { + let smallerChildIndex = this.getLeftChildIndex(index); + if (this.getRightChildIndex(index) < this.heap.length && this.heap[this.getRightChildIndex(index)] < this.heap[smallerChildIndex]) { + smallerChildIndex = this.getRightChildIndex(index); + } + if (this.heap[index] > this.heap[smallerChildIndex]) { + [this.heap[index], this.heap[smallerChildIndex]] = [this.heap[smallerChildIndex], this.heap[index]]; + index = smallerChildIndex; + } else { + break; + } + } + } + + size() { + return this.heap.length; + } + + peek() { + return this.heap[0]; + } +} + +function solution(scoville, K) { + const minHeap = new MinHeap(); + scoville.forEach(num => minHeap.insert(num)); + + let answer = 0; + + while (minHeap.size() > 1 && minHeap.peek() < K) { + let first = minHeap.extractMin(); + let second = minHeap.extractMin(); + let newScoville = first + (second * 2); + minHeap.insert(newScoville); + answer++; + } + + if (minHeap.peek() < K) { + return -1; + } + + return answer; +}