-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeDucksLiveTest.js
More file actions
48 lines (39 loc) · 1.06 KB
/
threeDucksLiveTest.js
File metadata and controls
48 lines (39 loc) · 1.06 KB
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
const Solution = (InputArray) => {
let answer = 0;
let index = 0;
while (index < InputArray.length) {
for (let i = index; i < InputArray.length; i++) {
const calculatedArea =
Math.min(InputArray[index], InputArray[i]) * (i - index);
if (answer < calculatedArea) {
answer = calculatedArea;
}
}
index++;
}
return answer;
};
const Solution2 = (InputArray) => {
let answer = 0;
let startIndex = 0;
let endIndex = InputArray.length - 1;
while (startIndex < endIndex) {
const calculatedArea =
Math.min(InputArray[startIndex], InputArray[endIndex]) *
(endIndex - startIndex);
if (answer < calculatedArea) {
answer = calculatedArea;
continue;
}
if (InputArray[startIndex] < InputArray[endIndex]) {
startIndex++;
} else {
endIndex--;
}
}
return answer;
};
console.log(Solution2([1, 8, 6, 2, 5, 4, 8, 3, 7])); // 49
console.log(Solution2([5, 5, 5, 5, 5, 5])); // 25
console.log(Solution2([0, 99999])); // 0
console.log(Solution2([1, 2, 3, 4, 5, 6, 7, 8])); // 16