[LeetCode]493. Reverse Pairs 深入浅出算法讲解和代码示例
来源:互联网 发布:算法 英文版 pdf 编辑:程序博客网 时间:2024/05/16 06:14
1、汇总概要
本提解题思路涵盖了插入排序、二分查找、mergeSort思路。
2、题目
Given an array nums, we call (i, j) an important reverse pair if i < j andnums[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:
The length of the given array will not exceed 50,000.
All the numbers in the input array are in the range of 32-bit integer.
3、审题
给一个序列,找出它的反转对个数。
判断反转对规则:if i < j and nums[i] > 2*nums[j].
4、解题思路
最基本的思路是双重循环遍历,时间复杂度是O(n*n),舍弃。
考虑O(nlogn)的复杂度。
1. 将列表数组分为左右两个子数组,list = [[x,x,x][x,x,x]],满足左边的数组有序,右边的数组无序;
2. 左边数组元素的index都小于右边数组的index,在遍历右边数组元素时,找到左边数组中比它大2倍的数,即是反转对。
讲到这里大家理解了,其实就是插入排序、二分查找反转对。
初始是左边数组为空,右边数组为原list,遍历右边数组中元素,依次往左边数组中插入排序,二分查找插入的位置。
该算法基本没有额外空间消耗,用的mergeSort性质。
5、代码示例 - Java
public class ReversePairs {public int midSearch(int[] list,int low,int high, int value) throws InterruptedException{int mid = (low+high)/2;if(value>list[mid]){mid++;if(mid<high){midSearch(list,mid,high,value);}else{return mid;}}else if(value<list[mid]){mid--;if(mid>low){midSearch(list,low,mid,value);}else{return mid;}}return mid;}public int insertAndPairs(int[] list,int start, int end,int value, int sum){int j=0;for(j=end+1;j>start;j--){list[j]=list[j-1];//insert the least one into list headif(list[j]>2*value){//find pairssum ++;}}list[start] = value;return sum;}public int getPairs(int[] list) throws InterruptedException{int low, mid, high;int i,j;int len = list.length;int sum = 0;for(i=1;i<len;i++){//merge sortint cur = list[i];low = 0;high = i-1;mid = (low+high)/2;if(cur <= list[0]){//[0,i-1][i,len-1],previous list is in ordersum = insertAndPairs(list,0,i-1,cur,sum);list[0] = cur;}else if(cur>=list[i-1]){//no need actioncontinue; }else{ //mid searchint index = midSearch(list,low,high,cur);//compare all values after index, [index,high]sum = insertAndPairs(list,index,i-1,cur,sum);}}return sum;}public static void main(String[] args) throws InterruptedException{int [] list = {2,4,3,5,1};ReversePairs rp = new ReversePairs();int sum = rp.getPairs(list);System.out.println("\n\nRES: "+sum);}}
---------------------------------------------------------------------------------------------------
本文链接:http://blog.csdn.net/karen0310/article/details/xx
请尊重作者的劳动成果,转载请注明出处!
---------------------------------------------------------------------------------------------------
- [LeetCode]493. Reverse Pairs 深入浅出算法讲解和代码示例
- [LeetCode]420. Strong Password Checker深入浅出算法讲解和代码示例
- [LeetCode] 137. Single Number II 深入浅出算法讲解和代码示例
- [LeetCode]632. Smallest Range 深入浅出讲解和代码示例
- [LeetCode]127. Word Ladder 深入浅出讲解和代码示例
- [LeetCode]147. Insertion Sort List 深入浅出讲解和代码示例
- [LeetCode]146. LRU Cache 深入浅出讲解和代码示例
- [LeetCode]44. Wildcard Matching 深入浅出讲解和代码示例
- [LeetCode]68. Text Justification 深入浅出讲解和代码示例
- [LeetCode]493. Reverse Pairs
- leetCode:493. Reverse Pairs
- Leetcode:493. Reverse Pairs
- leetcode 493. Reverse Pairs
- [LeetCode]149. Max Points on a Line 深入浅出讲解和代码示例
- [LeetCode] 564. Find the Closest Palindrome 深入浅出讲解和代码示例
- [Leetcode] 493. Reverse Pairs 解题报告
- leetcode练习 Reverse Pairs
- leetcode改进 Reverse Pairs
- hihoCoder #1148 : 2月29日 题解
- 跟我一起学数据结构(第一天)
- Kafka入门介绍(一)
- Systemd 入门教程:命令篇
- Linux中link,unlink,close,fclose详解
- [LeetCode]493. Reverse Pairs 深入浅出算法讲解和代码示例
- ListIterator等的使用。
- jQuery源码阅读(十一)---each、map、grep、merge、makeArray、inArray解读
- 阿里云上配置hostname使用内网ip,不能使用外网ip
- POJ1426 Find The Multiple
- 强制类型转换
- js-apply()
- php运行原理
- 针对thinkphp中"no input file specified"的解决方法