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 answer :

because   2147483647*2 is -2 (int)  ,  only 33bit can represent

int 4 byte 32 bit
2147483647--2147483647  signed

0-4294967295  unsigned

long 4 byte 32 bit (在 32 位机器上与int 相同)
double 8 byte 64 bit   1.79769e+308 ~ 2.22507e-308
float 4 byte 32 bit   3.40282e+038 ~ 1.17549e-038
long double 12 byte 96 bit  1.18973e+4932 ~ 3.3621e-4932

then i change the type to double:

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

wrong:

because the if the maxmise length is 50000, the process will lead to time exceed .

对于这类问题,一种很好的解法就是拆分数组来解决子问题,通过求解小问题来求解大问题。

有两类拆分方法:

  • 顺序重现(sequential recurrence relation):T(i, j) = T(i, j - 1) + C
  • C就是处理最后一个数字的子问题,找 T(i, j - 1)中的reverse pairs, T(i, j-1) 有序,只需要找大于2*nums[j]的数。     二分查找已经不满足条件了,只能使用Binary indexed tree 来实现。BIT的存储方式不是数组对应一一存入,而是有的对应存入,有的存若干个数字之和,其设计的初衷是在O(lgn)时间复杂度内完成求和运算。
  • 分割重现(Partition Recurrence relation):T(i, j) = T(i, m) + T(m+1, j) + C
  • C为合并两个部分的子问题。MergeSort的思想就是把数组对半拆分为子数组,柴刀最小的数组后开始排序,然后一层一层返回,得到有序的数组。时间复杂度O(nlogn)
class Solution {
public:
    int reversePairs(vector<int>& nums) {
        return mergeSort(nums, 0, nums.size() - 1);
    }
    int mergeSort(vector<int>& nums, int left, int right) {
        if (left >= right) return 0;
        int mid = left + (right - left) / 2;
        int res = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
        for (int i = left, j = mid + 1; i <= mid; ++i) {
            while (j <= right && nums[i] / 2.0 > nums[j]) ++j;
            res += j - (mid + 1);
        }
        sort(nums.begin() + left, nums.begin() + right + 1);
        return res;
    }
};

归并排序 MergeSort 和BIT可以解决,BST和 binary search不行

https://discuss.leetcode.com/topic/79227/general-principles-behind-problems-similar-to-reverse-pairsBST (binary search tree)

BIT (binary indexed tree)

时间: 2024-12-27 21:11:59

LeetCode -Reverse Pairs的相关文章

[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 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

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

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:

Leetcode Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode: Reverse Linked List

LeetCode: Reverse Linked List Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follo

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol