You are given two independent coding tasks similar to what a DevOps engineer might face when reasoning about Meta infrastructure systems such as Terragraph routing and large-scale log compaction. Implement both algorithms within one interview.
Given a 2D grid of 0s and 1s, where 0 means open and 1 means blocked, return the length of the shortest path from the top-left cell to the bottom-right cell moving in 4 directions. Return -1 if no path exists.
Given an array files where files[i] is the size of the ith file, merge adjacent files until one file remains. The cost of merging two groups is the sum of their sizes. Return the minimum total cost.
shortest_path_grid(grid) takes List[List[int]] and returns an integer.min_merge_cost(files) takes List[int] and returns an integer.Example 1:
grid = [[0,0,0],[1,1,0],[0,0,0]]4Example 2:
files = [10, 20, 30]9010+20=30, then 30+30=60, total cost 90.1 <= rows, cols <= 200grid[i][j] is 0 or 11 <= files.length <= 3001 <= files[i] <= 10^4