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)的更多相关文章

  1. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. [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 ...

  4. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  5. 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

    Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数

    给定一个整型数组 nums,按要求返回一个新的 counts 数组.数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于nums[i] 的元素的数量.例子:给定 nu ...

随机推荐

  1. HTML文本格式化

    文本格式化标签: 标签 描述 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> ...

  2. lsjORM ----让开发变得更加快捷(二)

    lsjORM结构 跟传统三层没有多大区别,这里添加DTO(参数列表)跟PetaPoce(数据库操作),普通的三层我们都喜欢用DBHelper或者SqlHelper来封装sql的辅助方法,PetaPoc ...

  3. HTML 学习整理

    一.名词解释 一.  HTML : Hypertext Markup Language  超文本标记语言 二.  CSS : Cascading Style Sheet  层叠样式表 三.  浏览器: ...

  4. Android开发之ViewPager

    什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...

  5. BigInteger构造函数解析

    1.BigInteger(byte[] val)这个构造函数用于转换一个字节数组包含BigInteger的二进制补码,以二进制表示成一个BigInteger. (用字节数组中值的ASCII码构造Big ...

  6. [02] Oracle简单单行函数(字符+数值+日期+转换+通用)

    1. 字符函数 --upper(str), lower(str):将str转换成大,小写 select upper('hello') as x1, lower('HELlo') as x2 from ...

  7. IOS 本地通知

    操作流程 1.接收通知 2.注册发送通知 用途:提示时间,闹钟 //接收本地通知(在Appdelegate里面实现) - (void)application:(UIApplication *)appl ...

  8. c#对象初始化

    class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...

  9. gitlab的安装以及汉化

    gitlab的安装 首先在网上下载好任意版本gitlab的rpm包 推荐下面的地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gi ...

  10. nginx 自定义代理返回 404

    在nginx的http段,加上一面的配置 proxy_intercept_errors on;//自定义代理返回的404错误提示