-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (64 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Complete the 'optimalPosition' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY food
* 2. INTEGER_ARRAY dist
*/
// const food = [3, 2, 5, 4];
// const dist = [2, 3, 4, 2];
const food = [2, 4, 5, 2];
const dist = [4, 3, 1, 3];
// const food = [8, 4, 1, 9];
// const dist = [10, 9, 3, 5];
function optimalPosition(foods, dists) {
const n = foods.length;
const food_copie = [...foods];
const finalArray = [];
console.log(foods);
console.log(dists);
let res = 0;
for (let index = 0; index < dists.length; index++) {
let final_res = null;
const food = foods[index];
const dist = dists[index];
// console.log({ food: food, index: index });
const newDistArray = index == 0 ? dists : arraySwitcher(dists);
const newFoodArray = index == 0 ? foods : arraySwitcher(food_copie);
for (let j = 0; j < newDistArray.length; j++) {
const element = foods[index];
const element2 = dists[j];
if ((final_res ?? element) - (element2 ?? dists[0]) < 0) {
// console.log("negative");
break;
} else {
// console.log(
// `${final_res ?? element} - ${element2 ?? dists[0]} + ${
// newFoodArray[j + 1] ?? 0
// } = `
// );
final_res =
(final_res ?? element) -
(element2 ?? dists[0]) +
(newFoodArray[j + 1] ?? 0);
// console.log(final_res);
if (j === newDistArray.length - 1) {
finalArray.push(index);
}
}
}
}
const result = finalArray.length == 0 ? -1 : Math.min(...finalArray);
console.log(
result == -1
? "There is Zero path where Son Goku can finish his travel 😓"
: `The optimal position for Son Goku to finish hes travel is: ${result}`
);
return result;
}
function arraySwitcher(array) {
array.push(array.shift(array));
return array;
}
optimalPosition(food, dist);