1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| static int[] maximumPerimeterTriangle(int[] sticks) {
int answerArr[] = new int[3]; Arrays.sort(sticks);
boolean isNonDegenerateTriangle = false; for (int i = 0; i < sticks.length - 2; i++) { int a = sticks[i]; int b = sticks[i + 1]; int c = sticks[i + 2];
if ((a + b) > c && (a + c) > b && (b + c) > a) { answerArr[0] = (int) a; answerArr[1] = (int) b; answerArr[2] = (int) c; isNonDegenerateTriangle = true; } }
if (!isNonDegenerateTriangle) { return new int[]{-1}; }
System.out.print(Arrays.toString(answerArr)); return answerArr; }
public static void main(String[] args) { System.out.println(maximumPerimeterTriangle(new int[]{1, 2, 3, 4, 5, 10}) + ", ans: [3,4,5]"); System.out.println(maximumPerimeterTriangle(new int[]{1, 1, 1, 3, 3}) + ", ans: [1,3,3]"); System.out.println(maximumPerimeterTriangle(new int[]{1,1,1,2,3,5}) + ", ans: [1,1,1]"); }
|