【题目描述】

Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) and an query list. For each query, give you an integer, return the number of element in the array that are smaller than the given integer.

Notice:We suggest you finish problem Segment Tree Build and Segment Tree Query II  first.

给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。

【注】在做此题前,最好先完成线段树的构造线段树查询 II这两道题目。

【题目链接】

www.lintcode.com/en/problem/count-of-smaller-number/

【题目解析】

此题可以用排序+二分查找的方法来做。先对数组排序,然后对于每个查询,用二分查找在数组中进行查询,找到原数组中第一个比查询数大的数,然后再从后往前统计。

由于这道题目不是查找==而是选择第一个>(num)的数的位置,所以while语句里面可以把>和=归为同一个分支>=,因为(==)存在包含重复数(duplicate)的情况,所以要和>一样,end指针前移替换mid。

那么另一个分支<,除了将start后移,还要更新返回值res。

第二点,如果while循环的约束条件是start < end,假如循环到最后start = end - 1,并且num就在end呢?这时应该返回res = start + 1,推测前一步,start = end - 2的时候,end的前移只能到mid为止,不能是mid - 1,否则就跳过了可能为所求结果的mid。

【参考答案】

www.jiuzhang.com/solutions/count-of-smaller-number/

Lintcode248 Count of Smaller Number solution 题解的更多相关文章

  1. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  2. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  3. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  4. LintCode "Count of Smaller Number before itself"

    Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...

  5. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

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

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

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

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

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

  9. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

随机推荐

  1. [工具]Microsoft To-Do,简约还是简陋?

    1. 简介 微软收购奇妙清单后,由奇妙清单的原班人马打造了一个全新的待办事项应用,就叫"To-Do"(简单粗暴,好像新浪微博直接就叫"微博"的感觉).这个应该刚 ...

  2. PHP实现网页爬虫

    抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现的网页抓取方式,如果熟悉JQuery选择器,这几种框架会相当简单. 一 ...

  3. Redis进阶实践之十四 Redis-cli命令行工具使用详解第一部分

    一.介绍       redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redis的东西要看的都差不多看完了.网上的东西也不多了.剩下来就看看官网的东西吧,一遍翻译,一遍测试. ...

  4. 一个可以自由定制外观、支持拖拽消除的MaterialDesign风格Android BadgeView

    为了尊重作者,先放上链接:https://github.com/qstumn/BadgeView BadgeView 一个可以自由定制外观.支持拖拽消除的MaterialDesign风格Android ...

  5. 裁剪Ubuntu内核和模块管理2

    准备工作 通常要运行的第一个命令是: #cd /usr/src/linux;make mrproper 该命令确保源代码目录下没有不正确的.o文件以及文件的互相依赖.由于我们使用刚下载的完整的源程序包 ...

  6. 10种软件开发中 over-engineering 的错误套路

    别把「不要过度使用 Generic」误解成「不用 Generic」,也别把「不要写一些不必要的 Wrapper」误解成「不写任何 Wrapper」.我只是在讲 over-engineering 这个事 ...

  7. UTF-8 的BOM带来的麻烦

    UTF-8 的BOM带来的麻烦 工作需要我用程序生成一个html文件. 由于服务器端使用apache+Tomcat来执行html和jsp文件. 开始生成html文件放在apache目录下,页面无法默认 ...

  8. 安装STS报错(一)

    安装STS报错 1.具体报错如下 2.报错原因 3.解决办法

  9. LINQ 按多个字段排序(orderby、thenby、Take)

    LINQ 按多个字段排序(orderby.thenby.Take) orderby  子句解析为 OrderBy()方法,orderby descending 子句解析为OrderBy Descend ...

  10. Linux以列表格式显示块设备

    Linux以列表格式显示块设备 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ lsblk -l NAME MAJ:MIN RM SIZE RO TYPE MO ...