Skip to content

Commit 83a56b3

Browse files
committed
Time: 23 ms (100.00%), Space: 57.3 MB (42.09%) - LeetHub
1 parent d4a0a05 commit 83a56b3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public long countFairPairs(int[] nums, int lower, int upper) {
3+
Arrays.sort(nums);
4+
5+
return findCountTarget(nums,upper+1)-findCountTarget(nums,lower);
6+
}
7+
8+
public long findCountTarget(int[] nums , int target){
9+
long cnt=0;
10+
int start = 0;
11+
int end = nums.length-1;
12+
13+
while(start<=end){
14+
if(nums[start]+nums[end]<target){
15+
cnt += end-start;
16+
start++;
17+
}else{
18+
end--;
19+
}
20+
}
21+
return cnt;
22+
}
23+
24+
25+
}

0 commit comments

Comments
 (0)