Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to be taken care of.

class Solution {
//////////////////
// Fenwick Tree //
vector<long long> ft;
void update(int i, long long x)
{
if (i == )
{
ft[] ++;
return;
}
if ((i + ) > ft.size()) return;
for (; i < ft.size(); i += (i & -i))
ft[i] += x;
} long long query(int i)
{
if (i == ) return ft[]; i = min(i, int(ft.size() - ));
long long s = ;
for (; i > ; i -= (i & -i))
s += ft[i];
return s + ft[];
}
//////////////////
public:
vector<int> countSmaller(vector<int>& nums) {
ft.assign(, );
vector<int> ret;
if(nums.size() < ) return {};
// handling neg
auto r = minmax_element(nums.begin(), nums.end());
int minv = *r.first;
int maxv = *r.second;
int d = ;
vector<long long> ns;
if (minv < )
{
d = -minv + ;
}
for (auto v : nums)
ns.push_back(v + d);
// for (int i = ns.size() - ; i >= ; i--)
{
int v = ns[i];
int r = query(v - );
update(v, );
ret.push_back(r);
}
reverse(ret.begin(), ret.end());
return ret;
}
};

LeetCode "Count of Smaller Number After Self"的更多相关文章

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

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

  3. LeetCode Count of Smaller Numbers After Self

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

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

  5. LintCode "Count of Smaller Number before itself"

    Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...

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

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

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

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

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

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

随机推荐

  1. Spark读取HDFS文件,文件格式为GB2312,转换为UTF-8

    package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.hadoop.conf.Configur ...

  2. jbrowse 的配置与使用gff, vcf, fa, bed, bam

    1,jbrowse 是什么东西 ? JBrowse is a genome browser with a fully dynamic AJAX interface, being developed a ...

  3. PHP语言基础(标记、注释、变量、数组、常量、函数)

    PHP标记风格 1.xml风格(标准风格推荐使用) 代码如下: <?php  echo"这是xml风格的标记";  ?>  xml风格的标记是常用的标记,也是推荐使用的 ...

  4. python内存管理机制

    主要分为三部分: (1)内存池机制(2)引用计数(3)垃圾回收 (1)内存池机制对于python来说,对象的类型和内存都是在运行时确定的,所以python对象都是动态类型简单来说,python内存分为 ...

  5. Centos搭建openvpn+mysql数据库认证

    服务器环境说明 1.系统版本 CentOS release 5.10 (Final) 64bits 2.软件版本 openvpn-2.3.6-1.el5 lzo-2.02-2.el5.1 lzo-d ...

  6. 关于C中scanf()函数读取字符串的问题

    #include <stdio.h> int main(void) { ]; scanf("%s", s_name); printf("Hello, %s!\ ...

  7. c#部分---又见循环。。

    小时候背的诗,现在竟然这样玩. 又是循环,可见其无处不在的特性.就像“春眠不觉晓,处处蚊子咬.”

  8. JsonModelStrategy策略添加

    今天在学习 zf2的时候,发现后端如果返回的是JsonModel的话,则在前台异步请求的时候发现取不到数据了,后来经过多次研究,才发现要在view_manager中创建策略,代ma是这样子的,stra ...

  9. 论文笔记之:RATM: RECURRENT ATTENTIVE TRACKING MODEL

    RATM: RECURRENT ATTENTIVE TRACKING MODEL ICLR 2016 本文主要内容是 结合 RNN 和 attention model 用来做目标跟踪. 其中模型的组成 ...

  10. web-api-global-error-handling

    http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling http://www.cnblogs. ...