HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
👻
개발 기록
/
코딩테스트 스터디
코딩테스트 스터디
/
Stock Price Fluctuation

Stock Price Fluctuation

Link
https://leetcode.com/problems/stock-price-fluctuation/
Deadline
Feb 12, 2022
Status
Archived
Type
Hash Table
heap
You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.
Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.
Design an algorithm that:
  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.
Implement the StockPrice class:
  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.
Example 1:
Input ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"] [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []] Output [null, null, null, 5, 10, null, 5, null, 2] Explanation StockPrice stockPrice = new StockPrice(); stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10]. stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5]. stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5. stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1. stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3. // Timestamps are [1,2] with corresponding prices [3,5]. stockPrice.maximum(); // return 5, the maximum price is 5 after the correction. stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2]. stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.
Constraints:
  • 1 <= timestamp, price <= 109
  • At most 105 calls will be made in total to update, current, maximum, and minimum.
  • current, maximum, and minimum will be called only after update has been called at least once.

풀이

은찬
class MaxHeap { constructor(){ this.queue = [0]; this.size = 0; } push(value) { this.queue.push(value); this.size++; this.moveUp(this.size); } moveUp(index){ while(Math.floor(index / 2)){ const parent = Math.floor(index / 2); if(this.queue[parent][1] < this.queue[index][1]){ const tmp = this.queue[index]; this.queue[index] = this.queue[parent]; this.queue[parent] = tmp; } index = parent; } } pop() { const maxValue = this.queue[1]; this.queue[1] = this.queue[this.size]; this.queue.pop(); this.size--; this.moveDown(1); return maxValue; } moveDown(index) { while(index * 2 <= this.size){ const maxChildIndex = this.findMaxChildIndex(index); if(this.queue[index][1] < this.queue[maxChildIndex][1]){ const tmp = this.queue[maxChildIndex]; this.queue[maxChildIndex] = this.queue[index]; this.queue[index] = tmp; } index = maxChildIndex; } } findMaxChildIndex(index) { const left = index * 2; const right = (index * 2) + 1; if(right > this.size){ return left; } else{ if(this.queue[right][1] > this.queue[left][1]){ return right; } else{ return left; } } } } class MinHeap { constructor(){ this.queue = [0]; this.size = 0; } push(value) { this.queue.push(value); this.size++; this.moveUp(this.size); } moveUp(index){ while(Math.floor(index / 2)){ const parent = Math.floor(index / 2); if(this.queue[parent][1] > this.queue[index][1]){ const tmp = this.queue[index]; this.queue[index] = this.queue[parent]; this.queue[parent] = tmp; } index = parent; } } pop() { const minValue = this.queue[1]; this.queue[1] = this.queue[this.size]; this.queue.pop(); this.size--; this.moveDown(1); return minValue; } moveDown(index) { while(index * 2 <= this.size){ const minChildIndex = this.findMinChildIndex(index); if(this.queue[index][1] > this.queue[minChildIndex][1]){ const tmp = this.queue[minChildIndex]; this.queue[minChildIndex] = this.queue[index]; this.queue[index] = tmp; } index = minChildIndex; } } findMinChildIndex(index) { const left = index * 2; const right = (index * 2) + 1; if(right > this.size){ return left; } else{ if(this.queue[right][1] < this.queue[left][1]){ return right; } else{ return left; } } } } class StockPrice { constructor(){ this.stock = new Map(); this.max = new MaxHeap(); this.min = new MinHeap(); this.curr = 0; } update(timestamp, price){ const stock = [timestamp, price] this.max.push(stock); this.min.push(stock); this.stock.set(timestamp, price); this.curr = this.curr < timestamp ? timestamp : this.curr; } current() { return this.stock.get(this.curr); } maximum() { let max = this.max.queue[1]; while(max[1] !== this.stock.get(max[0])){ this.max.pop(); max = this.max.queue[1]; } return max[1]; } minimum() { let min = this.min.queue[1]; while(min[1] !== this.stock.get(min[0])){ this.min.pop(); min = this.min.queue[1]; } return min[1]; } };