Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2824~2831 #1477

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/2800-2899/2823.Deep Object Filter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn = (x) => Array.isArray(x)
<ul>
<li><code>fn</code> is a function that returns a boolean value</li>
<li><code>obj</code> is a valid JSON object</li>
<li><code>2 &lt;= JSON.stringify(obj).length &lt;= 10**5</code></li>
<li><code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
</ul>

## Solutions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# [2824. 统计和小于目标的下标对数目](https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target)

[English Version](/solution/2800-2899/2824.Count%20Pairs%20Whose%20Sum%20is%20Less%20than%20Target/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>target</code>&nbsp;,请你返回满足&nbsp;<code>0 &lt;= i &lt; j &lt; n</code> 且 <code>nums[i] + nums[j] &lt; target</code>&nbsp;的下标对&nbsp;<code>(i, j)</code>&nbsp;的数目。

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

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

<p><strong class="example">示例 2:</strong></p>

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

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length == n &lt;= 50</code></li>
<li><code>-50 &lt;= nums[i], target &lt;= 50</code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:排序 + 二分查找**

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

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

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
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
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
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;
}
}
```

### **C++**

```cpp
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;
}
};
```

### **Go**

```go
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
}
```

### **TypeScript**

```ts
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;
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# [2824. Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target)

[中文文档](/solution/2800-2899/2824.Count%20Pairs%20Whose%20Sum%20is%20Less%20than%20Target/README.md)

## Description

Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, return <em>the number of pairs</em> <code>(i, j)</code> <em>where</em> <code>0 &lt;= i &lt; j &lt; n</code> <em>and</em> <code>nums[i] + nums[j] &lt; target</code>.

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length == n &lt;= 50</code></li>
<li><code>-50 &lt;= nums[i], target &lt;= 50</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python
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
```

### **Java**

```java
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;
}
}
```

### **C++**

```cpp
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;
}
};
```

### **Go**

```go
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
}
```

### **TypeScript**

```ts
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;
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
}
}
Loading