-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest-perimeter-triangle.js
More file actions
69 lines (57 loc) · 1.77 KB
/
largest-perimeter-triangle.js
File metadata and controls
69 lines (57 loc) · 1.77 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.
Example 1:
Input: nums = [2,1,2]
Output: 5
Example 2:
Input: nums = [1,2,1]
Output: 0
Constraints:
3 <= nums.length <= 104
1 <= nums[i] <= 106
*/
//array of nums, are they whole nums? unsigned? are the array aways with the length equals to 3? always integer?
//return a number that represents the biggest perimeter that three of the nums can form
//[2, 1, 2] = 5
//[1, 2, 1] = 0, because it can not for a triangle
//[1, 2, 3, 4, 5, 6, 7] = 18
//confirm if the nums passed can form a triangle, if not return 0
//sort the array by decrescent order get the three first numbers, check if they form a triangle and return the number when calculating the perimeter
/**
* @param {number[]} nums
* @return {number}
*/
var largestPerimeter = function(nums) {
nums.sort((a, b) => b - a)
let max = 0
let a, b, c
for(let i = 0; i < nums.length - 2; i++){
a = nums[i]
for(let j = i + 1; j < nums.length - 1; j++){
b = nums[j]
for(let k= j + 1; k < nums.length; k++){
c = nums[k]
if (isTriangle(a, b, c) && (a + b + c > max)){
max = a + b + c
return max
}
}
}
}
return max
};
function isTriangle(a, b, c){
if (Math.abs(a - b) < c && a + b > c){
if (Math.abs(a - c) < b && a + c > b){
if (Math.abs(b - c) < a && b + c > a){
return true
} else {
return false
}
} else {
return false
}
} else {
return false
}
}