Skip to content

Commit 128e459

Browse files
authored
feat: add solutions to lc problems: No.2824~2831 (#1477)
* No.2824.Count Pairs Whose Sum is Less than Target * No.2825.Make String a Subsequence Using Cyclic Increments * No.2826.Sorting Three Groups * No.2827.Number of Beautiful Integers in the Range * No.2828.Check if a String Is an Acronym of Words * No.2829.Determine the Minimum Sum of a k-avoiding Array * No.2830.Maximize the Profit as the Salesman * No.2831.Find the Longest Equal Subarray
1 parent 493eb33 commit 128e459

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3900
-1
lines changed

solution/2800-2899/2823.Deep Object Filter/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn = (x) => Array.isArray(x)
5555
<ul>
5656
<li><code>fn</code> is a function that returns a boolean value</li>
5757
<li><code>obj</code> is a valid JSON object</li>
58-
<li><code>2 &lt;= JSON.stringify(obj).length &lt;= 10**5</code></li>
58+
<li><code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
5959
</ul>
6060

6161
## Solutions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# [2824. 统计和小于目标的下标对数目](https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target)
2+
3+
[English Version](/solution/2800-2899/2824.Count%20Pairs%20Whose%20Sum%20is%20Less%20than%20Target/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
给你一个下标从 <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;的数目。
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong class="example">示例 1:</strong></p>
14+
15+
<pre>
16+
<b>输入:</b>nums = [-1,1,2,3,1], target = 2
17+
<b>输出:</b>3
18+
<b>解释:</b>总共有 3 个下标对满足题目描述:
19+
- (0, 1) ,0 &lt; 1 且 nums[0] + nums[1] = 0 &lt; target
20+
- (0, 2) ,0 &lt; 2 且 nums[0] + nums[2] = 1 &lt; target
21+
- (0, 4) ,0 &lt; 4 且 nums[0] + nums[4] = 0 &lt; target
22+
注意 (0, 3) 不计入答案因为 nums[0] + nums[3] 不是严格小于 target 。
23+
</pre>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
27+
<pre>
28+
<b>输入:</b>nums = [-6,2,5,-2,-7,-1,3], target = -2
29+
<b>输出:</b>10
30+
<b>解释:</b>总共有 10 个下标对满足题目描述:
31+
- (0, 1) ,0 &lt; 1 且 nums[0] + nums[1] = -4 &lt; target
32+
- (0, 3) ,0 &lt; 3 且 nums[0] + nums[3] = -8 &lt; target
33+
- (0, 4) ,0 &lt; 4 且 nums[0] + nums[4] = -13 &lt; target
34+
- (0, 5) ,0 &lt; 5 且 nums[0] + nums[5] = -7 &lt; target
35+
- (0, 6) ,0 &lt; 6 且 nums[0] + nums[6] = -3 &lt; target
36+
- (1, 4) ,1 &lt; 4 且 nums[1] + nums[4] = -5 &lt; target
37+
- (3, 4) ,3 &lt; 4 且 nums[3] + nums[4] = -9 &lt; target
38+
- (3, 5) ,3 &lt; 5 且 nums[3] + nums[5] = -3 &lt; target
39+
- (4, 5) ,4 &lt; 5 且 nums[4] + nums[5] = -8 &lt; target
40+
- (4, 6) ,4 &lt; 6 且 nums[4] + nums[6] = -4 &lt; target
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length == n &lt;= 50</code></li>
49+
<li><code>-50 &lt;= nums[i], target &lt;= 50</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
**方法一:排序 + 二分查找**
57+
58+
我们先对数组 $nums$ 进行排序,然后枚举 $j$,在 $[0, j)$ 的范围内使用二分查找第一个大于等于 $target - nums[j]$ 的下标 $i$,那么 $[0, i)$ 的范围内的所有下标 $k$ 都满足条件,因此答案增加 $i$。
59+
60+
遍历结束后,我们返回答案。
61+
62+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $nums$ 的长度。
63+
64+
<!-- tabs:start -->
65+
66+
### **Python3**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```python
71+
class Solution:
72+
def countPairs(self, nums: List[int], target: int) -> int:
73+
nums.sort()
74+
ans = 0
75+
for j, x in enumerate(nums):
76+
i = bisect_left(nums, target - x, hi=j)
77+
ans += i
78+
return ans
79+
```
80+
81+
### **Java**
82+
83+
<!-- 这里可写当前语言的特殊实现逻辑 -->
84+
85+
```java
86+
class Solution {
87+
public int countPairs(List<Integer> nums, int target) {
88+
Collections.sort(nums);
89+
int ans = 0;
90+
for (int j = 0; j < nums.size(); ++j) {
91+
int x = nums.get(j);
92+
int i = search(nums, target - x, j);
93+
ans += i;
94+
}
95+
return ans;
96+
}
97+
98+
private int search(List<Integer> nums, int x, int r) {
99+
int l = 0;
100+
while (l < r) {
101+
int mid = (l + r) >> 1;
102+
if (nums.get(mid) >= x) {
103+
r = mid;
104+
} else {
105+
l = mid + 1;
106+
}
107+
}
108+
return l;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int countPairs(vector<int>& nums, int target) {
119+
sort(nums.begin(), nums.end());
120+
int ans = 0;
121+
for (int j = 0; j < nums.size(); ++j) {
122+
int i = lower_bound(nums.begin(), nums.begin() + j, target - nums[j]) - nums.begin();
123+
ans += i;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
130+
### **Go**
131+
132+
```go
133+
func countPairs(nums []int, target int) (ans int) {
134+
sort.Ints(nums)
135+
for j, x := range nums {
136+
i := sort.SearchInts(nums[:j], target-x)
137+
ans += i
138+
}
139+
return
140+
}
141+
```
142+
143+
### **TypeScript**
144+
145+
```ts
146+
function countPairs(nums: number[], target: number): number {
147+
nums.sort((a, b) => a - b);
148+
let ans = 0;
149+
const search = (x: number, r: number): number => {
150+
let l = 0;
151+
while (l < r) {
152+
const mid = (l + r) >> 1;
153+
if (nums[mid] >= x) {
154+
r = mid;
155+
} else {
156+
l = mid + 1;
157+
}
158+
}
159+
return l;
160+
};
161+
for (let j = 0; j < nums.length; ++j) {
162+
const i = search(target - nums[j], j);
163+
ans += i;
164+
}
165+
return ans;
166+
}
167+
```
168+
169+
### **...**
170+
171+
```
172+
173+
```
174+
175+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# [2824. Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target)
2+
3+
[中文文档](/solution/2800-2899/2824.Count%20Pairs%20Whose%20Sum%20is%20Less%20than%20Target/README.md)
4+
5+
## Description
6+
7+
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>.
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums = [-1,1,2,3,1], target = 2
14+
<strong>Output:</strong> 3
15+
<strong>Explanation:</strong> There are 3 pairs of indices that satisfy the conditions in the statement:
16+
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = 0 &lt; target
17+
- (0, 2) since 0 &lt; 2 and nums[0] + nums[2] = 1 &lt; target
18+
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = 0 &lt; target
19+
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [-6,2,5,-2,-7,-1,3], target = -2
26+
<strong>Output:</strong> 10
27+
<strong>Explanation:</strong> There are 10 pairs of indices that satisfy the conditions in the statement:
28+
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = -4 &lt; target
29+
- (0, 3) since 0 &lt; 3 and nums[0] + nums[3] = -8 &lt; target
30+
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = -13 &lt; target
31+
- (0, 5) since 0 &lt; 5 and nums[0] + nums[5] = -7 &lt; target
32+
- (0, 6) since 0 &lt; 6 and nums[0] + nums[6] = -3 &lt; target
33+
- (1, 4) since 1 &lt; 4 and nums[1] + nums[4] = -5 &lt; target
34+
- (3, 4) since 3 &lt; 4 and nums[3] + nums[4] = -9 &lt; target
35+
- (3, 5) since 3 &lt; 5 and nums[3] + nums[5] = -3 &lt; target
36+
- (4, 5) since 4 &lt; 5 and nums[4] + nums[5] = -8 &lt; target
37+
- (4, 6) since 4 &lt; 6 and nums[4] + nums[6] = -4 &lt; target
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length == n &lt;= 50</code></li>
45+
<li><code>-50 &lt;= nums[i], target &lt;= 50</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
class Solution:
56+
def countPairs(self, nums: List[int], target: int) -> int:
57+
nums.sort()
58+
ans = 0
59+
for j, x in enumerate(nums):
60+
i = bisect_left(nums, target - x, hi=j)
61+
ans += i
62+
return ans
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
class Solution {
69+
public int countPairs(List<Integer> nums, int target) {
70+
Collections.sort(nums);
71+
int ans = 0;
72+
for (int j = 0; j < nums.size(); ++j) {
73+
int x = nums.get(j);
74+
int i = search(nums, target - x, j);
75+
ans += i;
76+
}
77+
return ans;
78+
}
79+
80+
private int search(List<Integer> nums, int x, int r) {
81+
int l = 0;
82+
while (l < r) {
83+
int mid = (l + r) >> 1;
84+
if (nums.get(mid) >= x) {
85+
r = mid;
86+
} else {
87+
l = mid + 1;
88+
}
89+
}
90+
return l;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int countPairs(vector<int>& nums, int target) {
101+
sort(nums.begin(), nums.end());
102+
int ans = 0;
103+
for (int j = 0; j < nums.size(); ++j) {
104+
int i = lower_bound(nums.begin(), nums.begin() + j, target - nums[j]) - nums.begin();
105+
ans += i;
106+
}
107+
return ans;
108+
}
109+
};
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
func countPairs(nums []int, target int) (ans int) {
116+
sort.Ints(nums)
117+
for j, x := range nums {
118+
i := sort.SearchInts(nums[:j], target-x)
119+
ans += i
120+
}
121+
return
122+
}
123+
```
124+
125+
### **TypeScript**
126+
127+
```ts
128+
function countPairs(nums: number[], target: number): number {
129+
nums.sort((a, b) => a - b);
130+
let ans = 0;
131+
const search = (x: number, r: number): number => {
132+
let l = 0;
133+
while (l < r) {
134+
const mid = (l + r) >> 1;
135+
if (nums[mid] >= x) {
136+
r = mid;
137+
} else {
138+
l = mid + 1;
139+
}
140+
}
141+
return l;
142+
};
143+
for (let j = 0; j < nums.length; ++j) {
144+
const i = search(target - nums[j], j);
145+
ans += i;
146+
}
147+
return ans;
148+
}
149+
```
150+
151+
### **...**
152+
153+
```
154+
155+
```
156+
157+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int countPairs(vector<int>& nums, int target) {
4+
sort(nums.begin(), nums.end());
5+
int ans = 0;
6+
for (int j = 0; j < nums.size(); ++j) {
7+
int i = lower_bound(nums.begin(), nums.begin() + j, target - nums[j]) - nums.begin();
8+
ans += i;
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func countPairs(nums []int, target int) (ans int) {
2+
sort.Ints(nums)
3+
for j, x := range nums {
4+
i := sort.SearchInts(nums[:j], target-x)
5+
ans += i
6+
}
7+
return
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int countPairs(List<Integer> nums, int target) {
3+
Collections.sort(nums);
4+
int ans = 0;
5+
for (int j = 0; j < nums.size(); ++j) {
6+
int x = nums.get(j);
7+
int i = search(nums, target - x, j);
8+
ans += i;
9+
}
10+
return ans;
11+
}
12+
13+
private int search(List<Integer> nums, int x, int r) {
14+
int l = 0;
15+
while (l < r) {
16+
int mid = (l + r) >> 1;
17+
if (nums.get(mid) >= x) {
18+
r = mid;
19+
} else {
20+
l = mid + 1;
21+
}
22+
}
23+
return l;
24+
}
25+
}

0 commit comments

Comments
 (0)