-
Notifications
You must be signed in to change notification settings - Fork 0
/
waterAndBricks.js
38 lines (34 loc) · 1.2 KB
/
waterAndBricks.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
let arr = [2, 0, 1, 2, 3, 0, 0, 1, 0, 3, 1, 0, 2, 1]; // Solution: 17
//let arr = [2, 1, 0, 3]; // Solution: 3
//let arr = [2, 0, 3, 0, 4]; // Solution: 5
let result = 0;
console.log("Das Ausgans-Array lautet: " + arr);
for (let i = 1; i < arr.length - 1 ; i++){
console.log("Value von i ist: " + arr[i]);
console.log("------------------");
let subArrLeft = arr.slice(0, i);
console.log(subArrLeft);
let maxValueLeft = Math.max(...subArrLeft);
console.log(maxValueLeft);
console.log("------------------");
let subArrRight = arr.slice(i+1);
console.log(subArrRight);
let maxValueRight = Math.max(...subArrRight);
console.log(maxValueRight);
console.log("------------------");
console.log("------------------");
if(maxValueLeft >= maxValueRight && arr[i] <= maxValueRight){
result += maxValueRight - arr[i];
}
else if (maxValueLeft < maxValueRight && arr[i] <= maxValueLeft){
result += maxValueLeft - arr[i];
}
else {
result += 0;
}
console.log("Das Result ist: " + result);
console.log("------------------");
console.log("------------------");
console.log("------------------");
}
console.log(result);