Simple Python O(n^2)


  • 0
    class Solution(object):
        def triangleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            res = 0
            nums.sort()
            for i in xrange(2, len(nums)):
                start, end = 0, i - 1
                target = nums[i]
                while start < end:
                    if nums[start] + nums[end] > target:
                        res += end - start
                        end -= 1
                    else:
                        start += 1
            return res
                    
                
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.