Minimum Cost Homecoming of a Robot in a Grid LeetCode solution in C++

1
class Solution {
2
public:
3
int minCost(vector<int> &start, vector<int> &end, vector<int> &costR, vector<int> &costC) {
4
int ans = 0;
5
int i = start[0], j = start[1];
6
int x = end[0], y = end[1];
7
int changeI = i < x ? 1 : -1;
8
int changeJ = j < y ? 1 : -1;
9

10
while (i != x) {
11
i += changeI;
12
ans += costR[i];
13
}
14

15
while (j != y) {
16
j += changeJ;
17
ans += costC[j];
18
}
19

20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0