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. 这样Recieve是O(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)
. 1point 3acres 璁哄潧
2). 其他情况的话就用hash map + binary search tree (BST), 比如unordered_map<int,
BST<pair<int, bool>>> sensor_information. BST就写了一个插入和搜索。这样时间复杂度是
O(lgN)

Comments
Post a Comment