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 from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it and return count number array.

Notice:We suggest you finish problem Segment Tree Build,Segment Tree Query II and Count of Smaller Number first.

给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000)。对于数组中的每个ai元素,请计算ai前的数中比它小的元素的数量。

【注】:做此题前最好先完成 Segment Tree Build,Segment Tree Query IICount of Smaller Number

【题目链接】

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

【题目解析】

此题可用线段树来做。

首先以0-10000为区间建树,并将所有区间count设为0。每一个最小区间(即叶节点)的count代表到目前为止遇到的该数的数量。

然后开始遍历数组,遇到A[i]时,去查0-A[i]-1区间的count,即为比A[i]小的数的数量

查完后将A[i]区间的count加1即可

【参考答案】

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

转载于:https://my.oschina.net/u/3776581/blog/1621115

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值