⚡Flashcard Drill➔
NeetCode #217LC-259MediumTwo Pointers
← Back to All Problems

#217 · #259 · 3Sum Smaller(较小的三数之和)

📌 Problem Statement & Constraints

Given an array of integers nums and an integer target, return the number of index triplets i < j < k such that nums[i] + nums[j] + nums[k] < target. Constraints: 0 <= nums.length <= 3500, -100 <= nums[i] <= 100, -100 <= target <= 100.

💡 Core Algorithmic Approaches

  1. Sort the array. Fix the smallest index i, then use two pointers on the remaining suffix.
  2. If nums[i] + nums[l] + nums[r] < target, then every pair (l, l+1), ..., (l, r) also works, contributing r - l triplets at once.
  3. That counting shortcut is what turns the O(n^3) brute force into O(n^2).
  4. If the sum is too large, decrement r; otherwise increment l.

💻 Benchmark Python3 Implementation

class Solution:
    def threeSumSmaller(self, nums: List[int], target: int) -> int:
        nums.sort()
        n = len(nums)
        res = 0
        for i in range(n - 2):
            l, r = i + 1, n - 1
            while l < r:
                if nums[i] + nums[l] + nums[r] < target:
                    res += r - l       # all pairs with this l work
                    l += 1
                else:
                    r -= 1
        return res

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n^2): the sort plus a two-pointer scan for each fixed index.
💾 Space Complexity
O(1) beyond the sort.

⚠️ Interview Pitfalls & Follow-ups

  • Counting one triplet at a time: O(n^3) and unnecessary; r - l counts them in bulk.
  • Using <= for the comparison: the sum must be strictly less than target.
  • Forgetting the sort: the bulk counting relies on the sorted order making all pairs with a given l valid.
  • Advancing r on a valid sum: the correct move is l += 1, since a larger l may still admit valid pairs.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Two Pointers Problems

中文速览与面试要点

较小的三数之和

给你一个长度为 n 的整数数组 nums 和一个整数 target,请你统计并返回满足 i &lt; j &lt; k 且 nums[i] + nums[j] + nums[k] &lt; target 的三元组数目。约束:1 ≤ nums.length ≤ 3500;-100 ≤ nums[i] ≤ 100;-100 ≤ target ≤ 100。

核心要点

  • 排序 + 固定最小值 + 双指针计数(标准解) :排序后,外层枚举 i 作为三元组的最小值,内层用相向双指针 l、r 在 i+1..n−1 中统计。
  • 关键计数技巧 :若 nums[i] + nums[l] + nums[r] &lt; target ,则由于数组升序, nums[l] 与 nums[l+1..r] 中 任意一个 配对都满足条件,因此可以一次性累加 r − l 个,并令 l += 1 。
  • 为什么可以一次加多个 :固定 i 与 l 时, nums[r] 是当前可选的最大值;既然它与 nums[l] 的和都满足,那么更小的 nums[j] (l &lt; j &lt; r)必然也满足。
  • 与 15(三数之和)的区别 :15 要求恰好等于 0 且需要去重;本题求「小于 target」的 个数 (按下标计数,不去重)。

常见陷阱

  • 满足条件时只 count += 1 :会严重少计。必须一次累加 r − l 个(因为 nums[l] 与 nums[l+1..r] 中任一个配对都合法)。
  • 按值去重 :本题求的是 下标三元组 的个数,不需要去重(与 15 题不同)。
  • 满足条件时让 r 左移 :方向反了。 nums[l] + nums[r] 已满足,应增大 l 以探索新的组合。
  • 用三重循环 :O(n³) 在 n = 3500 时是 4 × 10^10,必然超时。

参考实现

class Solution:
    def threeSumSmaller(self, nums: list[int], target: int) -> int:
        nums.sort()
        n = len(nums)
        count = 0
        for i in range(n - 2):
            l, r = i + 1, n - 1
            while l < r:
                if nums[i] + nums[l] + nums[r] < target:
                    # nums[l] 与 nums[l+1..r] 中任一个都满足 -> 一次累加 r-l 个
                    count += r - l
                    l += 1
                else:
                    r -= 1
        return count