• + 0 comments

    Python:

    def maximumPeople(p, x, y, r): # Return the maximum number of people that will be in a sunny town after removing exactly one cloud.

    cities = [[i[0], i[1]] for i in zip(x, p)]  # cities or towns
    cities = sorted(cities, key=lambda x: (x[0]))
    
    clouds = [[max(1, i[0]-i[1]), i[0]+i[1]] for idx, i in enumerate(zip(y, r))]
    clouds = sorted(clouds, key=lambda x: (x[0]))
    
    len_cloud = len(clouds)
    idx = 0
    start = 0
    sum_sunny = 0
    cloud_city = [0 for _ in range(len_cloud)]
    for j in cities:
        count = []
        check = True
        idx = start
    
        while idx < len_cloud:
            i = clouds[idx]
            if check and i[1] < j[0]:
                start = idx+1
                idx += 1
                continue
    
            if i[1] >= j[0]:
                check = False
    
            if i[0] <= j[0] and j[0] <= i[1]:
                count.append(idx)
    
            if len(count) > 1:
                break
    
            if i[0] > j[0]:
                break
    
            idx += 1
    
        if len(count) == 0:
            sum_sunny += j[1]
        if len(count) == 1:
            cloud_city[count[0]] += j[1]
    
    
    sum_cloud = max(cloud_city)
    # print(cities, selected_clouds)
    # print(sum_sunny, sum_cloud)
    return sum_sunny + sum_cloud