-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0611-valid-triangle-number.js
More file actions
44 lines (38 loc) · 1006 Bytes
/
0611-valid-triangle-number.js
File metadata and controls
44 lines (38 loc) · 1006 Bytes
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
/**
* Valid Triangle Number
* Time Complexity: O(N^2)
* Space Complexity: O(1)
*/
var triangleNumber = function (nums) {
let finalTriangleCount = 0;
const numItems = nums.length;
if (numItems < 3) {
return 0;
}
nums.sort((firstValue, secondValue) => firstValue - secondValue);
for (
let currentOuterIndex = 0;
currentOuterIndex < numItems - 2;
currentOuterIndex++
) {
if (nums[currentOuterIndex] === 0) {
continue;
}
let thirdPointer = currentOuterIndex + 2;
for (
let currentMiddleIndex = currentOuterIndex + 1;
currentMiddleIndex < numItems - 1;
currentMiddleIndex++
) {
thirdPointer = Math.max(thirdPointer, currentMiddleIndex + 1);
while (
thirdPointer < numItems &&
nums[currentOuterIndex] + nums[currentMiddleIndex] > nums[thirdPointer]
) {
thirdPointer++;
}
finalTriangleCount += thirdPointer - 1 - currentMiddleIndex;
}
}
return finalTriangleCount;
};