[LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
int n = nums.size();
vector<int> v(n);
for (int i = n - 1; i >= 0; --i) {
int val = nums[i];
int L = i + 1, R = n - 1;
while (L <= R) {
int M = L + (R - L) / 2;
if (nums[M] >= val) {
L = M + 1;
} else {
R = M - 1;
}
}
for (int j = i; j < R; ++j) {
nums[j] = nums[j + 1];
}
nums[R] = val;
v[i] = n - R - 1;
}
return v;
}
};
// 1320 ms
算法思想: 从右端折半插入. 对于每个数字来说, 数组长度 - 插入后的位置 - 1
就是该数字的count
.
时间复杂度: O(n^2)
.
空间复杂度: O(1)
.
还有更快的方法. 以后更新.
[LeetCode] 315. Count of Smaller Numbers After Self (Hard)的更多相关文章
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The countsarray has t ...
- LeetCode 315. 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(HARD) 主席树
Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...
- 315.Count of Smaller Numbers After Self My Submissions Question
You are given an integer array nums and you have to return a new counts array. Thecounts array has t ...
- 315. 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 ...
- 315. Count of Smaller Numbers After Self(Fenwick Tree)
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
给定一个整型数组 nums,按要求返回一个新的 counts 数组.数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于nums[i] 的元素的数量.例子:给定 nu ...
随机推荐
- HTML_常见命令学习笔记
1. java类中的这段代码 out.println(" <div class='line'>"); out.println(" <div align= ...
- WPF动画之路径动画(3)
XAML代码: <Window x:Class="路径动画.MainWindow" xmlns="http://schemas.microsoft.com/winf ...
- 【制作镜像Win*】环境准备
1. 保证网络通 2. 保证系统为centos6.x/rhel 6.x 3. yum源正确,推荐使用utsc源,nailgun也ok 4.安装软件包 virsh list guestmount yum ...
- 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- MongoDB源码编译
MongoDB源码编译 本人编译的版本编译的版本为mongodb2.6分支,目前MongoDB3.0已经发布,编译步骤和2.6的差不多,不过3.0版本要求编译器支持c++11标准,所以如果是在Linu ...
- greenplum学习
公司TM蛋疼,动不动让你学习新东西,就是不让你闲下来,本着胳膊拧不过大腿定律,忍了,这是背景. 好吧哥端起一本厚厚的<GreenPlum企业应用实战>,打开百度开始GP的学习之路: GP只 ...
- centOS 6 python MySQLdb 提示 no module
http://www.cnblogs.com/czh-liyu/archive/2012/11/30/2796028.html(转) 用python连接本地数据库时,提示no module MySQL ...
- (转) sphinx 高亮显示搜索词
http://hi.baidu.com/tewuapple/item/7a7bc34adbda24a8df2a9fe5 (转)
- 系统重装后phpnow修复
最近在捣鼓wordpress,主题写了一半然后就重装了win8,在新系统里面访问127.0.0.1的时候出现无法访问的情况.主题写了一半,又不想重装wordpress导数据库这些繁琐的过程,于是,尝试 ...
- 《JavaScript高级程序设计》笔记(3):传递参数
待更新... 9.17更新: ECMAScript中所有函数的参数都是按值传递的.也就是说,把函数外部复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样.基本类型值的传递如同基本类型变量的复 ...