<jupyter_start><jupyter_text>Apartment Hunting[link](https://www.algoexpert.io/questions/Apartment%20Hunting)  My Solution<jupyter_code>def apartmentHunting(blocks, reqs):
    # Write your code here.
    # O(br) time | O(br) space
    distancesReqs = {req: nearestDistance(blocks, req) for req in reqs}
    farestDists = []
    for i in range(len(blocks)):
        farestDists.append(max([distancesReqs[r][i] for r in reqs]))
    return farestDists.index(min(farestDists))

def nearestDistance(blocks, key):
    distances = [float('inf') for b in blocks]
    # left side
    nearestLeftDist = float('inf')
    for i, b in enumerate(blocks):
        if b[key] == True:
            nearestLeftDist = 0
        else:
            nearestLeftDist += 1
        distances[i] = nearestLeftDist
    # right side
    nearestRightDist = float('inf')
    for i in reversed(range(len(blocks))):
        b = blocks[i]
        if b[key] == True:
            nearestRightDist = 0
        else:
            nearestRightDist += 1
        distances[i] = min(distances[i], nearestRightDist)
    return distances
def apartmentHunting(blocks, reqs):
    # Write your code here.
    # O(br) time | O(b) space
    farestDists = [float('-inf') for b in blocks]
    for r in reqs:
        distances = nearestDistance(blocks, r)
        for i in range(len(blocks)):
            farestDists[i] = max(farestDists[i], distances[i])
    return farestDists.index(min(farestDists))

def nearestDistance(blocks, key):
    distances = [float('inf') for b in blocks]
    # left side
    nearestLeftDist = float('inf')
    for i, b in enumerate(blocks):
        if b[key] == True:
            nearestLeftDist = 0
        else:
            nearestLeftDist += 1
        distances[i] = nearestLeftDist
    # right side
    nearestRightDist = float('inf')
    for i in reversed(range(len(blocks))):
        b = blocks[i]
        if b[key] == True:
            nearestRightDist = 0
        else:
            nearestRightDist += 1
        distances[i] = min(distances[i], nearestRightDist)
    return distances<jupyter_output><empty_output><jupyter_text>Expert Solution<jupyter_code># O(b^2r) time | O(b) space - where b is the number of blocks and r is the number of requirements
def apartmentHunting(blocks, reqs):
    maxDistancesAtBlocks = [float("-inf" for block in blocks)]
    for i in range(len(blocks)):
        for req in reqs:
            closestReqDistance = float("inf")
            for j in range(len(blocks)):
                if blocks[j][req]:
                    closestReqDistance = min(closestReqDistance, distanceBetween(i, j))
            maxDistancesAtBlocks[i] = max(maxDistancesAtBlocks[i], closestReqDistance)
    return getIdxAtMinValue(maxDistancesAtBlocks)

def getIdxAtMinValue(array):
    idxAtMinValue = 0
    minValue = float("inf")
    for i in range(len(array)):
        if currentValue < minValue:
            minValue = currentValue
            idxAtMinValue = i
    return idxAtMinValue

def distanceBetween(a, b):
    return abs(a - b)
# O(br) time | O(br) space - where b is the number of blocks and r is the number of requirements
def apartmentHunting(blocks, reqs):
    minDistFromBlocks = list(map(lambda req: getMinDist(blocks, req), reqs))
    maxDistAtBlocks = getMaxDistAtBlocks(blocks, minDistFromBlocks)
    return getIdxAtMinValue(maxDistAtBlocks)

def getMinDist(blocks, req):
    minDist = [0 for block in blocks]
    closestReqIdx = float("inf")
    for i in range(len(blocks)):
        if blocks[i][req]:
            closestReqIdx = i
        minDist[i] = distanceBetween(i, closestReqIdx)
    for i in reversed(range(len(blocks))):
        if blocks[i][req]:
            closestReqIdx = i
        minDist[i] = min(minDist[i], distanceBetween(i, closestReqIdx))
    return minDist

def getMaxDistAtBlocks(blocks, minDistFromBlocks):
    maxDistAtBlocks = [0 for block in blocks]
    for i in range(len(blocks)):
        minDistAtBlocks = list(map(lambda distances:distances[i], minDistFromBlocks))
        maxDistAtBlocks[i] = max(minDistAtBlocks)
    return maxDistAtBlocks

def getIdxAtMinValue(array):
    idxAtMinValue = 0
    minValue = float("inf")
    for i in range(len(array)):
        currentValue = array[i]
        if currentValue < minValue:
            minValue = currentValue
            idxAtMinValue = i
    return idxAtMinValue

def distanceBetween(a,b):
    return abs(a - b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10532250789354984, 'fraction_numerical': 0.0033829499323410014, 'mean_word_length': 2.9143865842894967, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '103181', 'license': 'MIT', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1384, 'n_words': 362, 'path': 'algoExpert/apartment_hunting/solution.ipynb', 'repo_name': 'maple1eaf/learning_algorithm', 'size': '4434'}