Leetcode 611 Solution

This article provides solution to leetcode question 611 (valid-triangle-number)

https://leetcode.com/problems/valid-triangle-number

Solution

class Solution:
    def triangleNumber(self, nums: List[int]) -> int:
        nums.sort()
        nums = [num for num in nums if num > 0]

        ans = 0
        for i in range(len(nums) - 2):
            r = i + 1
            for j in range(i + 1, len(nums) - 1):
                while r < len(nums) - 1 and nums[i] + nums[j] > nums[r + 1]:
                    r += 1
                ans += max(r - j, 0)

        return ans