How to know that a triangle triple exists in our array?

后端 未结 13 1418
[愿得一人]
[愿得一人] 2020-12-04 16:52

I was stuck in solving the following interview practice question:
I have to write a function:

int triangle(int[] A);

that given a zero-

13条回答
  •  生来不讨喜
    2020-12-04 17:17

    A 100/100 php solution: http://www.rationalplanet.com/php-related/maxproductofthree-demo-task-at-codility-com.html

    function solution($A) {
        $cnt = count($A);
        sort($A, SORT_NUMERIC);
        return max($A[0]*$A[1]*$A[$cnt-1], $A[$cnt-1]*$A[$cnt-2]*$A[$cnt-3]);
    }
    

提交回复
热议问题