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. 应用Druid监控SQL语句的执行情况(转)

    Druid是什么? Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBo ...

  2. C# 网络通信基础 总结

    1.WebClient类 如果只是想从特定的URI(统一资源标识符)请求文件,则可以使用最简单的.NET类,System.Net.WebClient.支持http:.https:和file:标识符开头 ...

  3. (基础篇)PHP流程控制语句

    不论是PHP还是别的语法,程序总是由若干条语句组成. 从执行方式上看,语句的控制结构分为以下三种: 1.  顺序结构:从第一条语句到最后一条语句完全顺序执行: 2.  选择结构:根据用户输入或语句的中 ...

  4. Python 结巴分词

    今天的任务是对txt文本进行分词,有幸了解到"结巴"中文分词,其愿景是做最好的Python中文分词组件.有兴趣的朋友请点这里. jieba支持三种分词模式: *精确模式,试图将句子 ...

  5. FTP方式获取文件步骤

    1.在浏览器地址行输入FTP连接地址 2.进入FTP端之后,点击“檢視”-“在windows資源總覽中開啟FTP站臺”,這樣就可以打開FTP端的文件了

  6. 使用npoi.dll导出数据到excel

    .net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...

  7. my Highcharts

    1. a=a || {};  意义:如果a具有真值(不是undefined,null,NAN,false,0中的任意一种),则这个a可以被使用,否则将a定义为一个空的object对象{} 2. a | ...

  8. JS中的自定义属性

    <div id="div1" a="a" data-bbb="bbb">div</div> <script&g ...

  9. STL 简介,标准模板库

    这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL.  当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...

  10. C++ 学习小程序之 map 的用法

    1. map::at #include <iostream> #include <string> #include <map> using namespace st ...