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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- java 中的几种 "通用方法“
前言 Java中,除了基本的数值类型,其他所有数据类型(包括数组)都是对象. 而Object这个类是所有类的超类,它提供的方法,自然能够使用于它的所有子类(所有非基本数值类型). 本文介绍了Objec ...
- 编程工具系列之一------使用GDB的堆栈跟踪功能
在调试程序的过程中,查看程序的函数调用堆栈是一项最基本的任务,几乎所有的图形调试器都支持这项特性. GDB调试器当然也支持这一特性,但是功能更加灵活和丰富. GDB将当前函数的栈帧编号为0,为外层函数 ...
- 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- c#部分---结构体再利用;
//定义一个结构体,存放关于车辆的几个信息 //将所有车的信息都放入集合中 //车型号 价格(W) 轴距 (mm) 油耗(L/100km) //宝马320Li 38 2920 6.9 //宝马520L ...
- java parseint()
static int parseInt(String s) static int parseInt(String s, int radix) parseInt(String s)表示将 “数字” 转换 ...
- linux笔记_磁盘分区
一.分区的意义 1.不同操作系统往往不可以同时装载在同一个分区,分区解决了不同操作系统装载在同一个物理硬盘的兼容性问题 2.机械硬盘盘片外圈读写速度相对内圈要快,分区可以把常用数据限制在读写速度较快的 ...
- mysql 新建用户
//=============================================================================== //代理商子账户订单服务器为了安全 ...
- Java—面向对象—构造方法及相关思维导图
先构造一个Book类的代码 package org.hanqi.pn0120; public class Book { //构造方法 //1.方法名和类名一样 //2.没有返回值,不需要加void / ...
- mysql数据库从库同步延迟的问题
在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下,希望文章对各位会有所帮助. 在从服务器上执行show slave status;可以查看到很多 ...
- 黑马程序员——JAVA基础之单列设计模式
------- android培训.java培训.期待与您交流! ---------- 单列设计模式是面试中的一个常考的点,所谓单例模式就是说对象在内存中只能存在一个.如果有其他变量是该类对象,那么他 ...