Minimum Cost Homecoming of a Robot in a Grid LeetCode solution in C++
1class Solution {2public:3int minCost(vector<int> &start, vector<int> &end, vector<int> &costR, vector<int> &costC) {4int ans = 0;5int i = start[0], j = start[1];6int x = end[0], y = end[1];7int changeI = i < x ? 1 : -1;8int changeJ = j < y ? 1 : -1;910while (i != x) {11i += changeI;12ans += costR[i];13}1415while (j != y) {16j += changeJ;17ans += costC[j];18}1920return ans;21}22};