Count of Smaller Numbers After Self
来源:互联网 发布:java开源b2b商城系统 编辑:程序博客网 时间:2024/06/07 06:10
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]
.
Example:
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.Return the array
[2, 1, 1, 0]
.根据题意可以得知,对于输入数组nums,返回数组arrs,其中arrs[i]等于数组nums中位于nums[i]右边且小于nums[i]的元素数目。只需要保存与某个元素的比较结果,很容易想到二分查找树,所以,从后向前遍历数组nums,将元素nums[i]不断插入到二叉树中,同时统计当前小于nums[i]的元素数目。需要注意数组处理nums中的重复元素,由于统计小于nums[i]的元素,因此对于相等的元素也插入到二叉树节点右边,便于计算。代码如下
class BinarySearchTreeNode{BinarySearchTreeNode leftChildren;BinarySearchTreeNode rightChildren;int value;int num;public BinarySearchTreeNode(int v){this.value = v;num = 1;}}public int insert(int v, BinarySearchTreeNode node){int re = 0;while(true){if(v < node.value){node.num ++;if(node.leftChildren == null){node.leftChildren = new BinarySearchTreeNode(v);break;}node = node.leftChildren;}else{node.num ++;// v >= node.value,加上左子树以及node本身re += getNum(node.leftChildren);if(node.value != v)re++;if(node.rightChildren == null){node.rightChildren = new BinarySearchTreeNode(v);break;}node = node.rightChildren;}}return re;}private int getNum(BinarySearchTreeNode node){if(node == null)return 0;elsereturn node.num;}public List<Integer> countSmaller(int[] nums) { LinkedList<Integer> reList = new LinkedList<Integer>(); if(nums == null || nums.length == 0) return reList; reList.addFirst(0); BinarySearchTreeNode root = new BinarySearchTreeNode(nums[nums.length-1]); for(int i=nums.length-2; i >= 0; i--){ int el = nums[i]; int re = insert(el,root); reList.addFirst(re); } return reList;}
0 0
- leetcode Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- [LeetCode315]Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self | LeetCode
- leetcode Count of Smaller Numbers After Self
- 315Count of Smaller Numbers After Self
- LeetCode Count of Smaller Numbers After Self
- [Leetcode]Count of Smaller Numbers After Self
- leetcode - Count of Smaller Numbers After Self
- leetcode:Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- 【Leetcode】Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- PKU 3013 Big Christmas Tree 最短路 spfa
- ubuntu 16.04 编译opencv3.1,opencv多版本切换
- 一些统计特征与统计图的绘制-matlab
- 【HUSTOJ】1019: 求1+1/2+1/3+...+1/n
- python 图片格式转换产生的锯齿问题
- Count of Smaller Numbers After Self
- IOS杂记
- 面向对象设计原则——单一职责原则
- 数据结构(c)——赫夫曼树与赫夫曼编码
- Mac的使用小技巧
- Leetcode 257. Binary Tree Paths
- redis学习笔记(2)---链表adlist
- 基本百度地图制作
- java笔记→递归算法、枚举法、辗转相除法