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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- 十日谈 (share)
@拔赤 一直想写这篇“十日谈”,聊聊我对Web前端开发的体会,顺便解答下周围不少人的困惑和迷惘.我不打算聊太多技术,我想,通过技术的历练,得到的反思应当更重要. 我一直认为自己是“初级”前端开发工程师 ...
- Ionic基础——侧边栏ion-side-menus 以及ion-tap结合侧边栏详解
一. 侧边栏菜单 : ion-side-menus 侧边栏菜单是一个最多包含三个子容器的元素: 默认情况下,侧边栏菜单将只显示ion-side-menu-content容器的内容. 向左滑动时,将显示 ...
- 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 比较compareTo与equals及==的区别
1.compareTo: 附上:源码: public int compareTo(String anotherString) { int len1 = value.length; ...
- codeforces hungry sequence 水题
题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...
- layer 一款口碑极佳的web弹层组件,弹框专用
研究学习网址: http://layer.layui.com/
- Ubuntu安装Mysql+Django+MySQLdb
安装Mysql sudo apt-get install mysql-server mysql-client root@IdeaPad:/# mysql -u root -p Enter passwo ...
- VC线程中操作控件,引起程序卡死的问题。
[问题还原] 线程中操作控件,具体为控制一个按键的使能,使能后结束线程. 主程序中有一个死循环,等待线程结束. 然后,就没有然后了-- [解决方案] 在主程序死循环中,如果检测到界面消息,优先处理掉.
- Python实现ORM
ORM即把数据库中的一个数据表给映射到代码里的一个类上,表的字段对应着类的属性.将增删改查等基本操作封装为类对应的方法,从而写出更干净和更富有层次性的代码. 以查询数据为例,原始的写法要Python代 ...
- 黑马程序员——JAVA基础之基本数据类型包装类和1.5JDK新特性装箱
------- android培训.java培训.期待与您交流! ---------- 基本数据类型包装类: byte Byte short Short int Integer char Charac ...