leetcode315 Count of Smaller Numbers After Self
思路:
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的更多相关文章
- LeetCode315—Count of Smaller Numbers After Self—Java版归并算法
这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释. 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [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 ...
- 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 ...
- [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 ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
随机推荐
- iOS 摇一摇功能的实现
在 UIResponder中存在这么一套方法 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_A ...
- web中使用svg失量图形及ie8以下浏览器的处理方式
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- js中的width问题
1.在jQuery中,width()方法用于获得元素宽度: innerWidth()方法用于获得包括内边界(padding)的元素宽度, outerWidth()方法用于获得包括内边界(padding ...
- Linux时间子系统之四:定时器的引擎:clock_event_device【转】
本文转载自:http://blog.csdn.net/droidphone/article/details/8017604 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...
- 51nod 1600 Simple KMP
又被机房神犇肉丝哥哥和glory踩爆了 首先这个答案的输出方式有点套路,当前的答案=上一个答案+每一个后缀的f值=上一个答案+上一次算的每个后缀的f值+当前每个后缀的深度 这个题意给了个根深度为-1有 ...
- 实现自定义xib和storyboard的加载,
一:加载xib 1.分别创建xib,.h .m文件继承自UIView. 在xib上绑定类名. 或者创建文件的时候直接勾选xib 2.在控制器中调用类方法 jyq52787网盘/ios/潭州学院/iO ...
- CodeForces937B:Vile Grasshoppers(素数性质)
The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. ...
- [Java]手动编译Java
1.安装JDK 2.编写 Example.java 程序放到C 盘 public class Example { public static void main(string[] args) { sy ...
- 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...
- Synchronized之三:Synchronized与线程中断、线程wait
线程中断 见<Thread之八:interrupt中断> 正如中断二字所表达的意义,在线程运行(run方法)中间打断它,在Java中,提供了以下3个有关线程中断的方法 //中断线程(实例方 ...