-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 16.6 MB (…
…62.67%)
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 <= i < j < n</code> <em>and</em> <code>nums[i] + nums[j] < target</code>. | ||
<p> </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 < 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. | ||
</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 < 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 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length == n <= 50</code></li> | ||
<li><code>-50 <= nums[i], target <= 50</code></li> | ||
</ul> |
20 changes: 20 additions & 0 deletions
20
2917-count-pairs-whose-sum-is-less-than-target/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Approach: Two Pointer | ||
|
||
# Time: O(n log n) | ||
# Space: O(n) | ||
|
||
class Solution: | ||
def countPairs(self, nums: List[int], target: int) -> int: | ||
nums.sort() | ||
count = 0 | ||
left, right = 0, len(nums) - 1 | ||
|
||
while left < right: | ||
if (nums[left] + nums[right]) < target: | ||
count += right - left | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return count | ||
|