CountofSmallerNumbers【LeetCode】

import java.util.*;
public class CountofSmallerNumbersAfterSelf{
		int []count;
	public int[] countSmaller(int[] nums) {
		count = new int[nums.length];
		int []indexes = new int[nums.length];
		for(int i = 0; i < nums.length; i++){
			indexes[i] = i;
		}
		mergesort(nums, indexes, 0, nums.length - 1);
		for(int i:count)
			System.out.println(i);
		return count;
	}
	
	private void mergesort(int[] nums, int[] indexes, int start, int end){
		if(end <= start){
			return;
		}
		int mid = (start + end) / 2;
		mergesort(nums, indexes, start, mid);
		mergesort(nums, indexes, mid + 1, end);
		merge(nums, indexes, start, end);
	}
	
	private void merge(int[] nums, int[] indexes, int start, int end){
		int mid = (start + end) / 2;
		int left_index = start;
		int right_index = mid+1;
		int rightcount = 0;    	
		int[] new_indexes = new int[nums.length];
		int sort_index = start;
		while(left_index <= mid && right_index <= end){
			if(nums[indexes[right_index]] < nums[indexes[left_index]]){
				new_indexes[sort_index++] = indexes[right_index++];
				rightcount++;
			}else{
				count[indexes[left_index]] += rightcount;
				new_indexes[sort_index++] = indexes[left_index++];
			}
		}
		while(left_index <= mid){
			count[indexes[left_index]] += rightcount;
			new_indexes[sort_index++] = indexes[left_index++];
		}
		while(right_index <= end){
			new_indexes[sort_index++] = indexes[right_index++];
		}
		for(int i = start; i <= end; i++){
			indexes[i] = new_indexes[i];
		}
	}
	public static void main(String []args){
		int []nums={5,2,6,1};
		CountofSmallerNumbersAfterSelf ct=new CountofSmallerNumbersAfterSelf();
		ct.countSmaller(nums);
		
	}
}
/*
You are given an integer array nums and you have to return a new counts array. 
The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
Given nums = [5, 2, 6, 1]
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.
[2,1,1,0] 
*/

import java.util.ArrayList;
import java.util.Collections;
public class CountofSmallerNumbersII{
	public class TreeNode{
		int val;
		int num;
		TreeNode lchild;
		TreeNode rchild;
		public TreeNode(int val){
			this.val=val;
			this.num=0;
		}	
	} 
	public ArrayList<Integer> countSmaller(int []nums){
		ArrayList<Integer> list=new ArrayList<Integer>();
		list.add(0);
		TreeNode root=new TreeNode(nums[nums.length-1]);
		root.num=1;
		for(int i=nums.length-2;i>=0;i--){
			list.add(getIndex(root,nums[i],0));
		}
		Collections.reverse(list);
		for(Integer i:list){
			System.out.println(i);
		}
		return list;
	}
	
	public int getIndex(TreeNode root,int val,int num){
		if(root.val>=val){
			root.num+=1;
			if(root.lchild==null){
				TreeNode node=new TreeNode(val);
				node.num=1;
				root.lchild=node;
				return num;
			}else{
				return getIndex(root.lchild,val,num);
			}	
		}else{
			num+=root.num;
			if(root.rchild==null){
				TreeNode node=new TreeNode(val);
				node.num=1;
				root.rchild=node;
				return num;
			}else{
				return getIndex(root.rchild,val,num);
			}
		}
	}
	public static void main(String []args){
		int []nums={5,2,6,1};
		CountofSmallerNumbersII cs=new CountofSmallerNumbersII();
		cs.countSmaller(nums);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值