Receive and Query of Sensors

题目:
Given n sensor with state on/off, please implement 2 functions: 1). Recieve the state of one sensor at time t.
真正的可以从任何点开始和束,也就是,可以是一个点,一直网上爬到某个祖先的然后再拐到
另外一个孩子往下爬到某个后代种路径

2). Query the state of a given sensor at a given time. If you dont have the information of the sensor at that time, make the best guess. 

解题思路:


1). 如果Recieve操作很多,Query少得,就直接用一个hash map 来存, 比如 unordered_map<int, unordered_map<int, bool>> sensor_information. 这样RecieveO(1), Query worst case O(N). worst case 生在如果某个时间sensor i 没有某个时间 t 的状,所以best guess 就是找到所有小于时间t中的那个最大时间的状
. 1point 3acres 璁哄
2). 其他情况的就用hash map + binary search tree (BST), 比如unordered_map<int,
BST<pair<int, bool>>> sensor_information. BST就写了一个插入和搜索。这样时间度是
O(lgN)


Comments

Popular Posts