From ee8c5443d4d909aa1e2be052978a85b382a5c13c Mon Sep 17 00:00:00 2001 From: "sgh4123@naver.com" Date: Sun, 12 May 2024 14:12:25 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[PGS]=20=EC=A0=95=EC=88=98=20=EC=82=BC?= =?UTF-8?q?=EA=B0=81=ED=98=95=20/=20Level=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ .../dynamic_programming/integertriangle.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 .gitignore create mode 100644 SeoGeonhyuk/dynamic_programming/integertriangle.js 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; +} From 24889d92d2eefb216fef777449beebb5320bcd6f Mon Sep 17 00:00:00 2001 From: "sgh4123@naver.com" Date: Fri, 17 May 2024 21:53:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[PGS]=20=EB=8D=94=20=EB=A7=B5=EA=B2=8C=20/?= =?UTF-8?q?=20level=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SeoGeonhyuk/heap/morehot.js | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 SeoGeonhyuk/heap/morehot.js 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; +}