Medium
Minimum Cost Homecoming of a Robot in a Grid — Python
Full explanation · Time O(m + n) · Space O(1)
# 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])