当前位置: 首页 > news >正文

LeetCode -- Count of Smaller Numbers After Self

题目描述:


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].


给定一个数组a[n],返回数组b[n],每个元素对应当前元素a[i]右边小于a[i]的个数。


思路:
本题比较直接的实现是创建二叉搜索树。


从右向左遍历数组a[n],i∈[n-1,0],创建二叉树。
二叉树的每个节点node存放a[i]以及小于当前节点的元素个数。
如果a[i]小于node.Value,node.Count++,向左走;走不动时把a[i]创建为叶子;
如果a[i]大于等于node.Value,当前count++,向右走;走不动时把a[i]创建为叶子
返回当前count作为结果。


实现代码:


public class Solution {
    public IList<int> CountSmaller(int[] nums) 
    {
        if (nums == null || nums.Length == 0){
    		return new List<int>();
    	}
    	
    	var result = new List<int>(){0 /*there is no value on the right of last number*/};
    	var len = nums.Length;
    	var root = new BTNode(nums[len - 1]);
    	for (var i = len - 2; i >= 0; i--){
    		// add the value of arr on the tree one by one
    		// also update the count of smaller on the 'root' node
    		var smallerCount = BuildAndCount(root, nums[i]);
    		result.Add(smallerCount);
    	}
    	
    	result.Reverse();
    	
    	return result;
     }
     
     public int BuildAndCount(BTNode current, int value){
    
     	int countOfSmaller = 0;
     	while(true){
    		if(value > current.Value){
    			
    			// value bigger than current node , add current node's 'count of smaller' on this value
    			countOfSmaller += current.Count + 1;
    			// keep going right ,until reach leaf
    			if(current.Right == null){
    				current.Right = new BTNode(value);
    				break;
    			}else{
    				current = current.Right;
    			}
    		}
    		else{
    			// value is smaller than current node value , increase the count of smaller on current node
    			current.Count ++;
    			// put value on left tree leaf
    			if(current.Left == null){
    				current.Left = new BTNode(value);
    				break;
    			}
    			else{
    				current = current.Left;
    			}
    		}
    	}
    	
    	return countOfSmaller;
     }
     
     public class BTNode{
     	public BTNode Left;
    	public BTNode Right;
    	public int Value;
    	public int Count ;
    	public BTNode (int v){
    		Value = v;
    		Count = 0;
    	}
     }
}






也可以考虑BIT的实现:
https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/

相关文章:

  • LeetCode -- Valid Perfect Square
  • LeetCode -- Russian Doll Envelopes
  • 查看sql server数据库的空间大小...
  • LeetCode -- Longest Palindrome
  • 有朋远方来-致力于java培训的张孝祥
  • LeetCode -- Range Sum Query 2D - Immutable
  • 从Oracle到DB2,问题集(一)
  • LeetCode -- Dungeon Game
  • 从Oracle到DB2,问题集(二)
  • LeetCode -- Contains Duplicate II
  • Sql union的反义词Minus
  • LeetCode -- Path Sum III
  • LeetCode -- Minimum Number of Arrows to Burst Balloons
  • 反醒反醒
  • LeetCode -- Arranging Coins
  • 分享的文章《人生如棋》
  • C++11: atomic 头文件
  • css的样式优先级
  • CSS相对定位
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Promise面试题,控制异步流程
  • SpringCloud集成分布式事务LCN (一)
  • Theano - 导数
  • ucore操作系统实验笔记 - 重新理解中断
  • 大整数乘法-表格法
  • 解决iview多表头动态更改列元素发生的错误
  • 聊聊directory traversal attack
  • 前端工程化(Gulp、Webpack)-webpack
  • 如何编写一个可升级的智能合约
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 写给高年级小学生看的《Bash 指南》
  • Linux权限管理(week1_day5)--技术流ken
  • 正则表达式-基础知识Review
  • !$boo在php中什么意思,php前戏
  • #QT(串口助手-界面)
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • $$$$GB2312-80区位编码表$$$$
  • (1)(1.13) SiK无线电高级配置(五)
  • (10)ATF MMU转换表
  • (31)对象的克隆
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (五)c52学习之旅-静态数码管
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)Oracle存储过程编写经验和优化措施
  • (转)scrum常见工具列表
  • (转)树状数组
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • ***通过什么方式***网吧
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Framework 4.6.2改进了WPF和安全性