Minimum Cost Homecoming of a Robot in a Grid LeetCode solution in JavaScript

1
var minCost = function (startPos, homePos, rowCosts, colCosts) {
2
let totCosts = 0;
3

4
let rowDir = startPos[0] <= homePos[0] ? 1 : -1;
5
let colDir = startPos[1] <= homePos[1] ? 1 : -1;
6

7
let row = startPos[0];
8

9
while (row != homePos[0]) {
10
row += rowDir;
11
totCosts += rowCosts[row];
12
}
13

14
let col = startPos[1];
15

16
while (col != homePos[1]) {
17
col += colDir;
18
totCosts += colCosts[col];
19
}
20

21
return totCosts;
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0