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 ...
随机推荐
- Unable to resolve target android-5解决方案
1:问题:android导入项目的时候出现此错误 2:原因: 3:解决: 修改工程目录下的default.properties文件里的内容target=android-5 这个5修改成你的api版本就 ...
- 蓝牙BlueTooth技术学习理解
1.BLUETOOTH基本了解 BLUETOOTH出自丹麦 Bluetooth SIG 蓝牙技术联盟,非盈利组织.主要任务是发布蓝牙规格.管理资格认证程序.保护蓝牙商标及宣传蓝牙无线技术. 重要网站 ...
- 一步一步学Silverlight 2系列(18):综合实例之RSS阅读器
一步一步学Silverlight 2系列(18):综合实例之RSS阅读器 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支 ...
- lucene 5的测试程序——API变动太大
package hello; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import ...
- hadoop-3.0.0 配置中的 yarn.nodemanager.aux-services 项
在hadoop-3.0.0-alpha4 的配置中,yarn.nodemanager.aux-services项的默认值是“mapreduce.shuffle”,但如果在hadoop-2.2 中继续使 ...
- fastText入门
简介fastText是Facebook AI Research在2016年提出的文本分类和词训练的工具.它最大的特点:模型非常简单,训练速度快,并且能够达到与深度学习旗鼓相当的精度. 最近在做一个给微 ...
- this关键字使用
原文地址:https://www.cnblogs.com/alsf/p/5515996.html 一,表示类中属性 1,没有使用this的情况 class Person{ // 定义Person类 p ...
- 【210】通过OleDb读写Excel数据到DataTable
参考:C#通过OLEDB读写Excel2013显示到datagrid控件,修改数据集并更新excel2013 解决:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序.( ...
- Jmeter学习之While Controller
参考 https://www.cnblogs.com/richered/p/8404641.html https://blog.csdn.net/rwang99/article/details/511 ...
- typeof操作符返回一个字符串,表示未经计算的操作数的类型。
typeof操作符返回一个字符串,表示未经计算的操作数的类型. 语法 typeof运算符后跟操作数: typeof operand or typeof (operand) 参数 operand 是 ...