-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlargest_perimeter_triangle.dart
150 lines (127 loc) · 4.12 KB
/
largest_perimeter_triangle.dart
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
-* Largest Perimeter Triangle *-
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
*/
import 'dart:math';
class A {
// Runtime: 426 ms, faster than 80.00% of Dart online submissions for Largest Perimeter Triangle.
// Memory Usage: 149.6 MB, less than 100.00% of Dart online submissions for Largest Perimeter Triangle.
int largestPerimeter(List<int> nums) {
nums.sort();
for (int i = nums.length - 1; i > 1; --i)
if (nums[i] < nums[i - 1] + nums[i - 2])
return nums[i] + nums[i - 1] + nums[i - 2];
return 0;
}
}
class B {
// Runtime: 514 ms, faster than 60.00% of Dart online submissions for Largest Perimeter Triangle.
// Memory Usage: 150.2 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
//utility method to get max element at given index
void swapToGetMax(List<int> nums, int index) {
int max = 0, maxIndex = -1;
for (int i = 0; i <= index; i++)
if (max < nums[i]) {
max = nums[i];
maxIndex = i;
}
//actual swapping after finding max element in given range
int temp = nums[index];
nums[index] = max;
nums[maxIndex] = temp;
}
int largestPerimeter(List<int> nums) {
//if array has only 3 elements
if (nums.length == 3) {
if (nums[0] < nums[1] + nums[2] &&
nums[1] < nums[0] + nums[2] &&
nums[2] < nums[1] + nums[0])
return nums[0] + nums[1] + nums[2];
else
return 0;
}
//for more than 3 elements, without doing explicit sorting
int n = nums.length;
//here we are putting max element at last index
swapToGetMax(nums, n - 1);
//here we are putting max element at second last index
swapToGetMax(nums, n - 2);
//here we are putting max element at third last index
swapToGetMax(nums, n - 3);
//in loop checking if nums[i]<nums[i-1]+nums[i-2] which this triplet will form the max perimeter
for (int i = n - 1; i >= 2; i--) {
if (nums[i] < nums[i - 1] + nums[i - 2])
return nums[i] + nums[i - 1] + nums[i - 2];
//if not then will find max element as (i-3)th largest element
else if (i > 2) swapToGetMax(nums, i - 3);
}
return 0;
}
}
class C {
// Runtime: 596 ms, faster than 40.00% of Dart online submissions for Largest Perimeter Triangle.
// Memory Usage: 150.3 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
int largestPerimeter(List<int> nums) {
if (nums.length == 3) {
if (nums[0] < nums[1] + nums[2] &&
nums[1] < nums[0] + nums[2] &&
nums[2] < nums[1] + nums[0])
return nums[0] + nums[1] + nums[2];
else
return 0;
}
int n = nums.length;
int maxi = 0;
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
int a = nums[i];
int b = nums[j];
int c = nums[k];
if (a < b + c && b < c + a && c < a + b) maxi = max(maxi, a + b + c);
}
}
}
if (maxi > 0) return maxi;
return 0;
}
}
class D {
int largestPerimeter(List<int> nums) {
//sort the vector
nums.sort();
//any triangle sum of smaller two side greater than 3rd side...so we check that condition, a+b>c where a<b<c
int maxPerimeter = 0;
for (int i = 0; i <= nums.length - 3; i++) {
//check the triangle valid condition
if (nums[i] + nums[i + 1] > nums[i + 2]) {
maxPerimeter =
max(maxPerimeter, nums[i] + nums[i + 1] + nums[i + 2]); //find max
}
}
return maxPerimeter; //return the result
}
}
class F {
int largestPerimeter(List<int> nums) {
nums.sort((a, b) => a - b);
int i = nums.length - 1;
while (i >= 0) {
if (nums[i] < nums[i - 1] + nums[i - 2]) {
return nums[i] + nums[i - 1] + nums[i - 2];
} else {
i--;
}
}
return 0;
}
}