Implement a simple stock price display systemwhich will show High, Low and Last price for a given stock throughout one day.The data comes from a real-time feed and have the following messages:
PriceUpdate(t, P) -> Price of Stock A at timet is P.
Correction(t, NewP) -> Price of Stock A attime t is rectified to NewP
Remove(t) -> Disregard the price feedreceived at time t.
PriceUpdate(10100,850.50) -> high = 850.50, Low = 850.50, Last = 850.50
PriceUpdate(10200,852.25) -> high = 852.25, Low = 850.50, Last = 852.25
PriceUpdate(10300,848.00) -> high = 852.25, Low = 848.00, Last = 848.00
Correction(10200, 849.00) -> high = 850.50, Low = 848.00, Last 848.00
PriceUpdate(10400,855.00) -> high = 855.00, Low = 848.00, Last = 855.00
Correction(10300, 853.00) -> high = 855.00, Low = 850.50, Last = 855.00
PriceUpdate(10500,854.00) -> high = 855.00, Low = 848.00, Last = 854.00
Correction(10500,853.25) -> high = 855.00, Low = 848.00, Last = 853.25
Remove(10300) -> high = 855.00, Low = 849.00, Last = 853.25 简单说来PriceUpdate就是添加新的(timestamp, price), Correction是改之前的(timestamp, price), 求实现当前high(), low(), last()
简单说来PriceUpdate就是添加新的(timestamp, price), Correction是改之前的(timestamp, price), 求实现当前high(), low(), last()
LZ是用的Heap + HashMap, 特别问了时间复杂度(我猜到他想考heap的remove(obj)复杂度)
follow Up是: 有没有办法把复杂度降到O(logN)
 
LZ是用的Heap + HashMap, 特别问了时间复杂度(我猜到他想考heap的remove(obj)复杂度)
LZ是用treeMap代替了Heap,这样代价是每次找high,low也要logN search整个tree了
 
感觉需要2个数据结构:
1. TreeMap<Long, Double> time2priceMap. 
2. TreeMap<Double, Integer> price2countMap

priceupdate: insert new record into time2priceMap, update price count in price2countmap
correction: update record in time2pricemap, update prev price count, update prev price count (if 0, remove record), update new price count or needs to insert a new price record into price2countmap
high and low: lastkey and firstkey from price2countmap
last: last entry's price from time2pricemap

补充内容 (2017-2-4 07:00):
这样好像都是O(log(n))

G面经: Design Stock Price Display System的更多相关文章

  1. 《The Design of a Practical System for Fault-Tolerant Virtual Machines》论文总结

    VM-FT 论文总结 说明:本文为论文 <The Design of a Practical System for Fault-Tolerant Virtual Machines> 的个人 ...

  2. 《The Design of a Practical System for Fault-Tolerant Virtual Machines》论文研读

    VM-FT 论文研读 说明:本文为论文 <The Design of a Practical System for Fault-Tolerant Virtual Machines> 的个人 ...

  3. 17.观察者模式(Observer Pattern)

    using System; using System.Collections.Generic; namespace ConsoleApplication10 { /// <summary> ...

  4. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  5. [LeetCode] Design Log Storage System 设计日志存储系统

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  6. [LeetCode] Design In-Memory File System 设计内存文件系统

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  7. LeetCode Design Log Storage System

    原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/ 题目: You are given sever ...

  8. [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  9. Design In-Memory File System

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

随机推荐

  1. 遍历文件路径python版,java版

    python: # 获取所有txt路径列表 file_list = [] def gci(filepath): files=os.listdir(filepath) for fi in files: ...

  2. 原生HttpClient详细使用示例

    一.HttpClient类 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; ...

  3. Scala的泛型

    类比java中的泛型: 上界(协变).下界(逆变) scala中泛型采用中括号声明 val array = Array[Int](,,,) array() //声明什么类型就返回什么类型 //test ...

  4. 潭州课堂25班:Ph201805201 tornado 项目 第六课 用户和图片分享的集成(课堂笔记)

    tornado 相关说明 改善图片上传功能 ,生成唯一的 ID ,与路径拼接,生成 URL, 这里引用 uuid 的 python 库 在 photo.py 中创建个类,用来  辅助用户上传的图片,生 ...

  5. BSUIR Open Finals

    A. Game with chocolates 因为差值必须是$P$的幂,故首先可以$O(\log n)$枚举出先手第一步所有取法,判断之后的游戏是否先手必败. 对于判断,首先特判非法的情况,并假设$ ...

  6. css 定位布局

    文档流: 文档流,是指盒子按照html标签编写的顺序依次从上到下,从左到右排列.块元素占一行,行内元素在一行之内从左到在排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置. 关于定位: 可以 ...

  7. css 块元素、内联元素、内联块元素

    块元素.内联元素.内联块元素: 元素就是标签,布局中常用的有三种标签,块元素.内联元素.内联块元素,了解这三种元素的特性,才能熟练的进行页面布局. 块元素: 块元素,也可以称为行元素,布局中常用的标签 ...

  8. 使用Eclipse中的SVN提交代码遇到的问题

    问题: Previous operation has not finished; run 'cleanup' if it was interrupted svn: Commit failed (det ...

  9. 1#Two Sum(qsort用法)

    void*空类型指针,就好像暂时还没有确定类型,任何类型都可以赋给它.但是具体操作时一定要确定类型(如下,比较时先转Node) cmp返回一定是int,有-1,0,1三种,如果是1则第一个数要放在第二 ...

  10. [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...