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

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

    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
    

提交回复
热议问题