Skip to content

Commit b4d8610

Browse files
varunu28fishercoder1534
authored andcommitted
Added _976.java (#33)
Added _976.java Added _976.java
1 parent 8543fc1 commit b4d8610

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

Leetcode.iml

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_976.java) | O(nlogn) | O(1) | |Easy| Math Array
3233
|973|[K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_973.java) | O(nlogn) | O(K) | |Easy| Math Sort
3334
|970|[Powerful Integers](https://leetcode.com/problems/powerful-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_970.java) | O(?) | O(1) | |Easy| Math
3435
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | O(hlogn) | O(n) | |Medium| Hash Table, String
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 976. Largest Perimeter Triangle
7+
*
8+
* Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3
9+
* of these lengths.
10+
*
11+
* If it is impossible to form any triangle of non-zero area, return 0.
12+
* */
13+
14+
public class _976 {
15+
16+
public static class Solution1 {
17+
public int largestPerimeter(int[] A) {
18+
Arrays.sort(A);
19+
int n = A.length;
20+
21+
for (int i = n - 1; i > 1; i--) {
22+
if (A[i] < A[i - 1] + A[i - 2]) {
23+
return A[i] + A[i - 1] + A[i - 2];
24+
}
25+
}
26+
27+
return 0;
28+
}
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._976;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _976Test {
10+
11+
private static _976.Solution1 test;
12+
13+
@BeforeClass
14+
public static void setUp() {
15+
test = new _976.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(5, test.largestPerimeter(new int[]{2, 1, 2}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(0, test.largestPerimeter(new int[]{1, 2, 1}));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(10, test.largestPerimeter(new int[]{3, 2, 3, 4}));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(8, test.largestPerimeter(new int[]{3, 6, 2, 3}));
36+
}
37+
}

0 commit comments

Comments
 (0)