golang力扣leetcode 2034.股票价格波动

简介: golang力扣leetcode 2034.股票价格波动

2034.股票价格波动

2034.股票价格波动

题解

两种做法,第一种,heap包,具体使用看代码,维护一个大根堆和一个小根堆。第二种,用slice模拟Priority queue

代码

package main
import "container/heap"
type pair struct {
  timestamp, price int
}
type hp []pair
func (h hp) Len() int {
  return len(h)
}
func (h hp) Less(i, j int) bool {
  return h[i].price < h[j].price
}
func (h hp) Swap(i, j int) {
  h[i], h[j] = h[j], h[i]
}
func (h *hp) Push(x interface{}) {
  *h = append(*h, x.(pair))
}
func (h *hp) Pop() interface{} {
  cntHeap := *h
  val := cntHeap[h.Len()-1]
  *h = cntHeap[:h.Len()-1]
  return val
}
type StockPrice struct {
  mp           map[int]int
  cur          int
  maxHp, minHp hp
}
func Constructor() StockPrice {
  return StockPrice{
    mp:  map[int]int{},
    cur: 0,
  }
}
func (this *StockPrice) Update(timestamp int, price int) {
  if timestamp > this.cur {
    this.cur = timestamp
  }
  this.mp[timestamp] = price
  heap.Push(&this.maxHp, pair{
    timestamp: timestamp,
    price:     -price,
  })
  heap.Push(&this.minHp, pair{
    timestamp: timestamp,
    price:     price,
  })
}
func (this *StockPrice) Current() int {
  return this.mp[this.cur]
}
func (this *StockPrice) Maximum() int {
  for {
    pri := this.maxHp[0]
    if pri.price == -this.mp[pri.timestamp] {
      return -pri.price
    } else {
      heap.Pop(&this.maxHp)
    }
  }
}
func (this *StockPrice) Minimum() int {
  for {
    pri := this.minHp[0]
    if pri.price == this.mp[pri.timestamp] {
      return pri.price
    } else {
      heap.Pop(&this.minHp)
    }
  }
}
package main
type StockPrice struct {
  currPrice    int
  timeStamp    int
  timeToPriMap map[int]int
  priQue       []int
}
func Constructor() StockPrice {
  return StockPrice{
    currPrice:    0,
    timeStamp:    0,
    timeToPriMap: make(map[int]int),
    priQue:       make([]int, 0),
  }
}
func (this *StockPrice) Update(timestamp int, price int) {
  if timestamp >= this.timeStamp {
    this.timeStamp = timestamp
    this.currPrice = price
  }
  if oldPri, ok := this.timeToPriMap[timestamp]; ok {
    l, r := 0, len(this.priQue)-1
    for l+1 < r {
      mid := l + (r-l)/2
      if this.priQue[mid] <= oldPri {
        l = mid
      } else {
        r = mid
      }
    }
    idx := r
    if this.priQue[l] == oldPri {
      idx = l
    }
    //把旧的数据找到并删除
    copy(this.priQue[idx:], this.priQue[idx+1:])
    this.priQue = this.priQue[:len(this.priQue)-1]
  }
  this.timeToPriMap[timestamp] = price
  l, r := 0, len(this.priQue)-1
  for l+1 < r {
    mid := l + (r-l)/2
    if this.priQue[mid] <= price {
      l = mid
    } else {
      r = mid
    }
  }
  idx := 0
  if len(this.priQue) != 0 {
    if this.priQue[l] >= price {
      idx = l
    } else if this.priQue[r] >= price {
      idx = r
    } else if this.priQue[r] < price {
      idx = r + 1
    }
  }
  //把新的数据插入
  this.priQue = append(this.priQue, 0)
  copy(this.priQue[idx+1:], this.priQue[idx:])
  this.priQue[idx] = price
}
func (this *StockPrice) Current() int {
  return this.currPrice
}
func (this *StockPrice) Maximum() int {
  return this.priQue[len(this.priQue)-1]
}
func (this *StockPrice) Minimum() int {
  return this.priQue[0]
}
目录
相关文章
|
5月前
|
Go 开发者 索引
【LeetCode 热题100】路径与祖先:二叉树中的深度追踪技巧(力扣33 / 81/ 153/154)(Go语言版)
本文深入探讨了LeetCode中四道关于「搜索旋转排序数组」的经典题目,涵盖了无重复和有重复元素的情况。通过二分查找的变形应用,文章详细解析了每道题的解题思路和Go语言实现代码。关键点包括判断有序区间、处理重复元素以及如何缩小搜索范围。文章还总结了各题的异同,并推荐了类似题目,帮助读者全面掌握二分查找在旋转数组中的应用。无论是初学者还是有经验的开发者,都能从中获得实用的解题技巧和代码实现方法。
270 14
|
4月前
|
Go
【LeetCode 热题100】DP 实战进阶:最长递增子序列、乘积最大子数组、分割等和子集(力扣300 / 152/ 416 )(Go语言版)
本文深入解析三道经典的动态规划问题:**最长递增子序列(LIS)**、**乘积最大子数组** 和 **分割等和子集**。 - **300. LIS** 通过 `dp[i]` 表示以第 `i` 个元素结尾的最长递增子序列长度,支持 O(n²) 动态规划与 O(n log n) 的二分优化。 - **152. 乘积最大子数组** 利用正负数特性,同时维护最大值与最小值的状态转移方程。 - **416. 分割等和子集** 转化为 0-1 背包问题,通过布尔型 DP 实现子集和判断。 总结对比了三题的状态定义与解法技巧,并延伸至相关变种问题,助你掌握动态规划的核心思想与灵活应用!
158 1
|
4月前
|
分布式计算 算法 Go
【LeetCode 热题100】BFS/DFS 实战:岛屿数量 & 腐烂的橘子(力扣200 / 994 )(Go语言版)
本文讲解了两道经典的图论问题:**岛屿数量(LeetCode 200)** 和 **腐烂的橘子(LeetCode 994)**,分别通过 DFS/BFS 实现。在“岛屿数量”中,利用深度或广度优先搜索遍历二维网格,标记连通陆地并计数;“腐烂的橘子”则采用多源 BFS,模拟腐烂传播过程,计算最短时间。两者均需掌握访问标记技巧,是学习网格搜索算法的绝佳实践。
168 1
|
4月前
|
Go
【LeetCode 热题100】BFS/DFS 实战:岛屿数量 & 腐烂的橘子(力扣200 / 994 )(Go语言版)
本篇博客详细解析了三道经典的动态规划问题:198. 打家劫舍(线性状态转移)、279. 完全平方数与322. 零钱兑换(完全背包问题)。通过 Go 语言实现,帮助读者掌握动态规划的核心思想及其实战技巧。从状态定义到转移方程,逐步剖析每道题的解法,并总结其异同点,助力解决更复杂的 DP 问题。适合初学者深入理解动态规划的应用场景和优化方法。
109 0
|
4月前
|
算法 Go 索引
【LeetCode 热题100】回溯:括号生成 & 组合总和(力扣22 / 39 )(Go语言版)
本文深入解析了LeetCode上的两道经典回溯算法题:**22. 括号生成**与**39. 组合总和**。括号生成通过维护左右括号数量,确保路径合法并构造有效组合;组合总和则允许元素重复选择,利用剪枝优化搜索空间以找到所有满足目标和的组合。两者均需明确路径、选择列表及结束条件,同时合理运用剪枝策略提升效率。文章附有Go语言实现代码,助你掌握回溯算法的核心思想。
128 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
209 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
152 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
322 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
215 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
304 1

热门文章

最新文章

推荐镜像

更多