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].

思路:O(nlogn)复杂度算法。

将数组排序然后构建二叉搜索树。一开始二叉搜索树上的节点都标记为未处理过。然后我们从所给的nums数组的最后一个数倒着向前遍历,依次将每一个数在二叉搜索树中对应的节点标记为处理过,然后返回二叉搜索树中已经被标记为处理过,且小于该值的个数。

具体实现中,我们在每一个节点中设置一个count变量,计数以该节点为根节点的子树中已经被标记为处理过的节点个数,初始为0。之后算法运行过程中不断更新该值并通过该值更快地求解。本质上来说这是一个线段树的应用。

当我们要求二叉搜索树中有多少被处理过的节点值小于所给的值时,有三种情况:

  • 所给的值是当前子树的根节点。则小于它的数只可能在左子树中,因此返回根节点左孩子的count值。
  • 所给的值在当前子树的左子树中。则小于它的数只可能在左子树中,因此递归求解,返回左子树中被处理过且小于所给值的节点个数。
  • 所给的值在当前子树的右子树中。则小于它的数可能在左子树中或者是该根节点,或者在右子树中。因此递归求解,返回左子树和根节点中被处理过且小于所给值的节点个数加上右子树中被处理过且小于所给值的节点个数。

二叉搜索树的构建O(n),二叉搜索树的单次查找更新操作O(logn)。 总复杂度为O(n) + O(nlogn) = O(nlogn)

 class treeNode {
public:
int val, count;
treeNode *left, *right;
treeNode(int v) : val(v), count(), left(NULL), right(NULL) {}
};
class Solution {
public:
//convert a sortedArray to a binary search tree and return a pointer to its root node
treeNode* buildTree(vector<int>& sortedArray, int left, int right) {
if (right < left) return NULL;
int mid = left + (right - left) / ;
treeNode* cur = new treeNode(sortedArray[mid]);
cur->left = buildTree(sortedArray, left, mid - );
cur->right = buildTree(sortedArray, mid + , right);
return cur;
}
//count numbers in this binary search tree that were processed and are less than the target
int update(treeNode* node, int target) {
if (node == NULL) return -;
if (node->val == target) {
node->count++;
return node->left ? node->left->count : ;
}
else if (node->val < target) {
int lessCount = node->count - node->right->count;
int rightCount = update(node->right, target);
node->count++;
return lessCount + rightCount;
}
else {
int leftCount = update(node->left, target);
node->count++;
return leftCount;
}
}
vector<int> countSmaller(vector<int>& nums) {
vector<int> sortedArray = nums;
sort(sortedArray.begin(), sortedArray.end(), less<int>());
treeNode* node = buildTree(sortedArray, , sortedArray.size() - );
vector<int> res(nums.size());
for (int i = nums.size() - ; i >= ; i--)
res[i] = update(node, nums[i]);
return res;
}
};

Count of Smaller Numbers After Self -- LeetCode的更多相关文章

  1. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

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

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

  6. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  7. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

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

  9. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

随机推荐

  1. Python+Selenium框架设计篇之-什么是POM

    前面我们介绍了Python中的单元测试框架unittest,以后我们所有的测试类文件,都采用unittest来辅助我们进行debug和脚本开发.搞定了debug机制和确定了unittest来进行创建和 ...

  2. 孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句

    孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 从结构化编程流行以来,代码便被分块存储,称之为模块或库. 在pyt ...

  3. (原) Unreal搬山-引言(图多慎)

    @author:白袍小道 扯淡:(图多) 何为搬山,这里借了剑来少年郎一句.(若有同道中人,甚是开心,开心的很) 江湖岂能没前辈) (江湖很大,足够你浪) (刺客信条 \荒野 \神秘海域 \死亡空间 ...

  4. google protobuf 中的proto文件编写规则

    1. 简单介绍 protobuf文件:就是定义你要的消息(类似Java中的类)和消息中的各个字段及其数据类型(类似java类中的成员变量和他的数据类型) 2. Protobuf消息定义 消息由至少一个 ...

  5. J2EE的十三种技术——JDBC

    背景: 之前准备软考的时候,我们就学习过J2SE的视频.在进入J2EE之前,一定要复习和回顾下Java的基础知识,这对以后的学习十分重要.首先,简单回忆下java的体系结构.Java有三个体系结构:J ...

  6. 【转】PHP对象在内存中的分配

    对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上 说大体上是分为4 段,栈空间段.堆空 ...

  7. js 清除文本中的html标签

    text.replace(/<[^>]+>/g,"");

  8. vue前端开发那些事(1)

    如上图所示,用vue开发一个小型网站所涉及到的知识点.这只是前端部分已经这么多了.接下来我分解开来说. 1.Node 当我们开发vue项目的时候,首先要安装Node.js,那么我们即使当时不理解为什么 ...

  9. [经验分享]Windows系统下生成IOS证书

    我使用ApiCloud开发APP,开发后需要生成IOS的证书才能在项目开发控制台中进行编译,于是我在网上大海捞针似的寻找办法. 官方文档提供了使用苹果系统下生成IOS证书的步骤,对于我这个没有imac ...

  10. windows 系统下git 的安装

    在linux系统下,可以直接在命令窗口安装和使用git.但是,在windows系统下,想要达到同样的效果,可以安装git,使用git bash到达效果.具体安装步骤如下: 第一步:官网上下载git 网 ...