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

后端 未结 13 1356
[愿得一人]
[愿得一人] 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:25

    Here's simple solution in java with 100/100 score

    class Solution {
      public int solution(int[] A) {
        // write your code in Java SE 8
        Arrays.sort(A);
        for (int i = 0; i < A.length - 2; ++i) {
          long a = A[i] + (long) A[i + 1];
          long b = A[i + 2];
          if (a > b) {
            return 1;
          }
        }
        return 0;
      }
    }
    
    0 讨论(0)
  • 2020-12-04 17:25

    I have got another solution to count triangles. Its time complexity is O(N**3) and takes long time to process long arrays.

    Private Function solution(A As Integer()) As Integer
        ' write your code in VB.NET 4.0
        Dim result, size, i, j, k As Integer
            size = A.Length
            Array.Sort(A)
            For i = 0 To Size - 3
                j = i + 1
                While j < size
                    k = j + 1
                    While k < size
                        If A(i) + A(j) > A(k) Then
                            result += 1
                        End If
                        k += 1
                    End While
                    j += 1
                End While
            Next
            Return result
    End Function
    
    0 讨论(0)
  • 2020-12-04 17:25

    PHP Solution :

    function solution($x){
        sort($x);
        if (count($x) < 3) return 0; 
        for($i = 2; $i < count($x); $i++){
            if($x[$i] < $x[$i-1] + $x[$i-2]){
                return 1;
            }
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 17:26

    First of all, you can sort your sequence. For the sorted sequence it's enough to check that A[i] + A[j] > A[k] for i < j < k, because A[i] + A[k] > A[k] > A[j] etc., so the other 2 inequalities are automatically true.

    (From now on, i < j < k.)

    Next, it's enough to check that A[i] + A[j] > A[j+1], because other A[k] are even bigger (so if the inequality holds for some k, it holds for k = j + 1 as well).

    Next, it's enough to check that A[j-1] + A[j] > A[j+1], because other A[i] are even smaller (so if inequality holds for some i, it holds for i = j - 1 as well).

    So, you have just a linear check: you need to check whether for at least one j A[j-1] + A[j] > A[j+1] holds true.

    Altogether O(N log N) {sorting} + O(N) {check} = O(N log N).


    Addressing the comment about negative numbers: indeed, this is what I didn't consider in the original solution. Considering the negative numbers doesn't change the solution much, since no negative number can be a part of triangle triple. Indeed, if A[i], A[j] and A[k] form a triangle triple, then A[i] + A[j] > A[k], A[i] + A[k] > A[j], which implies 2 * A[i] + A[j] + A[k] > A[k] + A[j], hence 2 * A[i] > 0, so A[i] > 0 and by symmetry A[j] > 0, A[k] > 0.

    This means that we can safely remove negative numbers and zeroes from the sequence, which is done in O(log n) after sorting.

    0 讨论(0)
  • 2020-12-04 17:29

    Reversing the loop from Vlad solution for me seems to be easier to understand.

    The equation A[j-1] + A[j] > A[j+1] could be changed to A[k-2]+A[k-1]>A[k]. Explained in words, the sum of the last two largest numbers should be bigger than current largest value being checked (A[k]). If the result of adding the last two largest numbers(A[k-2] and A[k-1]) is not bigger than A[k], we can go to the next iteration of the loop.

    In addition, we can add the check for negative numbers that Vlad mentioned, and stop the loop earlier.

    int solution(vector<int> &A) {
        sort(A.begin(),A.end());
        for (int k = A.size()-1; k >= 2 && A[k-2] > 0 ; --k) {
            if ( A[k-2]+A[k-1] > A[k] )
                return 1;
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 17:30

    Do a quick sort first, this will generally take nlogn. And you can omit the third loop by binary search, which can take log(n). So altogether, the complexity is reduced to n^2log(n).

    0 讨论(0)
提交回复
热议问题