You are given an array of integers stones
where stones[i]
is the weight of the ith
stone.
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x
and y
with x <= y
. The result of this smash is:
x == y
, both stones are destroyed, andx != y
, the stone of weight x
is destroyed, and the stone of weight y
has new weight y - x
.Constraints:
Example 1:
Example 2:
/**
* @param {number[]} stones
* @return {number}
*/
var lastStoneWeight = function(stones) {
if (stones.length === 1) return stones[0];
let firstStone, secondStone;
while (stones.length > 1) {
stones.sort((a, b) => b - a);
firstStone = stones[0];
secondStone = stones[1];
if (firstStone != secondStone) {
stones.push(firstStone - secondStone);
}
stones.splice(0, 2);
}
return stones[0] ? stones[0] : 0;
};
var lastStoneWeight = function(stones) {
if (stones.length === 1) return stones[0];
const heap = new MaxBinaryHeap();
stones.forEach((stone) => heap.insert(stone));
let firstStone, secondStone, diff;
while (heap.values.length > 1) {
firstStone = heap.extractMax();
secondStone = heap.extractMax();
diff = firstStone - secondStone;
if (diff != 0) {
heap.insert(diff);
}
}
return heap.values.length !== 0 ? heap.values[0] : 0;
};
nums
, you will choose two different indices i
and j
of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1)
./**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
let max = 0;
let product = 0;
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
product = (nums[i] - 1)*(nums[j] - 1);
if (product > max) {
max = product;
}
}
}
return max;
};
var maxProduct = function(nums) {
const heap = new MaxBinaryHeap();
nums.forEach((num) => heap.insert(num));
const first = heap.extractMax();
const second = heap.extractMax();
return (first - 1)*(second - 1);
};
arr
. You can choose a set of integers and remove all the occurrences of these integers in the array./**
* @param {number[]} arr
* @return {number}
*/
var minSetSize = function(arr) {
const threshold = arr.length / 2;
const numMap = new Map();
let deletedCount = 0;
let setSize = 0;
arr.forEach((num) => {
let frequency = numMap.get(num);
frequency != undefined ? numMap.set(num, frequency + 1) : numMap.set(num, 1);
});
const frequencyArray = [...numMap.values()].sort((a,b) => b - a);
for (let i = 0; i < frequencyArray.length; i++) {
deletedCount += frequencyArray[i];
if (deletedCount >= threshold) {
setSize = i + 1;
break;
}
}
return setSize;
};
var minSetSize = function(arr) {
const threshold = arr.length / 2;
const numMap = new Map();
let deletedCount = 0;
let setSize = 0;
arr.forEach((num) => {
let frequency = numMap.get(num);
frequency != undefined ? numMap.set(num, frequency + 1) : numMap.set(num, 1);
});
const heap = new MaxBinaryHeap();
for (let frequency of numMap.values()) {
heap.insert(frequency);
}
for (let i = 0; i < heap.values.length; i++) {
deletedCount += heap.extractMax();
if (deletedCount >= threshold) {
setSize = i + 1;
break;
}
}
return setSize;
};
s
, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.s.length
<= 5 * 10^5s
consists of uppercase and lowercase English letters and digits./**
* @param {string} s
* @return {string}
*/
var frequencySort = function(s) {
const strMap = new Map();
let str;
let node;
let result = '';
for (let i = 0; i < s.length; i++) {
str = strMap.get(s[i]);
str != undefined ? strMap.set(s[i], str + 1) : strMap.set(s[i], 1);
}
const heap = new MaxBinaryHeap();
for (let [value, frequency] of strMap.entries()) {
heap.insert(value, frequency);
}
const length = heap.values.length;
for (let i = 0; i < length; i++) {
node = heap.extractMax();
result += node.value.repeat(node.frequency);
}
return result;
};
class Node {
constructor(value, frequency) {
this.value = value;
this.frequency = frequency;
}
}
class MaxBinaryHeap {
constructor() {
this.values = [];
}
insert(value, frequency) {
const node = new Node(value, frequency);
this.values.push(node);
this.bubbleUp();
}
bubbleUp() {
let idx = this.values.length - 1;
const element = this.values[idx];
let parentIdx;
let parent;
while(idx > 0) {
parentIdx = Math.floor((idx - 1)/2);
parent = this.values[parentIdx];
if (element.frequency <= parent.frequency) break;
this.values[parentIdx] = element;
this.values[idx] = parent;
idx = parentIdx;
}
}
extractMax() {
const max = this.values[0];
const end = this.values.pop();
if (this.values.length > 0) {
this.values[0] = end;
this.sinkDown();
}
return max;
}
sinkDown() {
let idx = 0;
const length = this.values.length;
const element = this.values[0];
let leftChildIdx;
let rightChildIdx;
let leftChild;
let rightChild;
let swap;
while(true) {
leftChildIdx = 2 * idx + 1;
rightChildIdx = 2 * idx + 2;
swap = null;
if (leftChildIdx < length) {
leftChild = this.values[leftChildIdx];
if (leftChild.frequency > element.frequency) {
swap = leftChildIdx;
}
}
if (rightChildIdx < length) {
rightChild = this.values[rightChildIdx];
if (
(swap === null && rightChild.frequency > element.frequency) ||
(swap !== null && rightChild.frequency > leftChild.frequency)
) {
swap = rightChildIdx;
}
}
if (swap === null) break;
this.values[idx] = this.values[swap];
this.values[swap] = element;
idx = swap;
}
}
}
명령어 | 수신 탑(높이) |
---|---|
I 숫자 | 큐에 주어진 숫자를 입력합니다. |
D 1 | 큐에서 최댓값을 삭제합니다. |
D -1 | 큐에서 최솟값을 삭제합니다. |
이중 우선순위 큐가 할 연산 operations
가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0]
비어있지 않으면 [최댓값, 최솟값]
을 return 하도록 solution 함수를 구현해주세요.
제한사항
operations
는 길이가 1 이상 1,000,000 이하인 문자열 배열입니다.operations
의 원소는 큐가 수행할 연산을 나타냅니다.입출력 예 1
입출력 예 2
function solution(operations) {
const heap = new MaxBinaryHeap();
operations.forEach((operation) => {
let [command, value] = operation.split(' ');
value = Number(value);
if (command === 'I') {
heap.insert(value);
return;
}
if (command === 'D') {
if (!heap.values.length) {
return;
}
if (value === 1) {
heap.extractMax();
return;
}
if (value === -1) {
heap.extractMin();
return;
}
}
});
if (!heap.values.length) {
return [0, 0];
}
const max = heap.extractMax();
const min = heap.extractMin();
return [max, min];
}
class MaxBinaryHeap {
constructor() {
this.values = [];
}
getSize() {
return this.values.length;
}
insert(element) {
this.values.push(element);
this.bubbleUp();
}
bubbleUp() {
let idx = this.values.length - 1;
const element = this.values[idx];
let parentIdx;
let parent;
while(idx > 0) {
parentIdx = Math.floor((idx - 1)/2);
parent = this.values[parentIdx];
if (element <= parent) break;
this.values[parentIdx] = element;
this.values[idx] = parent;
idx = parentIdx;
}
}
extractMax() {
const max = this.values[0];
const end = this.values.pop();
if (this.values.length > 0) {
this.values[0] = end;
this.sinkDown();
}
return max;
}
extractMin() {
const size = this.getSize();
const middleIndex = Math.floor(size/2);
let min = this.values[middleIndex];
let minIndex = middleIndex;
let currValue;
for (let i = middleIndex + 1; i < size; i++) {
currValue = this.values[i];
if (currValue < min) {
min = currValue;
minIndex = i;
}
}
this.values.splice(minIndex, 1);
return min;
}
sinkDown() {
let idx = 0;
const length = this.values.length;
const element = this.values[0];
let leftChildIdx;
let rightChildIdx;
let leftChild;
let rightChild;
let swap;
while(true) {
leftChildIdx = 2 * idx + 1;
rightChildIdx = 2 * idx + 2;
swap = null;
if (leftChildIdx < length) {
leftChild = this.values[leftChildIdx];
if (leftChild > element) {
swap = leftChildIdx;
}
}
if (rightChildIdx < length) {
rightChild = this.values[rightChildIdx];
if (
(swap === null && rightChild > element) ||
(swap !== null && rightChild > leftChild)
) {
swap = rightChildIdx;
}
}
if (swap === null) break;
this.values[idx] = this.values[swap];
this.values[swap] = element;
idx = swap;
}
}
}