LeetCode 题解工作台

统计和小于目标的下标对数目

给你一个下标从 0 开始长度为 n 的整数数组 nums 和一个整数 target ,请你返回满足 0 且 nums[i] + nums[j] 的下标对 (i, j) 的数目。 示例 1: 输入: nums = [-1,1,2,3,1], target = 2 输出: 3 解释: 总共有 3 个下标…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 二分·搜索·答案·空间

bolt

答案摘要

我们先对数组 进行排序,然后枚举 ,在 $[0, j)$ 的范围内使用二分查找第一个大于等于 $target - nums[j]$ 的下标 ,那么 $[0, i)$ 的范围内的所有下标 都满足条件,因此答案增加 。 遍历结束后,我们返回答案。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始长度为 n 的整数数组 nums 和一个整数 target ,请你返回满足 0 <= i < j < nnums[i] + nums[j] < target 的下标对 (i, j) 的数目。

 

示例 1:

输入:nums = [-1,1,2,3,1], target = 2
输出:3
解释:总共有 3 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = 0 < target
- (0, 2) ,0 < 2 且 nums[0] + nums[2] = 1 < target 
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = 0 < target
注意 (0, 3) 不计入答案因为 nums[0] + nums[3] 不是严格小于 target 。

示例 2:

输入:nums = [-6,2,5,-2,-7,-1,3], target = -2
输出:10
解释:总共有 10 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = -4 < target
- (0, 3) ,0 < 3 且 nums[0] + nums[3] = -8 < target
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = -13 < target
- (0, 5) ,0 < 5 且 nums[0] + nums[5] = -7 < target
- (0, 6) ,0 < 6 且 nums[0] + nums[6] = -3 < target
- (1, 4) ,1 < 4 且 nums[1] + nums[4] = -5 < target
- (3, 4) ,3 < 4 且 nums[3] + nums[4] = -9 < target
- (3, 5) ,3 < 5 且 nums[3] + nums[5] = -3 < target
- (4, 5) ,4 < 5 且 nums[4] + nums[5] = -8 < target
- (4, 6) ,4 < 6 且 nums[4] + nums[6] = -4 < target

 

提示:

  • 1 <= nums.length == n <= 50
  • -50 <= nums[i], target <= 50
lightbulb

解题思路

方法一:排序 + 二分查找

我们先对数组 numsnums 进行排序,然后枚举 jj,在 [0,j)[0, j) 的范围内使用二分查找第一个大于等于 targetnums[j]target - nums[j] 的下标 ii,那么 [0,i)[0, i) 的范围内的所有下标 kk 都满足条件,因此答案增加 ii

遍历结束后,我们返回答案。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 是数组 numsnums 的长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def countPairs(self, nums: List[int], target: int) -> int:
        nums.sort()
        ans = 0
        for j, x in enumerate(nums):
            i = bisect_left(nums, target - x, hi=j)
            ans += i
        return ans
speed

复杂度分析

指标
时间complexity ranges from O(n^2) for brute-force to O(n log n) for sorting plus binary search per element. Space complexity is O(1) extra for pointers or O(n) if storing sorted array separately.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for solutions that avoid counting pairs multiple times.

  • question_mark

    Consider if sorting can simplify pair comparisons.

  • question_mark

    Check if binary search reduces the number of sum calculations per index.

warning

常见陷阱

外企场景
  • error

    Forgetting to only count pairs where i < j, leading to overcounting.

  • error

    Neglecting negative numbers and assuming all sums increase with indices.

  • error

    Using inefficient brute-force for large arrays without noticing sorting opportunities.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Count pairs whose sum is less than or equal to target.

  • arrow_right_alt

    Count triplets with sum less than target using similar binary search logic.

  • arrow_right_alt

    Return the actual list of valid pairs instead of just the count.

help

常见问题

外企场景

统计和小于目标的下标对数目题解:二分·搜索·答案·空间 | LeetCode #2824 简单