Skip to content

Commit 4cc5f3e

Browse files
committed
+ problem 493
1 parent 94f6c81 commit 4cc5f3e

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 493. Reverse Pairs
2+
Given an integer array `nums`, return *the number of **reverse pairs** in the array*.
3+
4+
A **reverse pair** is a pair `(i, j)` where:
5+
6+
* `0 <= i < j < nums.length` and
7+
* `nums[i] > 2 * nums[j]`.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> nums = [1,3,2,3,1]
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong> The reverse pairs are:
14+
(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
15+
(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> nums = [2,4,3,5,1]
21+
<strong>Output:</strong> 3
22+
<strong>Explanation:</strong> The reverse pairs are:
23+
(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
24+
(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
25+
(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1
26+
</pre>
27+
28+
#### Constraints:
29+
* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
30+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
31+
32+
## Solutions (Python)
33+
34+
### 1. Solution
35+
```Python
36+
from sortedcontainers import SortedList
37+
38+
39+
class Solution:
40+
def reversePairs(self, nums: List[int]) -> int:
41+
sortednums = SortedList()
42+
ret = 0
43+
44+
for num in nums:
45+
ret += len(sortednums) - sortednums.bisect_right(2 * num)
46+
sortednums.add(num)
47+
48+
return ret
49+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 493. 翻转对
2+
给定一个数组 `nums` ,如果 `i < j``nums[i] > 2*nums[j]` 我们就将 `(i, j)` 称作一个***重要翻转对***
3+
4+
你需要返回给定数组中的重要翻转对的数量。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [1,3,2,3,1]
9+
<strong>输出:</strong> 2
10+
</pre>
11+
12+
#### 示例 2:
13+
<pre>
14+
<strong>输入:</strong> nums = [2,4,3,5,1]
15+
<strong>输出:</strong> 3
16+
</pre>
17+
18+
#### 注意:
19+
1. 给定数组的长度不会超过`50000`
20+
2. 输入数组中的所有数字都在32位整数的表示范围内。
21+
22+
## 题解 (Python)
23+
24+
### 1. 题解
25+
```Python
26+
from sortedcontainers import SortedList
27+
28+
29+
class Solution:
30+
def reversePairs(self, nums: List[int]) -> int:
31+
sortednums = SortedList()
32+
ret = 0
33+
34+
for num in nums:
35+
ret += len(sortednums) - sortednums.bisect_right(2 * num)
36+
sortednums.add(num)
37+
38+
return ret
39+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class Solution:
5+
def reversePairs(self, nums: List[int]) -> int:
6+
sortednums = SortedList()
7+
ret = 0
8+
9+
for num in nums:
10+
ret += len(sortednums) - sortednums.bisect_right(2 * num)
11+
sortednums.add(num)
12+
13+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
[483][483l] |[Smallest Good Base][483] |![py]
292292
[485][485l] |[Max Consecutive Ones][485] |![rs]
293293
[492][492l] |[Construct the Rectangle][492] |![rs]
294+
[493][493l] |[Reverse Pairs][493] |![py]
294295
[494][494l] |[Target Sum][494] |![rb]&nbsp;&nbsp;![rs]
295296
[495][495l] |[Teemo Attacking][495] |![rs]
296297
[496][496l] |[Next Greater Element I][496] |![rs]
@@ -1615,6 +1616,7 @@
16151616
[483]:Problemset/0483-Smallest%20Good%20Base/README.md#483-smallest-good-base
16161617
[485]:Problemset/0485-Max%20Consecutive%20Ones/README.md#485-max-consecutive-ones
16171618
[492]:Problemset/0492-Construct%20the%20Rectangle/README.md#492-construct-the-rectangle
1619+
[493]:Problemset/0493-Reverse%20Pairs/README.md#493-reverse-pairs
16181620
[494]:Problemset/0494-Target%20Sum/README.md#494-target-sum
16191621
[495]:Problemset/0495-Teemo%20Attacking/README.md#495-teemo-attacking
16201622
[496]:Problemset/0496-Next%20Greater%20Element%20I/README.md#496-next-greater-element-i
@@ -2939,6 +2941,7 @@
29392941
[483l]:https://leetcode.com/problems/smallest-good-base/
29402942
[485l]:https://leetcode.com/problems/max-consecutive-ones/
29412943
[492l]:https://leetcode.com/problems/construct-the-rectangle/
2944+
[493l]:https://leetcode.com/problems/reverse-pairs/
29422945
[494l]:https://leetcode.com/problems/target-sum/
29432946
[495l]:https://leetcode.com/problems/teemo-attacking/
29442947
[496l]:https://leetcode.com/problems/next-greater-element-i/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
[483][483l] |[最小好进制][483] |![py]
292292
[485][485l] |[最大连续1的个数][485] |![rs]
293293
[492][492l] |[构造矩形][492] |![rs]
294+
[493][493l] |[翻转对][493] |![py]
294295
[494][494l] |[目标和][494] |![rb]&nbsp;&nbsp;![rs]
295296
[495][495l] |[提莫攻击][495] |![rs]
296297
[496][496l] |[下一个更大元素 I][496] |![rs]
@@ -1615,6 +1616,7 @@
16151616
[483]:Problemset/0483-Smallest%20Good%20Base/README_CN.md#483-最小好进制
16161617
[485]:Problemset/0485-Max%20Consecutive%20Ones/README_CN.md#485-最大连续1的个数
16171618
[492]:Problemset/0492-Construct%20the%20Rectangle/README_CN.md#492-构造矩形
1619+
[493]:Problemset/0493-Reverse%20Pairs/README_CN.md#493-翻转对
16181620
[494]:Problemset/0494-Target%20Sum/README_CN.md#494-目标和
16191621
[495]:Problemset/0495-Teemo%20Attacking/README_CN.md#495-提莫攻击
16201622
[496]:Problemset/0496-Next%20Greater%20Element%20I/README_CN.md#496-下一个更大元素-i
@@ -2939,6 +2941,7 @@
29392941
[483l]:https://leetcode.cn/problems/smallest-good-base/
29402942
[485l]:https://leetcode.cn/problems/max-consecutive-ones/
29412943
[492l]:https://leetcode.cn/problems/construct-the-rectangle/
2944+
[493l]:https://leetcode.cn/problems/reverse-pairs/
29422945
[494l]:https://leetcode.cn/problems/target-sum/
29432946
[495l]:https://leetcode.cn/problems/teemo-attacking/
29442947
[496l]:https://leetcode.cn/problems/next-greater-element-i/

0 commit comments

Comments
 (0)