LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000...
Solution by segment tree:
- struct Node
- {
- Node(int s, int e) : start(s), end(e), cnt(), left(nullptr), right(nullptr)
- {};
- int start;
- int end;
- int cnt;
- //
- Node *left;
- Node *right;
- };
- class Solution {
- Node *pRoot;
- void update(int v)
- {
- Node *p = pRoot;
- while(p)
- {
- p->cnt ++;
- if(p->start == p->end) break;
- int mid = (p->start + p->end) / ;
- if(v <= mid)
- {
- if(!p->left)
- {
- p->left = new Node(p->start, mid);
- }
- p = p->left;
- }
- else
- {
- if(!p->right)
- {
- p->right = new Node(mid + , p->end);
- }
- p = p->right;
- }
- }
- }
- int query(Node *p, int v)
- {
- if(!p) return ;
- int mid = (p->start + p->end) / ;
- if(v < mid)
- {
- return query(p->left, v);
- }
- else if(v == mid)
- {
- return p->left ? p->left->cnt : ;
- }
- // v > mid
- return (p->left ? p->left->cnt : ) + query(p->right, v);
- }
- public:
- /**
- * @param A: An integer array
- * @return: Count the number of element before this element 'ai' is
- * smaller than it and return count number array
- */
- vector<int> countOfSmallerNumberII(vector<int> &A) {
- pRoot = new Node(, );
- vector<int> ret;
- for(auto v : A)
- {
- ret.push_back(query(pRoot, v - ));
- update(v);
- }
- return ret;
- }
- };
LintCode "Count of Smaller Number before itself"的更多相关文章
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- Lintcode249 Count of Smaller Number before itself solution 题解
[题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...
- Lintcode248 Count of Smaller Number solution 题解
[题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...
- Count of Smaller Number before itself
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- [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 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 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- dedecms 按照栏目指定的id排序
方法: 1.打开include/taglib/channelartlist.lib.php,找到大约78行,把 代码如下(一定要注意表名一致): $dsql->SetQuery("SE ...
- css中常用的hack
<!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #t ...
- selenium执行js报错
selenium执行js报错 Traceback (most recent call last): dr.execute_script(js) File "C:\Python27\l ...
- Optimizing shaper — hashing filters (HTB)
I have a very nice shaper in my linux box :-) How the configurator works — it’s another question, he ...
- Linux磁盘文件的命名
磁盘的常用接口有两种:IDE和SATA接口,目前主流的是SATA接口. IDE接口由IDE扁平电缆线连接,一个电缆可连接两个IDE接口,通常主机又都会提供两个IDE接口,因此最多可以接到四个IDE设备 ...
- CSS table-layout 、border-collapse属性
( table-layout)设置表格布局算法: 可能的值 值 描述 automatic 默认.列宽度由单元格内容设定. fixed 列宽由表格宽度和列宽度设定. inherit 规定应该从父元素继承 ...
- 快速对字符转义,避免跨站攻击XSS
XSS已经成为非常流行的网站攻击方式,为了安全起见,尽量避免用户的输入.可是有些情况下不仅不避免,反而要求鼓励输入,比如写博客.博客园开放性很高,可以运行手写的JS.之前比较著名的例子就是,凡是看到某 ...
- C函数及指针学习1
1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...
- Codeforces Round #144 (Div. 2)
A. Perfect Permutation 奇偶对调. B. Non-square Equation \(s(x)\)不超过200,根据求根公式计算\(x\). C. Cycles 每次新增点时都和 ...
- Compiler ,Interpreter, Linker
https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...