leetcode315
- public class Solution {
- public List<Integer> countSmaller(int[] nums) {
- List<Integer> res = new ArrayList<>();
- if(nums == null || nums.length == 0) return res;
- TreeNode root = new TreeNode(nums[nums.length - 1]);
- res.add(0);
- for(int i = nums.length - 2; i >= 0; i--) {
- int count = insertNode(root, nums[i]);
- res.add(count);
- }
- Collections.reverse(res);
- return res;
- }
- public int insertNode(TreeNode root, int val) {
- int thisCount = 0;
- while(true) {
- if(val <= root.val) {
- root.count++;
- if(root.left == null) {
- root.left = new TreeNode(val); break;
- } else {
- root = root.left;
- }
- } else {
- thisCount += root.count;
- if(root.right == null) {
- root.right = new TreeNode(val); break;
- } else {
- root = root.right;
- }
- }
- }
- return thisCount;
- }
- }
- class TreeNode {
- TreeNode left;
- TreeNode right;
- int val;
- int count = 1;
- public TreeNode(int val) {
- this.val = val;
- }
- }
从后向前进行判断,线性的搜索时间复杂度比较高,因此建立一个二叉搜索树,每次插入节点的时候,更新父节点的“右侧小”的数字的数量。
leetcode315的更多相关文章
- LeetCode315—Count of Smaller Numbers After Self—Java版归并算法
这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释. 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高 ...
- [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 ...
- leetcode315 Count of Smaller Numbers After Self
思路: bit + 离散化. 实现: #include <bits/stdc++.h> using namespace std; class Solution { public: int ...
- leetcode315 计算右侧小于当前元素的个数
1. 采用归并排序计算逆序数组对的方法来计算右侧更小的元素 time O(nlogn): 计算逆序对可以采用两种思路: a. 在左有序数组元素出列时计算右侧比该元素小的数字的数目为 cnt=r-mid ...
- 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...
随机推荐
- LeetCode——5.Longest Palindromic Substring
一.题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 二.题目大意: 给定一个字符串,找出它最长的回文子串.例如,字符 ...
- git 对比两个分支差异
比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: 1.查看 dev 有,而 master 中没有的: git log dev ^master 同 ...
- [UE4]虚幻4的网络适合开发什么游戏
使用虚幻4开发网络游戏的两种方式 一.虚幻4只做客户端,服务器端独立开发,适用于任何网络游戏 二.使用虚幻4同时开发客户端和服务器(使用虚幻4内置的服务器),适用于一部分网络游戏. 如果使用虚幻4作为 ...
- [UE4]关于分支Sequence和条件分支的组合用法
当需要不管条件语句是否成立的后面都需要执行的语句,可以使用“Sequence”来分支,达到简化蓝图连线的目的.如下图所示:
- third party sales process 继续说
Trading company: A vendor或production plant: B END Customer: C third party sales与individual purchasin ...
- 有关于Integer的一些小问题
先看一小段源码: Integer a1=; Integer a2=; Integer b1=); Integer b2=); Integer c1=; Integer c2=; System.out. ...
- Python 画3D图像
绘制一副3D图像 draw3D(X,Y,Z, angle) import numpy as np from matplotlib import pyplot as plt from mpl_toolk ...
- 解决linux更新apt软件源时报出GPG错误
今天给树莓派换源,爆出N个这错误: W: GPG error: http://mirrors.neusoft.edu.cn/raspbian/raspbian wheezy InRelease: Th ...
- IntelliJ IDEA 版本控制器 - Git
1.下载Git 2.测试Git是否安装成功 3.设置 本机 Git 标识,同时解决未设置标识产生的错误 Successfully created project 'demo' on GitHub, b ...
- python爬虫之登录
#-*-coding:utf--*- import cookielib, urllib, urllib2 import json import threading,time class Order(o ...