↓Skip to main content
  1. LeetCode/

LeetCode 611: Valid Triangle Number

·2 mins· ·
LeetCode Medium Array Two-Pointers Binary-Search Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-27

  • Submissions: Wrong Answer → Accepted ×2

Approach
#

After sorting, a triangle only needs the two shorter sides to exceed the longest (Two Pointers Template 4). Fix the longest side k, with i from 0 and j from k - 1:

  • nums[i] + nums[j] > nums[k]: any i in i..j-1 is larger still, so add j - i at once and move j left.
  • Otherwise move i right.

My Attempts
#

Attempt 1 added j - 1 (a typo): for [4, 7, 9] with i = 0 and j = 1 it should add 1 but added 0, returning 0.

Solution
#

class Solution:
    def triangleNumber(self, nums: List[int]) -> int:
        nums.sort()
        res = 0
        for k in range(2, len(nums)):
            i, j = 0, k - 1
            while i < j:
                if nums[i] + nums[j] > nums[k]:
                    res += j - i
                    j -= 1
                else:
                    i += 1
        return res

Complexity
#

Time O(n²), space O(1) besides sorting.

Mistakes to Avoid
#

  • Add j - i at a time, not j - 1.

Interview Explanation
#

After sorting I fix the longest side and move two pointers over the shorter ones: when the sum is large enough every left choice in between works, so I add j - i and move right inward; otherwise the left pointer advances. Time O(n²).

Related

LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based
LeetCode 11: Container With Most Water
·2 mins
LeetCode Medium Array Two-Pointers Greedy
LeetCode 11 note: start pointers at both ends and always move the shorter side; moving the taller side only narrows the width without raising the height
LeetCode 275: H-Index II
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 275 note: with sorted citations, find the first i where citations[i] >= n - i; the answer is n - i, in O(log n) with binary search
LeetCode 540: Single Element in a Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 540 note: before the single element every pair starts at an even index, so binary search whether nums[mid] equals nums[mid + 1] at an even mid
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices