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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- Oracle SQL语句追踪
Oracle SQL语句追踪 1 SQL语句追踪 追踪SQL语句的执行过程需要在Oracle服务器端进行,Oracle服务器端会检测并记录访问进程所执行的所有SQL语句.下面使用的命令都是在命令行 ...
- Container ViewController初探1
今天调试程序遇到个问题,iOS7下在弹出Modal的子界面时,弹出层次不对,键盘和界面被分割在了Window的两侧,导致显示异常Presenting view controllers on detac ...
- Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 整理的一些免费的Android项目实战系列视频教程
http://blog.itpub.net/29737144/viewspace-1212539/
- Base64编码的实现(三种方式)
package com.smart.base; import org.apache.commons.codec.binary.Base64; public class Base64Test { pri ...
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.,appcompatv7
error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom ...
- 奇怪的电梯(HDU1548) (Dijkstra)或者(BFS)
问题 E: 奇怪的电梯 时间限制: 1 Sec 内存限制: 64 MB提交: 35 解决: 16[提交][状态][讨论版] 题目描述 有一天桐桐做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都 ...
- Android ——Handler相关
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- JAVA条件语句
1.if if (判断条件) { 条件成立时执行的代码 } 2.if..else if (判断条件) { 条件成立时执行的代码 } else { 条件不成立时执行的代码 } 3.多重if if (条件 ...
- 【NOIP2010】关押罪犯
一开始看错题了,然后怎么想都想不明白--原题: S 城现有两座监狱,一共关押着 N 名罪犯,编号分别为 1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲 ...