![JAR search and dependency download from the Maven repository](/logo.png)
g0901_1000.s0976_largest_perimeter_triangle.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0901_1000.s0976_largest_perimeter_triangle;
// #Easy #Array #Math #Sorting #Greedy #Programming_Skills_I_Day_3_Conditional_Statements
// #2024_12_19_Time_7_ms_(99.33%)_Space_45.5_MB_(8.45%)
import java.util.Arrays;
public class Solution {
public int largestPerimeter(int[] nums) {
// for non zero area of triangle (c < a+b)
Arrays.sort(nums);
for (int i = nums.length - 1; i >= 2; i--) {
if (nums[i] < nums[i - 1] + nums[i - 2]) {
return nums[i] + nums[i - 1] + nums[i - 2];
}
}
return 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy