2087.cpp (525B)
1 class Solution { 2 public: 3 int minCost(const vector<int> &startPos, const vector<int> &homePos, const vector<int> &rowCosts, 4 const vector<int> &colCosts) const { 5 int i = startPos[0], j = startPos[1]; 6 int x = homePos[0], y = homePos[1]; 7 int res = 0; 8 9 while (i != x) { 10 i += i < x ? 1 : -1; 11 res += rowCosts[i]; 12 } 13 14 while (j != y) { 15 j += j < y ? 1 : -1; 16 res += colCosts[j]; 17 } 18 19 return res; 20 } 21 };