Cumulative sum of the smallest neighbors in the previous row in a Numpy array
first, your problem is example of dynamic programming. Here some tips how to approach problem in python
your solution calculate each table cell separately. But function min_parent() use data only from previous row, so you can calculate row by row (and numpy is designed to fast operations on vectors)
i am using like you range &lt;j - 1 : j + 2) but check if task don't need &lt;j - 1, j + 2&gt;


Example code


assert(w >= 2) # if not, you should handle w == 1 in special way

def vectorized_solution(arr, h):
    sum_arr = arr.copy()
    for i in range(1, h):
        parent = sum_arr[i - 1, :]
        sum_arr[i, 0] += min(parent[0], parent[1])
        sum_arr[i, 1:-1] += np.minimum.reduce([parent[0:-2], parent[1:-1], parent[2:]])
        sum_arr[i, -1] += min(parent[-2], parent[-1])
    return sum_arr