本文主要是介绍Lintcode 565 · Heart Beat[Python],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对于发出ping的slave,那其至少可以工作到发出ping的时间+ 2倍的k。在检查时,只要slave的最近一次更新的时间小于这个检查时间节点,就说明这个slave挂了。所以,设置一个dict,记录每个slave的存活时间。
class HeartBeat:def __init__(self):# do intialization if necessaryself.timeunite = 0self.serverlivesofar = collections.defaultdict(int)"""@param: slaves_ip_list: a list of slaves'ip addresses@param: k: An integer@return: nothing"""def initialize(self, slaves_ip_list, k):# write your code hereself.timeunite = 2*kfor ip in slaves_ip_list:self.serverlivesofar[ip] = self.timeunite"""@param: timestamp: current timestamp in seconds@param: slave_ip: the ip address of the slave server@return: nothing"""def ping(self, timestamp, slave_ip):# write your code hereif slave_ip not in self.serverlivesofar:returnself.serverlivesofar[slave_ip] = timestamp + self.timeunite"""@param: timestamp: current timestamp in seconds@return: a list of slaves'ip addresses that died"""def getDiedSlaves(self, timestamp):# write your code hereres = []for ip, timelive in self.serverlivesofar.items():if timelive <= timestamp:res.append(ip)return res
这篇关于Lintcode 565 · Heart Beat[Python]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!