Skip to content
LC-2087 Medium LeetCode

2087. Minimum Cost Homecoming of a Robot in a Grid

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 51% Topics: Array, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(m + n)
# Space: O(1)

class Solution(object):
    def minCost(self, startPos, homePos, rowCosts, colCosts):
        """
        :type startPos: List[int]
        :type homePos: List[int]
        :type rowCosts: List[int]
        :type colCosts: List[int]
        :rtype: int
        """
        [x0, y0], [x1, y1] = startPos, homePos
        return (sum(rowCosts[i] for i in xrange(min(x0, x1), max(x0, x1)+1))-rowCosts[x0]) + \
               (sum(colCosts[i] for i in xrange(min(y0, y1), max(y0, y1)+1))-colCosts[y0])

Solution from kamyu104/LeetCode-Solutions · MIT