[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 ...
随机推荐
- jquery无法读取json文件问题
jquery无法读取json文件,如:user.json文件无法读取.把user.json文件的后缀名修改为aspx,文件内容不变,则可以读取~ 原理不懂!~~
- jquery click事件的可选参数data的作用
$("#ID").click({x:"value"},function (e) { alert(e.data.x); });
- Environment variable:"PATH" 状态 失败
问题截图: 问题内容: 未能满足某些最低安装要求.请复查并修复下表中列出的问题,然后重新检查系统. Checks Environment Variable: "PATH" ...
- jquery基础-包裹 替换 删除 复制
<!doctype html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 解读Spring Ioc容器设计图
在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...
- 总结一下const和readonly
const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初始化: const隐含static,不可以再写stat ...
- 基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置
第一章. 背景简介 对于大多数企业应用,都希望能做到7*24小时不间断运行.要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群.系统变为集群 ...
- 带缓冲的IO和不带缓冲的IO
文件描述符: 文件描述符是一个小的非负整数,是内核用来标识特定进程正在访问的文件 标准输入/输出/出错: shell为每个程序打开了三个文件描述符,STDIN_FILEON,STDOUT_FILEON ...
- OpenJudge/Poj 1517 u Calculate e
1.链接地址: http://bailian.openjudge.cn/practice/1517 http://poj.org/problem?id=1517 2.题目: 总时间限制: 1000ms ...
- MongoDB笔记(三)启动命令mongod的参数
上一节有关访问权限的笔记,是由启动命令mongod的参数auth引发的有关问题,这节就来看看mongod的其他参数 MongoDB启动命令mongod参数说明: 基本配置 --quiet # 安静输出 ...