LC493. 翻转对-归并排序

题目描述
在这里插入图片描述
思路: 利用归并排序的思想当我们希望在数组中求出逆序对的数目时,我们可以使用归并排序的方法。这道题中i < jnums[i] > 2 * nums[j]的要求与逆序对类似,因此我们也可以使用归并排序的方法求出翻转对的数目。

在归并排序中,当我们归并两个子数组 nums[start .. mid]nums[mid + 1 .. end] 时,我们可以计算出对于前者中的每一个元素nums[i],后者中满足 nums[i] > 2 * nums[j]j的数目。由于两个子数组已经排好序,因此对于固定的 i,满足条件的 j 的区间一定是从后者的左端点开始,并且随着 i 的增加,j 区间的右端点不会减小。因此我们可以在 nums[mid + 1 .. end] 中维护一个指针 pos,表示对于当前的inums[mid + 1 .. pos] 的两倍都小于 nums[i]。随着 i 的增加,我们尝试向右移动 pos使得更多的数满足条件。

    int reversePairs(vector<int>& nums) {
        return mergersort(nums, 0, nums.size()-1);
    }
    int mergersort(vector<int>& nums, int s, int e)
    {
        if(s < e)
        {
            int mid = s + ((e - s) >> 1);
            int sum = mergersort(nums, s, mid) + mergersort(nums, mid+1, e);
            int j = mid + 1;
            for (int i = s; i <= mid; i++) {
                while (j <= e && nums[i] > nums[j] * 2LL)
                    j++;
                sum += j - (mid + 1);
            }

            merger(nums,s,mid,e);
            return sum;
        }
        return 0;
    }
    int merger(vector<int>& nums, int s, int m, int e)
    {
        int help[e-s+1];
        int r = m + 1;
        int l = s;
        int res = 0, i = 0;
        while(l <= m && r <= e)
        {
            help[i++] = nums[l] < nums[r] ? nums[l++] : nums[r++];
        }
        while(l <= m) help[i++] = nums[l++];
        while(r <= e) help[i++] = nums[r++];
        for(i = s; i <= e; i++)
        {
            nums[i] = help[i-s];
        }
        return res;
    }

时间复杂度:O(N log N)
空间复杂度:O(N)

Sure! Here's your code with comments added: ```matlab F = zeros(length(z), 1); % Initialize the F vector with zeros for i = 1:length(z) % Define the Phi function using anonymous function Phi = @(theta, R, r) (z(i) + lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc - lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) + lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc - lm).^2 - 2*R.*r.sin(theta))); % Calculate the value of F(i) using the integral3 function F(i) = BrNI / (4 * lc * (Rc - rc)) * integral3(Phi, 0, 2*pi, rc, Rc, rm, Rm); end ``` This code calculates the values of the vector `F` using a loop. The `Phi` function is defined as an anonymous function that takes `theta`, `R`, and `r` as input parameters. It performs a series of calculations and returns a value. The integral of `Phi` is then calculated using the `integral3` function. The result is stored in the corresponding element of the `F` vector. Please note that I have made some assumptions about the variables and functions used in your code since I don't have the complete context. Feel free to modify or clarify anything as needed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值