2.1 493. Reverse Pairs (Divide and Conquer)

本文介绍了一种算法,用于找出数组中满足特定条件的重要反向对数量。通过实例展示了如何实现该算法,并提供了C++代码示例。适用于对数组操作及算法优化感兴趣的读者。

题目:

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

翻译:
给定一个数组NUMS,我们叫(I,J)的重要反向对,如果我<j和NUMS [I]> 2 * NUMS [J]。你需要返回给定数组中的重要反向双数。

例1:
输入:[1,3,2,3,1]
输出:2

例2:
输入:[2,4,3,5,1]
输出:3

注意:
给定数组的长度不超过50,000。
输入阵列中的所有数字是在32位整数的范围。



解题
思路:
1.复制一个数组v2并每个元素都乘二,将之排序。
2.v1从0到length 循环,每次将当前元素乘二在v2中二叉搜索得到迭代器然后将之在v2中删除,再将当前元素在v2中二叉搜索得到比其小的元素个数,最后累积返回。


代码展示:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;


class Solution {
	//记录v中比aim小的元素个数并返回 
	int count(vector<long> &v,long aim) {
		int begin = 0, end = v.size() - 1, middle;
		if(end < 0) return 0;
		while(begin < end) {
			middle = (begin+end) /2;
			if(v[middle] < aim) begin = middle+1;
			else end = middle;
		}
		if(v[begin]>=aim) return begin;
		else return v.size();
	}
	
	//在v中查找aim 并返回迭代器 
	vector<long>::iterator Mysearch(vector<long>& v, long aim) {
		int begin = 0,end = v.size()-1;
		while(begin < end) {
			int	middle = (begin+end)/2;
			if(v[middle] == aim) return v.begin()+middle;
			else if(v[middle] < aim) begin = middle+1;
			else end = middle;
		}
		return v.begin() + begin;
	}
public:
	
    int reversePairs(vector<int>& nums) {
    	int sum = 0;
		int n = nums.size();
		vector<long> v(n,0);
		for(int i = 0; i< n;i++) v[i] = (long)nums[i] * 2;
		
		sort(v.begin(),v.end());
		
		for(int i= 0; i< n;i++) {
			vector<long>::iterator iter = Mysearch(v,(long)nums[i] *2);
			v.erase(iter);
			sum += count(v,nums[i]);	
		}
		return sum;
    }
};

int main() {
	int arr[] = {2,4,3,5,1};
	vector<int> v(arr,arr+5);
	Solution s;
	cout <<s.reversePairs(v);
} 

解题状态:
### Python `x.reverse` 方法的使用说明与示例 在 Python 中,`x.reverse` 是一种针对列表的操作方法,用于对列表中的元素进行原地反转(即将列表的第一个元素变为最后一个,第二个变为倒数第二个,依此类推)。此方法不会返回任何值(返回值为 `None`),但它会直接修改原始列表。 #### 基本语法 ```python x.reverse() ``` - **功能描述**:该方法将列表中的元素顺序完全颠倒。 - **适用范围**:仅适用于列表类型的数据结构。对于其他不可变数据类型(如字符串、元组等),不支持这一方法[^1]。 --- #### 示例一:基本用法——列表反转 以下代码展示了如何通过 `reverse()` 对整数列表进行反转: ```python my_list = [1, 2, 3, 4, 5] my_list.reverse() print(my_list) # 输出: [5, 4, 3, 2, 1] ``` --- #### 示例二:嵌套列表的反转 需要注意的是,`reverse()` 只会对顶层列表的元素顺序进行反转,而不会影响子列表内部的内容: ```python nested_list = [[1, 2], [3, 4], [5, 6]] nested_list.reverse() print(nested_list) # 输出: [[5, 6], [3, 4], [1, 2]] ``` 尽管外部列表被反转了,但 `[1, 2]`, `[3, 4]`, `[5, 6]` 这些子列表本身并未改变其内部顺序。 --- #### 示例三:与其他方法结合使用 可以将 `reverse()` 结合其他方法一起使用,比如先排序再反转以获得降序效果: ```python numbers = [7, 3, 9, 1, 5] numbers.sort() # 升序排序 numbers.reverse() # 排序后再反转得到降序 print(numbers) # 输出: [9, 7, 5, 3, 1] ``` 这种方法虽然有效,但在某些情况下可能不如直接使用 `sort(reverse=True)` 更加简洁明了[^1]。 --- #### 特殊情况处理 如果尝试对空列表调用 `reverse()` 方法,则不会引发错误,也不会有任何变化: ```python empty_list = [] empty_list.reverse() print(empty_list) # 输出: [] ``` 另外值得注意的一点是,由于 `reverse()` 修改的是原对象而非创建新副本,所以在需要保持原有数据不变的情况下应该谨慎使用。 --- ### 总结 `x.reverse` 提供了一种简单直观的方式来快速翻转列表中所有项的位置关系。然而要注意它的局限性在于只能作用于可变序列类型之上,并且执行过程属于就地更新性质而不是生成新的实例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值