LeetCode "Count of Smaller Number After Self"
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"的更多相关文章
- 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 ...
- [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 ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- 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 ...
- LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...
- 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 ...
- 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 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- Core Java Volume I — 3.4. Variables
3.4. VariablesIn Java, every variable has a type. You declare a variable by placing the type first, ...
- 十五个最常用Linux命令行 - imsoft.cnblogs
众多Linux管理员在使用Linux的时候会经常使用到很多Linux命令行,其中有绝大部分不是经常使用到的.在本文中主要为大家总结了经常使用的十五个最常用Linux命令行,希望对刚刚接触Linux命令 ...
- 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 阅读《构建之法》第八、九、十章有感和Sprint总结
1.阅读<构建之法>读后感 第八章:需求分析 需求分析,我觉得需求分析挺重要的,一个需求分析是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应 ...
- LinkedHashSet与TreeSet
区别 类型 实现 特点 TreeSet hashMap.实现sortedSet接口 升序(基本类型--),自定义 LinkedHashSet HashSet 初始顺序 **注意点: 1.treeSet ...
- SQLITE SUBSTR
insert into t_user values(4,'u1234567890'); sqlite> select substr(username,1,1) from t_user where ...
- 铺地毯 2011年NOIP全国联赛提高组
题目描述 Description 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从 ...
- 数据库必会必知 之 SQL四种语言:DDL DML DCL TCL(转)
今天群里面讨论,DDL 还是 DML,我这种小白还是总结下他们的区别吧. 1. DDL – Data Definition Language 数据库定义语言:定义数据库的结构. 其主要命令有CREAT ...
- mysql优化参数thread_cache_size
thread_cache_size功能在mysql数据库配置文件中是非常重要的一项功能了,如果对thread_cache_size优化做得好我们可以让服务器跑得非常快,设置不好就会发现很小访问量就非常 ...
- Visible 绑定
目的 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 <script src="knockout.js"></script><div da ...