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. C语言Makefile文件使用

    C语言中代码Makefile文件的写法 单文件,例: #定义变量 CFLAGS=gcc #具体命令都需要一个入口,all: 这个就相当于入口,默认情况,执行第一次入口, #后面执行其他入口进行依赖,如 ...

  2. ubuntu13 安装并配置ssh,交换密钥

    1.下载安装sudo apt-get install ssh,若出现问题,可选择安装openssh-server,或ssh-server等 2.启动ssh,命令:sudo /etc/init.d/ss ...

  3. abap append 用法

    [转自http://blog.chinaunix.net/uid-7982817-id-91999.html]Append用法总结 2008-11-14 11:42:19 分类: Syntax APP ...

  4. 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  5. Matlab GUI设计中的一些常用函数

    Matlab GUI常用函数总结 % — 文件的打开.读取和关闭% — 文件的保存% — 创建一个进度条% — 在名为display的axes显示图像,然后关闭% — 把数字转化为时间格式% — ch ...

  6. Jquery_笔记

    1.请确保在 <body> 元素的onload事件中没有注册函数,否则不会触发+$(document).ready()事件.

  7. Apache配置站点根目录、用户目录及页面访问属性

    一.配置站点根目录及页面访问属性 DocumentRoot "/www/htdoc" <Directory "/www/htdoc"> Option ...

  8. Codeforces Round #126 (Div. 2)

    A. Cinema 假设当前要的位置为\((x, y)\),如果枚举答案的横坐标,那么每次找离\(y\)最近的纵坐标. 如果占用了位置\((x,y)\),需要要更新第\(x\)行的信息,而占用位置\( ...

  9. window7资源管理器一直重启(百度知道找到可用)

    今天我的机器也出现这种问题:我的解决方式是,在开机时选择系统修复选项中的进入命令行方式(尝试过用安全模式,文件被占用,现象一样),然后cd C:\Users\Administrator\AppData ...

  10. LeetCode(228) Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...