Created
September 11, 2016 15:10
-
-
Save arielm/d29c7fd200c75f233e47f35292f96149 to your computer and use it in GitHub Desktop.
Solution (81%) to Triangle problem on Codility (https://codility.com/programmers/task/triangle)
This file contains 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
/* | |
* CORRECTNESS: 100% | |
* PERFORMANCE: 50% | |
* | |
* FOR A 100% SOLUTION, SEE: | |
* https://codesays.com/2014/solution-to-triangle-by-codility/ | |
*/ | |
class Solution | |
{ | |
public int solution(int[] A) | |
{ | |
int N = A.length; | |
if (N >= 3) | |
{ | |
for (int i = 0; i < N - 2; i++) | |
{ | |
int N2 = (N - 2) - i; | |
for (int i2 = 0; i2 < N2; i2++) | |
{ | |
int P = i; | |
int Q = i + 1; | |
int R = i + 2 + i2; | |
if (checkTriplet(A, P, Q, R)) | |
{ | |
return 1; | |
} | |
} | |
int N3 = N2 - 1; | |
for (int i3 = 0; i3 < N3; i3++) | |
{ | |
int P = i; | |
int Q = i + 2; | |
int R = i + 3 + i3; | |
if (checkTriplet(A, P, Q, R)) | |
{ | |
return 1; | |
} | |
} | |
} | |
} | |
return 0; | |
} | |
boolean checkTriplet(int[] A, int P, int Q, int R) | |
{ | |
if ((long)A[P] + (long)A[Q] > (long)A[R]) | |
{ | |
if ((long)A[Q] + (long)A[R] > (long)A[P]) | |
{ | |
return ((long)A[R] + (long)A[P] > (long)A[Q]); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment