算法打卡第四天

644 阅读2分钟

今日打卡题目:2034. 股票价格波动

题目描述

给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。
不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。
请你设计一个算法,实现:
更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。
找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。
找到当前记录里股票的 最高价格 。
找到当前记录里股票的 最低价格 。

又是没写出来的一天,解答思路不难,主要是运算速度,一直超时,难受。主要是用了许多的for循环进行计算,适量减少就能通过了。下面学习波大佬的解答思路。

思路:

  1. 存在映射关系,因此用MAP进行存储;
  2. 查找当前价格就是查找时间最大值对应的价格;
  3. 查找最大值或者最小值可以将价格进行排序,取第一个和最后一个即可。

大佬解答代码一

class StockPrice {
  constructor() {
    // 当前的时间、价格
    this.time = 0;
    this.price = 0;
    this.timePrice = new Map();
  }
  update(timestamp, price) {
    this.timePrice.set(timestamp, price);
    // 若当前输入的时间大于等于存的时间
    // 说明是新的时间,而不是纠错的时间
    if (this.time <= timestamp) {
      this.time = timestamp;
      this.price = price;
    }
  }
  current() {
    return this.price;
  }
  maximum() {
    let max = 0;
    for (const price of this.timePrice.values()) {
      if (max < price) max = price;
    }
    return max;
  }
  minimum() {
    let min = Infinity;
    for (const price of this.timePrice.values()) {
      if (min > price) min = price;
    }
    return min;
  }
}

//作者:lzxjack
//链接:https://leetcode.cn/problems/stock-price-fluctuation/solution/mapbao-cun-shi-ke-jie-ge-javascript-by-l-9pjx/
//来源:力扣(LeetCode)

大佬解答代码二

var StockPrice = function() {
    this.maxTime = 0
    this.timeMap = new Map()
    this.maxPrice = new PriorityQueue((a, b)=>a[0] - b[0] > 0)
    this.minPrice = new PriorityQueue((a, b)=>a[0] - b[0] < 0)
};

/** 
 * @param {number} timestamp 
 * @param {number} price
 * @return {void}
 */
StockPrice.prototype.update = function(timestamp, price) {
    this.maxTime = Math.max(timestamp, this.maxTime)
    this.timeMap.set(timestamp, price)
    this.maxPrice.offer([price, timestamp])
    this.minPrice.offer([price, timestamp])
};

/**
 * @return {number}
 */
StockPrice.prototype.current = function() {
    return this.timeMap.get(this.maxTime)
};

/**
 * @return {number}
 */
StockPrice.prototype.maximum = function() {
    while(true){
        const cur = this.maxPrice.peek()
        if(this.timeMap.get(cur[1]) === cur[0])
            return cur[0]
        this.maxPrice.poll()
    }
};

/**
 * @return {number}
 */
StockPrice.prototype.minimum = function() {
    while(true){
        const cur = this.minPrice.peek()
        if(this.timeMap.get(cur[1]) === cur[0])
            return cur[0]
        this.minPrice.poll()
    }
};

/**
 * Your StockPrice object will be instantiated and called as such:
 * var obj = new StockPrice()
 * obj.update(timestamp,price)
 * var param_2 = obj.current()
 * var param_3 = obj.maximum()
 * var param_4 = obj.minimum()
 */

 class PriorityQueue {
  constructor(
    compare = (a, b) => a < b 
    ){
    this.data = []
    this.size = 0
    this.compare = compare
  }

  peek() {
    return this.size === 0 ? null : this.data[0] 
  }

  offer(val) {
    this.data.push(val)
    this._shifUp(this.size++)
  }

  poll() {
    if(this.size === 0) { return null }
    this._swap(0, --this.size)
    this._shifDown(0)
    return this.data.pop()
  }

  _parent(index) {
    return index - 1 >> 1
  }
  
  _child(index) {
    return (index << 1) + 1
  }

  _shifDown(index) {
    while(this._child(index) < this.size) {
      let child = this._child(index)
      if(child + 1 < this.size 
        && this.compare(this.data[child + 1], this.data[child])) {
          child = child + 1
      }
      if(this.compare(this.data[index], this.data[child])){
        break
      }
      this._swap(index, child)
      index = child
    }
  }

  _shifUp(index) {
    while(this._parent(index) >= 0 
    && this.compare(this.data[index], this.data[this._parent(index)])) {
      this._swap(index, this._parent(index))
      index = this._parent(index)
    }
  }

  _swap(a, b) {
    [this.data[a], this.data[b]] = [this.data[b], this.data[a]]
  }
}


//作者:himymBen
//链接:https://leetcode.cn/problems/stock-price-fluctuation/solution/pythonjavajavascriptgo-mo-ni-by-himymben-rayz/
//来源:力扣(LeetCode)