1. public class Solution {
  2. public List<Integer> countSmaller(int[] nums) {
  3. List<Integer> res = new ArrayList<>();
  4. if(nums == null || nums.length == 0) return res;
  5. TreeNode root = new TreeNode(nums[nums.length - 1]);
  6. res.add(0);
  7. for(int i = nums.length - 2; i >= 0; i--) {
  8. int count = insertNode(root, nums[i]);
  9. res.add(count);
  10. }
  11. Collections.reverse(res);
  12. return res;
  13. }
  14.  
  15. public int insertNode(TreeNode root, int val) {
  16. int thisCount = 0;
  17. while(true) {
  18. if(val <= root.val) {
  19. root.count++;
  20. if(root.left == null) {
  21. root.left = new TreeNode(val); break;
  22. } else {
  23. root = root.left;
  24. }
  25. } else {
  26. thisCount += root.count;
  27. if(root.right == null) {
  28. root.right = new TreeNode(val); break;
  29. } else {
  30. root = root.right;
  31. }
  32. }
  33. }
  34. return thisCount;
  35. }
  36. }
  37.  
  38. class TreeNode {
  39. TreeNode left;
  40. TreeNode right;
  41. int val;
  42. int count = 1;
  43. public TreeNode(int val) {
  44. this.val = val;
  45. }
  46. }

从后向前进行判断,线性的搜索时间复杂度比较高,因此建立一个二叉搜索树,每次插入节点的时候,更新父节点的“右侧小”的数字的数量。

参考:https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76587/Easiest-Java-solution

leetcode315的更多相关文章

  1. LeetCode315—Count of Smaller Numbers After Self—Java版归并算法

    这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释. 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高 ...

  2. [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 ...

  3. leetcode315 Count of Smaller Numbers After Self

    思路: bit + 离散化. 实现: #include <bits/stdc++.h> using namespace std; class Solution { public: int ...

  4. leetcode315 计算右侧小于当前元素的个数

    1. 采用归并排序计算逆序数组对的方法来计算右侧更小的元素 time O(nlogn): 计算逆序对可以采用两种思路: a. 在左有序数组元素出列时计算右侧比该元素小的数字的数目为 cnt=r-mid ...

  5. 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

    Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...

随机推荐

  1. LeetCode——5.Longest Palindromic Substring

    一.题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 二.题目大意: 给定一个字符串,找出它最长的回文子串.例如,字符 ...

  2. git 对比两个分支差异

    比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: 1.查看 dev 有,而 master 中没有的: git log dev ^master 同 ...

  3. [UE4]虚幻4的网络适合开发什么游戏

    使用虚幻4开发网络游戏的两种方式 一.虚幻4只做客户端,服务器端独立开发,适用于任何网络游戏 二.使用虚幻4同时开发客户端和服务器(使用虚幻4内置的服务器),适用于一部分网络游戏. 如果使用虚幻4作为 ...

  4. [UE4]关于分支Sequence和条件分支的组合用法

    当需要不管条件语句是否成立的后面都需要执行的语句,可以使用“Sequence”来分支,达到简化蓝图连线的目的.如下图所示:

  5. third party sales process 继续说

    Trading company: A vendor或production plant: B END Customer: C third party sales与individual purchasin ...

  6. 有关于Integer的一些小问题

    先看一小段源码: Integer a1=; Integer a2=; Integer b1=); Integer b2=); Integer c1=; Integer c2=; System.out. ...

  7. Python 画3D图像

    绘制一副3D图像 draw3D(X,Y,Z, angle) import numpy as np from matplotlib import pyplot as plt from mpl_toolk ...

  8. 解决linux更新apt软件源时报出GPG错误

    今天给树莓派换源,爆出N个这错误: W: GPG error: http://mirrors.neusoft.edu.cn/raspbian/raspbian wheezy InRelease: Th ...

  9. IntelliJ IDEA 版本控制器 - Git

    1.下载Git 2.测试Git是否安装成功 3.设置 本机 Git 标识,同时解决未设置标识产生的错误 Successfully created project 'demo' on GitHub, b ...

  10. python爬虫之登录

    #-*-coding:utf--*- import cookielib, urllib, urllib2 import json import threading,time class Order(o ...