[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 ...
随机推荐
- 走进React的学习之路
GIT: 代码管理.https://git-scm.com/book/zh/v2 ES6: http://www.infoq.com/cn/minibooks/ES6-in-Depth Webpack ...
- XML序列化/反序列化数据库形式保存和读取。
直接上码: 首先创建class1类 public class Class1 { public string name { get; set; } public int age { get; set; ...
- Container.ItemIndex 获取到行的序号
如果在ASP.NET中应用了Repeater.Gridview,想获取到行的序号,很简单,使用Container.ItemIndex即可.在Gridview中使用<%# Container.Da ...
- 如何在VC++ 中调试MEX文件
MEX文件对应的是将C/C++文件语言的编写之后 得到的相关文件加载到Matlab中运行的一种方式, 现对于Matlab 中的某些程序运行效率而言, C/C++ 代码某些算法的领域上面执行效率很高,若 ...
- myeclipse、eclipse去掉无用的workSpace
在 eclipse\configuration\.settings\org.eclipse.ui.ide.prefs 文件里面有下面这段,我们可以从 RECENT_WORKSPACES 里面看到它列出 ...
- bzoj1007:[HNOI2008]水平可见直线
思路:首先按斜率排序,如果斜率相同就取截距最大的,显然截距小的会被覆盖而对答案没有贡献,然后考虑斜率不同的如何统计答案,可以用一个单调栈维护,当前新插入的直线显然斜率是要比当前栈顶斜率要大的,然后如果 ...
- 九度OJ 1082 代理服务器 -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...
- yaffs2文件系统
1 .yaffs2源码目录文件复制到需要移植的linux内核目录fs/下 同时替换掉源码文件中的Makefile文件跟Kconfig文件. 2.在内核中添加对yaffs2的支持. 3.在make me ...
- ci 用本身 email 类发 email
//比如 在控制器用 email 方法发送邮件 //用126的smtp 发送,示例邮件为 myemail@126.com 密码为 password public function email() { ...
- 高效的VS调试技巧
本文总结了十个调试技巧,当你使用VS的时候可以节省你很多时间. 1.悬停鼠标查看表达式 调试有时候很有挑战性,当你步入一个函数想看看哪块出错的时候,查看调用栈来想想值是从哪来的.另一些情况下,则需要添 ...