思路:

bit + 离散化。

实现:

 #include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int sum(vector<int> & bit, int i)
{
int ans = ;
while (i) { ans += bit[i]; i -= i & -i; }
return ans;
}
void add(vector<int> & bit, int i, int x)
{
int n = bit.size() - ;
while (i <= n) { bit[i] += x; i += i & -i; }
}
vector<int> countSmaller(vector<int>& nums)
{
int n = nums.size();
vector<int> a(nums.begin(), nums.end()), b(nums.begin(), nums.end());
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = ; i < n; i++)
b[i] = lower_bound(a.begin(), a.end(), b[i]) - a.begin() + ;
vector<int> bit(n + , ), ans(n, );
for (int i = n - ; i >= ; i--)
{
add(bit, b[i], );
ans[i] = b[i] == ? : sum(bit, b[i] - );
}
return ans;
}
};
int main()
{
vector<int> v;
v.push_back();
v.push_back(-);
v.push_back();
v.push_back();
vector<int> ans = Solution().countSmaller(v);
for (auto it: ans) cout << it << " ";
cout << endl;
return ;
}

leetcode315 Count of Smaller Numbers After Self的更多相关文章

  1. LeetCode315—Count of Smaller Numbers After Self—Java版归并算法

    这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释. 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高 ...

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

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

  3. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

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

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

  5. [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self

    You are given an integer array nums and you have to return a new countsarray. The counts array has t ...

  6. Count of Smaller Numbers After Self -- LeetCode

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

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

  8. LeetCode Count of Smaller Numbers After Self

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

  9. LeetCode 315. Count of Smaller Numbers After Self

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

随机推荐

  1. ulink函数的使用【学习笔记】

    #include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("open error&quo ...

  2. POJ3104 Drying —— 二分

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  3. golang OOP面向对象

    摘自:http://www.01happy.com/golang-oop/ golang中并没有明确的面向对象的说法,实在要扯上的话,可以将struct比作其它语言中的class. 类声明 1 2 3 ...

  4. CollectionView网格布局

    说句老实话,UICollectionView真的太强大了,而且要掌握高级部分是相当困难的.至少笔者是这么认为的,如果觉得自己比较厉害,可以轻而易举地掌握UICollectionView的使用的,希望可 ...

  5. SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...

  6. GCD深入理解(1)

    写在前面 本文原文为raywenderlich的<grand-central-dispatch-in-depth-part-1>:顺便提及一下,笔者认为,对于iOS初学者而言,raywen ...

  7. 「网络流24题」「Codevs1237」 餐巾计划问题

    1237 餐巾计划问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond   题目描述 Description 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相 ...

  8. fastText入门

    简介fastText是Facebook AI Research在2016年提出的文本分类和词训练的工具.它最大的特点:模型非常简单,训练速度快,并且能够达到与深度学习旗鼓相当的精度. 最近在做一个给微 ...

  9. MYSQL数据库学习----索引和触发器

    一:索引 索引是创建在数据库表上,其作用是提高对表中数据的查询速度. 假设数据库中有一张1000条记录的表格,如果没有创建索引的话,用户想通过查询条件查询,实际上是把整个数据库中1000条记录都读取一 ...

  10. ORA-00020: maximum number of processes (xxxx) exceeded 报错解决方法

    转自:http://blog.51cto.com/lee90/1788124 今天java开发在连接线上的oracle大量导数据,一会提示连接不上数据库了.我本地用sqldeveloper也连接不上. ...