2087. Minimum Cost Homecoming of a Robot in a Grid

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2087

class Solution:
    def minCost(
        self,
        startPos: List[int],
        homePos: List[int],
        rowCosts: List[int],
        colCosts: List[int],
    ) -> int:
        i, j = startPos
        x, y = homePos
        res = 0
        if i < x:
            res += sum(rowCosts[i + 1 : x + 1])
        else:
            res += sum(rowCosts[x:i])
        if j < y:
            res += sum(colCosts[j + 1 : y + 1])
        else:
            res += sum(colCosts[y:j])
        return res