LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/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].
题解:
从右向左扫描数组nums, try to find the position of nums[i] in BST.
For each BST node, it contains its left subtree size count, its duplicate count.
When inserting a new node, returns the sum of smaller count.
Time Complexity: O(n^2). BST不一定balance. Space: O(n).
AC Java:
 class Solution {
     public List<Integer> countSmaller(int[] nums) {
         LinkedList<Integer> res = new LinkedList<>();
         if(nums == null || nums.length == 0){
             return res;
         }
         int n = nums.length;
         res.offerFirst(0);
         TreeNode root = new TreeNode(nums[n - 1]);
         root.count = 1;
         for(int i = n - 2; i >= 0; i--){
             int smallerCount = insert(root, nums[i]);
             res.offerFirst(smallerCount);
         }
         return res;
     }
     private int insert(TreeNode root, int num){
         int smallerCountSum = 0;
         while(root.val != num){
             if(root.val > num){
                 root.leftCount++;
                 if(root.left == null){
                     root.left = new TreeNode(num);
                 }
                 root = root.left;
             }else{
                 smallerCountSum += root.leftCount + root.count;
                 if(root.right == null){
                     root.right = new TreeNode(num);
                 }
                 root = root.right;
             }
         }
         root.count++;
         return smallerCountSum + root.leftCount;
     }
 }
 class TreeNode{
     int val;
     int count;
     int leftCount;
     TreeNode left;
     TreeNode right;
     public TreeNode(int val){
         this.val = val;
         this.count = 0;
         this.leftCount = 0;
     }
 }
LeetCode Count of Smaller Numbers After Self的更多相关文章
- [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 ... 
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
		说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ... 
- leetcode 315. Count of Smaller Numbers After Self 两种思路
		说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ... 
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
		315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ... 
- [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self
		You are given an integer array nums and you have to return a new countsarray. The counts array has t ... 
- LeetCode "Count of Smaller Number After Self"
		Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ... 
- LeetCode 315. Count of Smaller Numbers After Self
		原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ... 
- Count of Smaller Numbers After Self -- LeetCode
		You are given an integer array nums and you have to return a new counts array. The counts array has ... 
- [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数
		You are given an integer array nums and you have to return a new counts array. The countsarray has t ... 
随机推荐
- Hadoop2.2.0 hive0.12 hbase0.94 配置问题记录
			环境:centos6.2 Hadoop2.2.0 hive0.12 hbase0.94 1>hadoop配好之后,跑任务老失败,yarn失败,报out of memory错误,然后怎么调整内存大 ... 
- [深入浅出Windows 10]布局原理
			5.2 布局原理 很多时候在编写程序界面的时候都会忽略了应用布局的重要性,仅仅只是把布局看作是对UI元素的排列,只要能实现布局的效果就可以了,但是在实际的产品开发中这是远远不够的,你可能面临要实现的布 ... 
- Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
			脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ... 
- virtual关键字的本质是什么?
			MSDN上对virtual方法的解释:试着翻译如下 当一个方法声明包含virtual修饰符,这个方法就是虚方法.如果没有virtual修饰符,那么就不是虚方法. 非虚方法的实现是不变的:不管该方法是被 ... 
- BZOJ1171: 大sz的游戏&BZOJ2892: 强袭作战
			Description 大sz最近在玩一个由星球大战改编的游戏.话说绝地武士当前共控制了N个星球.但是,西斯正在暗处悄悄地准备他们的复仇计划.绝地评议会也感觉到了这件事.于是,准备加派绝地武士到各星球 ... 
- BZOJ1025: [SCOI2009]游戏
			Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ... 
- js小效果-简易计算器
			<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ... 
- C#多线程操作界面控件的解决方案(转)
			C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ... 
- Eclipse上的项目分享到GitHub
			1. 右击项目:team --> Share Project 2. 在弹出的选择框中选择 Git ,点击Next 3. Configure Git Repository 按照下图选择,点击Fin ... 
- map的用法
			Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ... 
