493. Reverse Pairs

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].

You need to return the number of important reverse pairs in the given array.

Example1:

Input: [1,3,2,3,1]
Output: 2

Example2:

Input: [2,4,3,5,1]
Output: 3

Note:

  1. The length of the given array will not exceed 50,000.
  2. All the numbers in the input array are in the range of 32-bit integer.

拿到题目后,最直观的解法就是遍历尝试所有的(i, j)组合,看看有多少种组合满足 i < j and nums[i] > 2*nums[j]这一条件。

那么,写成代码就是一个嵌套的for循环:

for i:= 0; i < len(nums); i++ {
    for j := i; j < len(nums); j++ {
    }
}

可以想见,这样解法的时间复杂度为O(n²),空间复杂度为O(1)。

那么,有么有什么办法,可以把时间复杂度降低一些呢?

O(n*n)中,至少有个一个n是无法优化的,因为至少需要遍历每个元素一次,那么第二个n能否降低为<n的值呢?

想象一下,当我们从第0个元素往右遍历的时候,随着index的增加,我们能知道哪些信息?(想象一个卷轴打开的样子,未打开的部分是未知的,已打开的部分就能看的内容)

对于这类顺序展开的情况,我们可以将其抽象成 f(i,j) = f(i, j-1) + c的情况。其中,可以假定f(i, j-1) 已知,我们通过解决c来完成f(i, j)的求解。

那么,在这道题目中,f(i, j-1)是什么,它给我们提供了哪些信息量呢?知道f(i, j-1)后,我们需要做什么才能得到f(i, j)呢?

我们假设 f(i, j) 代表start为i,end为j的范围内,满足nums[i] > 2*nums[j] 这一条件的pair数。

那么,当知道了 f(i, j-1) 后,我们怎么才能得到 f(i, j) 呢?

f(i, j) 相较于 f(i, j-1) 相当于end的index向后移动了一位。我们需要知道从 i 到 j-1 范围内,一共有多少个点满足 nums[i] > 2*nums[j] ,我们想求的是个count计数。

以题目的第二个例子举例:

[2,4,3,5,1]

当访问到第四个元素时(下标为3):

2  4 3 5  

我们想要知道在第四个元素前面有没有谁是它的2倍以上,并且如果有的话,共有多少个这样的数?

最简单的方法,就是数一下,从[0,j-1]遍历看看有多少个num大于nums[3]*2,写成伪代码为:

result := 0

for j := 0; j < len(nums); j++ {
   count := 0
   for i := 0; i < j; i++ {
        if nums[i] > 2*nums[j] {
             count += 1
        }
   }
   result += count
}

但如果这样找的话,每次都需要从头遍历到j位,因此时间复杂度并没有下降,还是O(n²)。那么,有没有什么办法能让我们知道第j位之前,有多少个元素大于2*nums[j]吗?

假如我们把前面 [0, j-1] 个元素排一下序,然后再找找 j-1 位之前,是否出现过大于

Reference:

https://leetcode.com/problems/reverse-pairs/discuss/97268/General-principles-behind-problems-similar-to-%22Reverse-Pairs%22

原文地址:https://www.cnblogs.com/elaron/p/12215687.html

时间: 2024-08-29 00:04:56

493. Reverse Pairs的相关文章

493. Reverse Pairs(BST, BIT, MergeSort)

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2 Example2: Input: [2,4,3,5,1] Output:

LeetCode 493. Reverse Pairs

原题链接在这里:https://leetcode.com/problems/reverse-pairs/ 题目: Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1

【leetcode】493. Reverse Pairs

好吧这其实是一次小比赛,在回上海前的周末.好久没打比赛,简单题也只是AK了而已,还TM错了一次.最后45名57分钟加一次罚时,第一名18分钟,好吧.也行了对于业余组选手. 错在没考虑到乘2会超max_int. 程序没了,也没心情找了.孤单的情人节. 貌似有两个人被发现作弊了我变成了43名,还找到了程序,也算一点小小的安慰吧. class Solution { public: int reversePairs(vector<int>& aa) { vector<long long&

493 Reverse Pairs 翻转对

给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: [1,3,2,3,1]输出: 2示例 2:输入: [2,4,3,5,1]输出: 3注意:    给定数组的长度不会超过50000.    输入数组中的所有数字都在32位整数的表示范围内.详见:https://leetcode.com/problems/reverse-pairs/description/ C

[LeetCode] Reverse Pairs 翻转对

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2 Example2: Input: [2,4,3,5,1] Output:

LeetCode -Reverse Pairs

my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=nums.size(); int count=0; for (int i=0;i<length;i++) { for(int j=i+1;j<length;j++) { if(nums[i]>2*nums[j]) count++; } } return count; } }; wrong answ

Reverse Pairs

For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return total of reverse pairs in A. Example Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3 Solution 1: the same as the leetcode ques

[Swift]LeetCode493. 翻转对 | Reverse Pairs

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. You need to return the number of important reverse pairs in the given array. Example1: Input: [1,3,2,3,1] Output: 2  Example2: Input: [2,4,3,5,1] Output:

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea