Skip to content

Commit f8a21e9

Browse files
committed
leetcode
1 parent 3c07ff6 commit f8a21e9

File tree

4 files changed

+326
-0
lines changed

4 files changed

+326
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
3+
-* Largest Perimeter Triangle *-
4+
5+
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.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [2,1,2]
12+
Output: 5
13+
Example 2:
14+
15+
Input: nums = [1,2,1]
16+
Output: 0
17+
18+
19+
Constraints:
20+
21+
3 <= nums.length <= 104
22+
1 <= nums[i] <= 106
23+
24+
25+
*/
26+
27+
import 'dart:math';
28+
29+
class A {
30+
// Runtime: 426 ms, faster than 80.00% of Dart online submissions for Largest Perimeter Triangle.
31+
// Memory Usage: 149.6 MB, less than 100.00% of Dart online submissions for Largest Perimeter Triangle.
32+
33+
int largestPerimeter(List<int> nums) {
34+
nums.sort();
35+
for (int i = nums.length - 1; i > 1; --i)
36+
if (nums[i] < nums[i - 1] + nums[i - 2])
37+
return nums[i] + nums[i - 1] + nums[i - 2];
38+
return 0;
39+
}
40+
}
41+
42+
class B {
43+
// Runtime: 514 ms, faster than 60.00% of Dart online submissions for Largest Perimeter Triangle.
44+
// Memory Usage: 150.2 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
45+
46+
//utility method to get max element at given index
47+
void swapToGetMax(List<int> nums, int index) {
48+
int max = 0, maxIndex = -1;
49+
for (int i = 0; i <= index; i++)
50+
if (max < nums[i]) {
51+
max = nums[i];
52+
maxIndex = i;
53+
}
54+
//actual swapping after finding max element in given range
55+
int temp = nums[index];
56+
nums[index] = max;
57+
nums[maxIndex] = temp;
58+
}
59+
60+
int largestPerimeter(List<int> nums) {
61+
//if array has only 3 elements
62+
if (nums.length == 3) {
63+
if (nums[0] < nums[1] + nums[2] &&
64+
nums[1] < nums[0] + nums[2] &&
65+
nums[2] < nums[1] + nums[0])
66+
return nums[0] + nums[1] + nums[2];
67+
else
68+
return 0;
69+
}
70+
//for more than 3 elements, without doing explicit sorting
71+
int n = nums.length;
72+
//here we are putting max element at last index
73+
swapToGetMax(nums, n - 1);
74+
//here we are putting max element at second last index
75+
swapToGetMax(nums, n - 2);
76+
//here we are putting max element at third last index
77+
swapToGetMax(nums, n - 3);
78+
//in loop checking if nums[i]<nums[i-1]+nums[i-2] which this triplet will form the max perimeter
79+
for (int i = n - 1; i >= 2; i--) {
80+
if (nums[i] < nums[i - 1] + nums[i - 2])
81+
return nums[i] + nums[i - 1] + nums[i - 2];
82+
//if not then will find max element as (i-3)th largest element
83+
else if (i > 2) swapToGetMax(nums, i - 3);
84+
}
85+
return 0;
86+
}
87+
}
88+
89+
class C {
90+
// Runtime: 596 ms, faster than 40.00% of Dart online submissions for Largest Perimeter Triangle.
91+
// Memory Usage: 150.3 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
92+
int largestPerimeter(List<int> nums) {
93+
if (nums.length == 3) {
94+
if (nums[0] < nums[1] + nums[2] &&
95+
nums[1] < nums[0] + nums[2] &&
96+
nums[2] < nums[1] + nums[0])
97+
return nums[0] + nums[1] + nums[2];
98+
else
99+
return 0;
100+
}
101+
int n = nums.length;
102+
int maxi = 0;
103+
for (int i = 0; i < n - 2; i++) {
104+
for (int j = i + 1; j < n - 1; j++) {
105+
for (int k = j + 1; k < n; k++) {
106+
int a = nums[i];
107+
int b = nums[j];
108+
int c = nums[k];
109+
if (a < b + c && b < c + a && c < a + b) maxi = max(maxi, a + b + c);
110+
}
111+
}
112+
}
113+
if (maxi > 0) return maxi;
114+
return 0;
115+
}
116+
}
117+
118+
class D {
119+
int largestPerimeter(List<int> nums) {
120+
//sort the vector
121+
nums.sort();
122+
//any triangle sum of smaller two side greater than 3rd side...so we check that condition, a+b>c where a<b<c
123+
int maxPerimeter = 0;
124+
125+
for (int i = 0; i <= nums.length - 3; i++) {
126+
//check the triangle valid condition
127+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
128+
maxPerimeter =
129+
max(maxPerimeter, nums[i] + nums[i + 1] + nums[i + 2]); //find max
130+
}
131+
}
132+
133+
return maxPerimeter; //return the result
134+
}
135+
}
136+
137+
class F {
138+
int largestPerimeter(List<int> nums) {
139+
nums.sort((a, b) => a - b);
140+
int i = nums.length - 1;
141+
while (i >= 0) {
142+
if (nums[i] < nums[i - 1] + nums[i - 2]) {
143+
return nums[i] + nums[i - 1] + nums[i - 2];
144+
} else {
145+
i--;
146+
}
147+
}
148+
return 0;
149+
}
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "sort"
4+
5+
func largestPerimeter(nums []int) int {
6+
sort.Slice(nums, func(i, j int) bool {
7+
return nums[i] > nums[j]
8+
})
9+
for i := 0; i < len(nums)-2; i++ {
10+
a := nums[i]
11+
b := nums[i+1]
12+
c := nums[i+2]
13+
if a < (b + c) {
14+
return a + b + c
15+
}
16+
}
17+
return 0
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# 🔥 Largest Perimeter Triangle 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
Before diving into code.. Let us recall the basic rule for lengths of sides of a triangle
6+
i.e, sum of any two sides of a triangle should be greater than its third side.
7+
Simply, a + b > c
8+
where a, b, c are sides of triangle
9+
Approach:
10+
Since we need the largest perimeter from the given nums. We sort the array and traverse from the end
11+
12+
if nums[i] < nums[i-1] + nums[i-2], then
13+
we Stop and return the perimeter==> nums[i] + nums[i-1] + nums[i-2]
14+
15+
if there is no values that satisfies the given condition then
16+
we return 0
17+
18+
```dart
19+
class Solution {
20+
// Runtime: 426 ms, faster than 80.00% of Dart online submissions for Largest Perimeter Triangle.
21+
// Memory Usage: 149.6 MB, less than 100.00% of Dart online submissions for Largest Perimeter Triangle.
22+
23+
int largestPerimeter(List<int> nums) {
24+
nums.sort();
25+
for (int i = nums.length - 1; i > 1; --i)
26+
if (nums[i] < nums[i - 1] + nums[i - 2])
27+
return nums[i] + nums[i - 1] + nums[i - 2];
28+
return 0;
29+
}
30+
}
31+
```
32+
33+
## Solution - 2 Recursive
34+
35+
```dart
36+
class Solution {
37+
// Runtime: 514 ms, faster than 60.00% of Dart online submissions for Largest Perimeter Triangle.
38+
// Memory Usage: 150.2 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
39+
40+
//utility method to get max element at given index
41+
void swapToGetMax(List<int> nums, int index) {
42+
int max = 0, maxIndex = -1;
43+
for (int i = 0; i <= index; i++)
44+
if (max < nums[i]) {
45+
max = nums[i];
46+
maxIndex = i;
47+
}
48+
//actual swapping after finding max element in given range
49+
int temp = nums[index];
50+
nums[index] = max;
51+
nums[maxIndex] = temp;
52+
}
53+
54+
int largestPerimeter(List<int> nums) {
55+
//if array has only 3 elements
56+
if (nums.length == 3) {
57+
if (nums[0] < nums[1] + nums[2] &&
58+
nums[1] < nums[0] + nums[2] &&
59+
nums[2] < nums[1] + nums[0])
60+
return nums[0] + nums[1] + nums[2];
61+
else
62+
return 0;
63+
}
64+
//for more than 3 elements, without doing explicit sorting
65+
int n = nums.length;
66+
//here we are putting max element at last index
67+
swapToGetMax(nums, n - 1);
68+
//here we are putting max element at second last index
69+
swapToGetMax(nums, n - 2);
70+
//here we are putting max element at third last index
71+
swapToGetMax(nums, n - 3);
72+
//in loop checking if nums[i]<nums[i-1]+nums[i-2] which this triplet will form the max perimeter
73+
for (int i = n - 1; i >= 2; i--) {
74+
if (nums[i] < nums[i - 1] + nums[i - 2])
75+
return nums[i] + nums[i - 1] + nums[i - 2];
76+
//if not then will find max element as (i-3)th largest element
77+
else if (i > 2) swapToGetMax(nums, i - 3);
78+
}
79+
return 0;
80+
}
81+
}
82+
```
83+
84+
## Solution - 3
85+
86+
```dart
87+
class Solution {
88+
// Runtime: 596 ms, faster than 40.00% of Dart online submissions for Largest Perimeter Triangle.
89+
// Memory Usage: 150.3 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.
90+
int largestPerimeter(List<int> nums) {
91+
if (nums.length == 3) {
92+
if (nums[0] < nums[1] + nums[2] &&
93+
nums[1] < nums[0] + nums[2] &&
94+
nums[2] < nums[1] + nums[0])
95+
return nums[0] + nums[1] + nums[2];
96+
else
97+
return 0;
98+
}
99+
int n = nums.length;
100+
int maxi = 0;
101+
for (int i = 0; i < n - 2; i++) {
102+
for (int j = i + 1; j < n - 1; j++) {
103+
for (int k = j + 1; k < n; k++) {
104+
int a = nums[i];
105+
int b = nums[j];
106+
int c = nums[k];
107+
if (a < b + c && b < c + a && c < a + b) maxi = max(maxi, a + b + c);
108+
}
109+
}
110+
}
111+
if (maxi > 0) return maxi;
112+
return 0;
113+
}
114+
}
115+
```
116+
117+
## Solution - 4
118+
119+
```dart
120+
class Solution {
121+
int largestPerimeter(List<int> nums) {
122+
//sort the vector
123+
nums.sort();
124+
//any triangle sum of smaller two side greater than 3rd side...so we check that condition, a+b>c where a<b<c
125+
int maxPerimeter = 0;
126+
127+
for (int i = 0; i <= nums.length - 3; i++) {
128+
//check the triangle valid condition
129+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
130+
maxPerimeter =
131+
max(maxPerimeter, nums[i] + nums[i + 1] + nums[i + 2]); //find max
132+
}
133+
}
134+
135+
return maxPerimeter; //return the result
136+
}
137+
}
138+
```
139+
140+
## Solution - 5
141+
142+
```dart
143+
class Solution {
144+
int largestPerimeter(List<int> nums) {
145+
nums.sort((a, b) => a - b);
146+
int i = nums.length - 1;
147+
while (i >= 0) {
148+
if (nums[i] < nums[i - 1] + nums[i - 2]) {
149+
return nums[i] + nums[i - 1] + nums[i - 2];
150+
} else {
151+
i--;
152+
}
153+
}
154+
return 0;
155+
}
156+
}
157+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
9090
- [Palindrome Linked List](PalindromeLinkedList/palindrome_linked_list.dart)
9191
- [Increasing Triplet Subsequence](IncreasingTripletSubsequence/increasing_triplet_subsequence.dart)
9292
- [Shortest Word Distance](ShortestWordDistance/shortest_word_distance.dart)
93+
- [Largest Perimeter Triangle](LargestPerimeterTriangle/largest_perimeter_triangle.dart)
9394

9495
## Reach me via
9596

0 commit comments

Comments
 (0)