2087. Minimum Cost Homecoming of a Robot in a Grid 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021class Solution {public: int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) { int ret = 0; int i = startPos[0], j = startPos[1], x = homePos[0], y = homePos[1]; if(i<x){ for(int c = i+1 ; c<=x;++c) ret+=rowCosts[c]; } else if(i>x){ for(int c = i-1;c>=x;--c) ret+=rowCosts[c]; } if(j<y){ for(int c = j+1;c<=y;++c) ret+=colCosts[c]; } else if(j>y){ for(int c = j-1;c>=y;--c) ret+=colCosts[c]; } return ret; }}; analysis time complexity O(nm) space complexity O(1)