[LintCode] Triangle Count
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public int triangleCount(int S[]) { // write your code here int res = 0; if(S.length == 0 || S == null) return res; Arrays.sort(S); for(int i = S.length - 1; i > 0; i--) { int l = 0; int r = i - 1; while(l < r) { if(S[l] + S[r] > S[i]){ res += (r-l); r--; } else l++; } } return res; } |
Leave A Comment