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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- 转载blog_Linux下Tomcat日志定期清理 及 logrotate 配置
服务器上的tomcat的catalina.out文件越来越大,查看起来很不方便,以前每次都是想起来的时候手工清理一下(cat /dev/null > catalina.out),后来发现了log ...
- archlinux下查看机器的ip地址
问题原因 默认archlinux没有安装ifconfig这个在其它发行版中常用的命令,刚开始不知道怎么查看机器的ip. 解决方案 使用archlinux中已经安装的ip命令.在后面跟上addr, ro ...
- IIS 7.0 下 httpMoudle 失效的问题
在web.config里配置了: <system.web> <httpModules> <add type="DevExpress.Web.ASPxClass ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- sessionStorage和localStorage中 存储
转:http://my.oschina.net/crazymus/blog/371757 sessionStorage只在页面打开是起作用, localStorage关闭页面后仍然起作用. 有时候,我 ...
- 【NOIP2010】关押罪犯
一开始看错题了,然后怎么想都想不明白--原题: S 城现有两座监狱,一共关押着 N 名罪犯,编号分别为 1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲 ...
- C++实现水波纹、火焰和血浆效果
点击这里查看原文 Code Project着火了! 整个工程有三个类,它们可以让你在图象上添加一些很酷的效果. 我把这些文件都放到我的代码压缩包里面了,并且做了一个小工程来让一些人使用起来更方便,但是 ...
- PADS Logic 常见错误报告内容
1.PCB Decal LED0805 not found in Library pcb封装不在库中. 找到原图中的pcb-save to library 未分配PCB时候,右键Edit part-找 ...
- linux free命令中buffer与cache的区别
linux free命令中buffer与cache的区别 2012-05-15 个评论 收藏 我要投稿 linux free命令中buffer与cache的区别 ~$ ...
- Iaas-cloudstack概念
管理serverother high end ports tomcat20400 tomcat server.xml ...