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. zabbix4.0构建实录

    [Nginx] #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@centos ...

  2. KaliLinux常用服务配置教程DHCP服务工作流程

    KaliLinux常用服务配置教程DHCP服务工作流程 DHCP服务工作流程如图1.1所示. 具体的工作流程如下所示: (1)DHCP客户端以广播的方式发出DHCP Discover报文. (2)所有 ...

  3. webpack打包后访问不到json文件

    一.问题描述 在vue中,前端写ajax假数据,用axios将json数据渲染到组件中,开发期间一切正常,webpack打包压缩后,json文件的路径错误,页面访问不到数据,导致渲染失败. 二.预期结 ...

  4. (二)文档请求不同源之window.name跨域

    一.基本原理 window.name不是一个普通的全局变量,而是当前窗口的名字.这里要注意的是每个iframe都有包裹它的window,而这个window 是top window的子窗口,而它自然也有 ...

  5. NOIP2011 D2T3 观光公交 做题笔记

    目录 归纳题目的性质 算法 60分 100分 code 大家来找茬 总结 归纳题目的性质 每一个加速器效果相同(1) 车子等到所有人上车之后才会发车, 这个最早发车时间不由加速器的配比决定(2) 要优 ...

  6. c++中一个多态的实例

    #include <iostream> #include <fstream> #include <vector> #include <algorithm> ...

  7. svn打分支和合并操作

    1.svn打分支 到trunk里,选择Branch/tag.... 填写分支版本路径 到branch里svn up 一下,就有1.4.0分支了 2.svn合并 到trunk里,选择Merge.. 选择 ...

  8. 浅谈vue性能优化

    基础优化 所谓的基础优化是任何 web 项目都要做的,并且是问题的根源.HTML,CSS,JS 是第一步要优化的点 分别对应到 .vue 文件内的,<template>,<style ...

  9. shell - shift

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如 shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shi ...

  10. Jquery常用的方法总结

    1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...