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) 我们可以直接套用主席树的模板. 主席树 ...
随机推荐
- Centos7 在 Xshell里 vim的配置
Centos里的VI只默认安装了vim-minimal-7.x.所以无论是输入vi或者vim查看文件,syntax功能都无法正常启用.因此需要用yum安装另外两个组件:vim-common-7.x和v ...
- whith ~ as 用法
个人理解 with self.client.get("/", catch_response=True) as response: 其实就是 response = self.clie ...
- springboot(运行原理参考借鉴)
什么是springboot答:Spring Boot makes it easy to create stand-alone, production-grade Spring based Applic ...
- netty 3.x 实现http server和遇到的坑
先转载一篇 [初学与研发之NETTY]netty3之文件上传 http://blog.csdn.net/mcpang/article/details/41139859 客户端: [java] view ...
- typescript泛型接口
//函数类型接口 /* interface ConfigFn{ (value1:string,value2:string):string; } var setData:ConfigFn=functio ...
- 第11章 拾遗4:IPv6(2)_给计算机配置IPv6地址
4. 给计算机配置IPv6地址 4.1 无状态自动配置IPv6地址 (1)网络拓扑 ①无状态地址自动配置是指不需要DHCP服务器进行管理,由客户端向路由器发送前缀请求(RS)询问其所在网段.路由器收到 ...
- AWS之SSH登录:使用 PuTTY 从 Windows 连接到 Linux 实例
使用 PuTTY 从 Windows 连接到 Linux 实例 启动您的实例之后,您可以连接到该实例,然后像使用您面前的计算机一样来使用它. 注意 启动实例后,需要几分钟准备好实例,以便您能连接到实例 ...
- SqlServer存储过程输出参数
if exists(select 1 from sysobjects where name='P_PreOrderInfo') drop Procedure P_PreOrderInfo go Cre ...
- sql-datediff
SQL中DateDiff的用法 DATEDIFF返回跨两个指定日期的日期和时间边界数. 语法DATEDIFF ( datepart , startdate , enddate ) 参数datepart ...
- (转)Java调用Weservice
原文地址:http://www.cnblogs.com/jiangxu1989/p/6491483.html https://www.cnblogs.com/neughj/p/5145630.html ...