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 ...
随机推荐
- unity 解析xml
using UnityEngine; using System.Collections; using System.IO; using System.Xml; public class xml : M ...
- python 代码片段21
#coding=utf-8 @doco def foo(): pass ''' deco把foo函数拿过来,加上一些额外的功能再重新赋值给foo,如下 ''' foo=deco(foo) def lo ...
- [转]复制虚拟机后linux中的eth0变成eth1问题
为什么原来的eth0会变成eth1? 很多Linux distribution使用udev动态管理设备文件,并根据设备的信息对其进行持久化命名.udev会在系统引导的过程中识别网卡,将mac地址和网卡 ...
- SVN版本控制工具使用学习
SVN版本控制工具使用学习 Subversion是优秀的版本控制工具. 1.下载和搭建SVN服务器 http://subversion.apache.org/packages.html 类型有5种,推 ...
- 【bzoj1367】[Baltic2004]sequence
2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...
- nodeJS中exports和mopdule.exports的区别
每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {} module.exports = {}; Node.js为了方 ...
- SpringMVC+Thymeleaf如何处理URL中的动态查询参数
1.使用一个Map<String, String>接收数据 When you request a Map annotated with @RequestParam Spring creat ...
- 【Go语言】学习资料
这段时间一直在看Go语言,6月3日Apple发布了swift发现里面竟然也有许多Go语言的影子,截至现在每天都在感觉到Go语言的强大.确实值得一学 今天在这里给园友们推荐一些Go语言的学习资料 网站 ...
- Windows Phone Foreground Toast
Basically ToastPrompt is an UI component that derives from the Coding4Fun toolkit's abstract PopUp&l ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...