Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions leetcode3/황은지/1046. Last Stone Weight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// class MaxHeap {
// constructor() {
// this.heap = [];
// }

// getParentIndex(idx) {
// return Math.floor((idx - 1) / 2);
// }

// getRightChildIndex(idx) {
// return idx * 2 + 2;
// }

// getLeftChildIndex(idx) {
// return idx * 2 + 1;
// }

// hasParent(idx) {
// return this.getParentIndex(idx) >= 0;
// }

// size(){
// return this.heap.length;
// }
// add(val) {
// this.heap.push(val);
// this.heapifyUp();
// }

// remove() {
// if (this.heap.length === 0) return null;

// const top = this.heap[0];
// this.heap[0] = this.heap[this.heap.length - 1];
// this.heap.pop();
// this.heapifyDown();
// return top;
// }

// heapifyUp(val) {
// let idx = this.heap.length - 1;
// while (this.hasParent(idx)) {
// let parentIdx = getParentIndex(idx);
// if (this.heap[idx] > this.heap[parentIdx]) {
// this.swap(idx, parentIdx);
// idx = parentIdx;
// } else {
// break;
// }
// }
// }

// heapifyDown() {
// let idx = 0;
// while (this.getLeftChildIndex(idx) > 0) {
// let largerIdx = this.getLeftChildIndex(idx);
// if (
// this.getRightChildIndex(idx) < this.heap.length &&
// this.heap[this.getRightChildIndex(idx)] < this.heap[largerIdx]
// ) {
// largerIdx = this.getRightChildIndex(idx);
// }
// if (this.heap[largerIdx] > this.heap[idx]) {
// this.swap(largerIdx, idx);
// idx = largerIdx;
// } else {
// break;
// }
// }
// }

// swap(a, b) {
// [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
// }
// }

/**
* @param {number[]} stones
* @return {number}
*/
var lastStoneWeight = function (stones) {
const maxHeap = new MaxHeap();
for (const stone of stones) {
maxHeap.push(stone);
}

while (maxHeap.size() > 1) {
const num1 = maxHeap.pop();
const num2 = maxHeap.pop();
if (num1 === num2) continue;
maxHeap.push(Math.abs(num1 - num2));
}
if (maxHeap.size() === 0) return 0;
return maxHeap.pop();
};
8 changes: 8 additions & 0 deletions leetcode3/황은지/838. Push Dominoes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @param {string} dominoes
* @return {string}
*/
var pushDominoes = function (dominoes) {
// 움직이는 것들 사이를 기점으로 바꿔진다.
// 앞에서 하면 반대쪽을 기점으로 보지못한다. 투포인터로 해야되나..?
};
Loading