We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
fromcollectionsimportdefaultdictn=len(p)m=len(y)# Prepare events for cloud coveragecloud_start=[]cloud_end=[]foriinrange(m):cloud_start.append((y[i]-r[i],i))cloud_end.append((y[i]+r[i],i))cloud_start.sort()cloud_end.sort()# Pair towns with their populations and sort by locationtowns=sorted([[x[i],p[i],-1]foriinrange(n)],key=lambdat:t[0])active_clouds=set()cloud_start_i,cloud_end_i=0,0cloud_populations=defaultdict(int)free_population=0foriinrange(n):town_loc=towns[i][0]# Add clouds starting before or at town locationwhilecloud_start_i<mandcloud_start[cloud_start_i][0]<=town_loc:active_clouds.add(cloud_start[cloud_start_i][1])cloud_start_i+=1# Remove clouds ending before town locationwhilecloud_end_i<mandcloud_end[cloud_end_i][0]<town_loc:active_clouds.discard(cloud_end[cloud_end_i][1])cloud_end_i+=1iflen(active_clouds)==0:# Town is sunnyfree_population+=towns[i][1]eliflen(active_clouds)==1:# Town covered by exactly one cloudc=next(iter(active_clouds))cloud_populations[c]+=towns[i][1]# The maximum number of sunny people after removing one cloudmax_cloud_pop=max(cloud_populations.values(),default=0)returnfree_population+max_cloud_pop
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cloudy Day
You are viewing a single comment's thread. Return to all comments →
Python3: