返回介绍

solution / 2800-2899 / 2824.Count Pairs Whose Sum is Less than Target / README_EN

发布于 2024-06-17 01:02:59 字数 4937 浏览 0 评论 0 收藏 0

2824. Count Pairs Whose Sum is Less than Target

中文文档

Description

Given a 0-indexed integer array nums of length n and an integer target, return _the number of pairs_ (i, j) _where_ 0 <= i < j < n _and_ nums[i] + nums[j] < target.

 

Example 1:

Input: nums = [-1,1,2,3,1], target = 2
Output: 3
Explanation: There are 3 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target
- (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target 
- (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.

Example 2:

Input: nums = [-6,2,5,-2,-7,-1,3], target = -2
Output: 10
Explanation: There are 10 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target
- (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target
- (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target
- (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target
- (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target
- (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target
- (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target
- (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target
- (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target
- (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target

 

Constraints:

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

Solutions

Solution 1: Sorting + Binary Search

First, we sort the array $nums$. Then, for each $j$, we use binary search in the range $[0, j)$ to find the first index $i$ that is greater than or equal to $target - nums[j]$. All indices $k$ in the range $[0, i)$ meet the condition, so the answer increases by $i$.

After the traversal, we return the answer.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.

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
class Solution {
  public int countPairs(List<Integer> nums, int target) {
    Collections.sort(nums);
    int ans = 0;
    for (int j = 0; j < nums.size(); ++j) {
      int x = nums.get(j);
      int i = search(nums, target - x, j);
      ans += i;
    }
    return ans;
  }

  private int search(List<Integer> nums, int x, int r) {
    int l = 0;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (nums.get(mid) >= x) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return l;
  }
}
class Solution {
public:
  int countPairs(vector<int>& nums, int target) {
    sort(nums.begin(), nums.end());
    int ans = 0;
    for (int j = 0; j < nums.size(); ++j) {
      int i = lower_bound(nums.begin(), nums.begin() + j, target - nums[j]) - nums.begin();
      ans += i;
    }
    return ans;
  }
};
func countPairs(nums []int, target int) (ans int) {
  sort.Ints(nums)
  for j, x := range nums {
    i := sort.SearchInts(nums[:j], target-x)
    ans += i
  }
  return
}
function countPairs(nums: number[], target: number): number {
  nums.sort((a, b) => a - b);
  let ans = 0;
  const search = (x: number, r: number): number => {
    let l = 0;
    while (l < r) {
      const mid = (l + r) >> 1;
      if (nums[mid] >= x) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return l;
  };
  for (let j = 0; j < nums.length; ++j) {
    const i = search(target - nums[j], j);
    ans += i;
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文