-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Added _976.java #33
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.