题目一:
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
public class Solution {
public int InversePairs(int [] array) {
if(array == null||array.length < 2)
return 0;
int[] tmp=new int[array.length];
return help(array,tmp,0,array.length-1);
}
public int help(int[] array,int[] tmp,int left,int right)
{
if(left<right)
{
int center=left+(right-left)/2;
int count1=help(array,tmp,left,center);
int count2=help(array,tmp,center+1,right);
int count3=merge(array,tmp,left,center+1,right);
return count1+count2+count3;
}
else
return 0;
}
public int merge(int[] array,int[] tmp,int leftpos,int rightpos,int rightend)
{
int tmppos=rightend;
int leftend=rightpos-1;
int count=0;
int index=rightend;
while(leftend>=leftpos&&rightend>=rightpos)
{
if(array[leftend]>array[rightend])
{
count+=rightend-rightpos+1; //key.
tmp[tmppos--]=array[leftend--];
}
else
{
tmp[tmppos--]=array[rightend--];
}
}
while(leftend>=leftpos)
tmp[tmppos--]=array[leftend--];
while(rightend>=rightpos)
tmp[tmppos--]=array[rightend--];
while(index>=leftpos)
{
array[index]=tmp[index];
index--;
}
return count;
}
}
题目二:
小和的定义:
例如数组 s={1,3,5,2,4,6};在s[2]左边小于等于s[2]的数的和为1+3=4;统计所有s中元素的该和并求和,即为该数组的小和。
public int getSmallSum(int[] data)
{
if(data == null||data.length < 2)
return 0;
int[] tmparray=new int[data.length];
return mergeSort(data,tmparray,0,data.length-1);
}
public int mergeSort(int[] array,int[] tmparray,int left,int right)
{
if(left < right)
{
int mid=left+(right-left)/2;
int left_res=mergeSort(array,tmparray,left,mid);
int right_res=mergeSort(array,tmparray,mid+1,right);
int merge_res=merge(array,tmparray,left,mid+1,right);
return left_res+right_res+merge_res;
}
return 0;
}
public int merge(int[] array,int[] tmparray,int leftpos,int rightpos,int rightend)
{
int leftend=rightpos-1;
int index=leftpos;
int tmppos=leftpos;
int cursum=0;
while(leftpos<=leftend&&rightpos<=rightend)
{
if(array[leftpos] <= array[rightpos])
{
cursum+=(rightend-rightpos+1)*array[leftpos]; //key
tmparray[tmppos++]=array[leftpos++];
}
if(array[leftpos] > array[rightpos])
{
tmparray[tmppos++]=array[rightpos++];
}
}
while(leftpos<=leftend)
{
tmparray[tmppos++]=array[leftpos++];
}
while(rightpos<=rightend)
{
tmparray[tmppos++]=array[rightpos++];
}
for(int i=index;i<=rightend;i++)
{
array[i]=tmparray[i];
}
return cursum;
}