Skip to content

Added _976.java #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Leetcode.iml

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!

| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
|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
|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
|970|[Powerful Integers](https://leetcode.com/problems/powerful-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_970.java) | O(?) | O(1) | |Easy| Math
|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
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/fishercoder/solutions/_976.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fishercoder.solutions;

import java.util.Arrays;

/**
* 976. Largest Perimeter Triangle
*
* Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3
* of these lengths.
*
* If it is impossible to form any triangle of non-zero area, return 0.
* */

public class _976 {

public static class Solution1 {
public int largestPerimeter(int[] A) {
Arrays.sort(A);
int n = A.length;

for (int i = n - 1; i > 1; i--) {
if (A[i] < A[i - 1] + A[i - 2]) {
return A[i] + A[i - 1] + A[i - 2];
}
}

return 0;
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/fishercoder/_976Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fishercoder;

import com.fishercoder.solutions._976;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class _976Test {

private static _976.Solution1 test;

@BeforeClass
public static void setUp() {
test = new _976.Solution1();
}

@Test
public void test1() {
assertEquals(5, test.largestPerimeter(new int[]{2, 1, 2}));
}

@Test
public void test2() {
assertEquals(0, test.largestPerimeter(new int[]{1, 2, 1}));
}

@Test
public void test3() {
assertEquals(10, test.largestPerimeter(new int[]{3, 2, 3, 4}));
}

@Test
public void test4() {
assertEquals(8, test.largestPerimeter(new int[]{3, 6, 2, 3}));
}
}