当前位置: 首页 > news >正文

Golang | Leetcode Golang题解之第502题IPO

题目:

题解:

func findMaximizedCapital(k, w int, profits, capital []int) int {n := len(profits)type pair struct{ c, p int }arr := make([]pair, n)for i, p := range profits {arr[i] = pair{capital[i], p}}sort.Slice(arr, func(i, j int) bool { return arr[i].c < arr[j].c })h := &hp{}for cur := 0; k > 0; k-- {for cur < n && arr[cur].c <= w {heap.Push(h, arr[cur].p)cur++}if h.Len() == 0 {break}w += heap.Pop(h).(int)}return w
}type hp struct{ sort.IntSlice }
func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

http://www.mrgr.cn/news/56606.html

相关文章:

  • 【数据结构与算法】第3课—数据结构之单链表
  • 字符串和编码
  • Preview Sections 预览部分
  • 开头的例子的理解
  • Marin说PCB之GMSL2 通道上的ESD器件的设计注意事项--03
  • RequestBody接收参数报错com.fasterxml.jackson.databind.exc.MismatchedInputException
  • RIGOL示波器 AUTO键功能已被限制,怎么解决?
  • 大规模图形计算框架之HAMA
  • Apache配置案例一:完成web服务的一个基本应用
  • 读数据工程之道:设计和构建健壮的数据系统17存储的原材料
  • 导出你的大脑:AI如何成为个人认知的延伸
  • MATLAB人脸考勤系统
  • stm32 单片机(on-chip flash)(片上flash)使用 rt-thread 的FAL 软件包
  • Python | Leetcode Python题解之第502题IPO
  • 利用 Direct3D 绘制几何体—7.编译着色器
  • OracleSQL语句 某字段重复数据只取一条
  • word中某些段落行间距无法更改
  • Java 之 Map遍历并删除的几种方法对比
  • 一种用于传感器网络的新型OPC UA PubSub协议绑定(MQTT-SN)
  • go 语言 Gin Web 框架的实现原理探究
  • Java | Leetcode Java题解之第501题二叉搜索树中的众数
  • 有什么好点子帮助更好的学习英语吗?
  • MySQL-事物隔离级别
  • C++ —— 实现一个日期类
  • 使用Mock库进行依赖注入的实用指南
  • TinyC编译器5—词法分析