There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol).

The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n.


	If the robot moves up or down into a cell whose row is r, then this move costs rowCosts[r].
	If the robot moves left or right into a cell whose column is c, then this move costs colCosts[c].


Return the minimum total cost for this robot to return home.

&nbsp;
Example 1:

Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]
Output: 18
Explanation: One optimal path is that:
Starting from (1, 0)
-&gt; It goes down to (2, 0). This move costs rowCosts[2] = 3.
-&gt; It goes right to (2, 1). This move costs colCosts[1] = 2.
-&gt; It goes right to (2, 2). This move costs colCosts[2] = 6.
-&gt; It goes right to (2, 3). This move costs colCosts[3] = 7.
The total cost is 3 + 2 + 6 + 7 = 18

Example 2:

Input: startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]
Output: 0
Explanation: The robot is already at its home. Since no moves occur, the total cost is 0.


&nbsp;
Constraints:


	m == rowCosts.length
	n == colCosts.length
	1 &lt;= m, n &lt;= 105
	0 &lt;= rowCosts[r], colCosts[c] &lt;= 104
	startPos.length == 2
	homePos.length == 2
	0 &lt;= startrow, homerow &lt; m
	0 &lt;= startcol, homecol &lt; n


class Solution:
    def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
        def getRange(left, right, array):
            if left > right:
                right, left = left, right
            return sum((array[i] for i in range(left,right+1)))
        
        totalRowCost = getRange(startPos[0], homePos[0], rowCosts)
        totalColCost = getRange(startPos[1], homePos[1], colCosts)
        
        #Don't pay for the position you start out on
        return totalRowCost + totalColCost - rowCosts[startPos[0]] - colCosts[startPos[1]]
class Solution {
    public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
        int total = 0;
        
        // if home is to the down of start move, down till there
        if(homePos[0]>startPos[0]){
            int i = startPos[0]+1;
            while(i<=homePos[0]){
                total += rowCosts[i]; // adding cost while moving corresponding to the cell
                i++;
            }
        }
        
        // else if home is up from the start, move up till there
        else if(homePos[0]<startPos[0]){
            int i = startPos[0]-1;
            while(i>=homePos[0]){
                total += rowCosts[i]; // adding cost while moving corresponding to the cell
                i--;
            }
        }
        
        // if home is right to the start, move right till there
        if(homePos[1]>startPos[1]){
            int i = startPos[1]+1;
            while(i<=homePos[1]){
                total += colCosts[i];  // adding cost while moving corresponding to the cell
                i++;
            }
        }
        
        // else if home is left to the start, move left till there
        else if(homePos[1]<startPos[1]){
            int i = startPos[1]-1;
            while(i>=homePos[1]){
                total += colCosts[i];  // adding cost while moving corresponding to the cell
                i--;
            }
        }
        
        return total;
    }
}
// This is Straightforward Question becuase you need to travel at least one time the incoming rows and column in path.
// So why you need to complicate the path just traverse staright and to homePos Row and homePos column and you will get the ans...
// This Question would have become really tough when negative values also possible in the row and column vectors because that negative values could have decresed the results...but here its simple and concise.
class Solution {
public:
    int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {
        int ans = 0;
        if(startPos[0] < homePos[0])
        {
            for(int i = startPos[0]+1 ; i <=homePos[0] ; i++)
            {
                ans+=rowCosts[i];
            }
        }
        if(startPos[0] > homePos[0])
        {
            for(int i = startPos[0]-1 ; i >=homePos[0] ; i--)
            {
                ans+=rowCosts[i];
            }
        }
        if(startPos[1] < homePos[1])
        {
            for(int i = startPos[1]+1 ; i <=homePos[1] ; i++)
            {
                ans+=colCosts[i];
            }
        }
         if(startPos[1] > homePos[1])
        {
            for(int i = startPos[1]-1 ; i >=homePos[1] ; i--)
            {
                ans+=colCosts[i];
            }
        }
        return ans;
    }
};
var minCost = function(startPos, homePos, rowCosts, colCosts) {
    let totCosts = 0;

    let rowDir = startPos[0] <= homePos[0] ? 1 : -1;
    let colDir = startPos[1] <= homePos[1] ? 1 : -1;
    
    let row = startPos[0];

    while (row != homePos[0]) {
        row += rowDir;
        totCosts += rowCosts[row];
    }

    let col = startPos[1];

    while (col != homePos[1]) {
        col += colDir;
        totCosts += colCosts[col];
    }

    return totCosts;
};
Minimum Cost Homecoming of a Robot in a Grid