Leetcode 315. Count of Smaller Numbers After Self

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 315. Count of Smaller Numbers After Self相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Count of Smaller Numbers After Self

2. Solution

**解析:**Version 1,从右往左遍历数组,每次都将数据放到有序序列里,使用二分查找寻找数据所在的位置,索引位置即为右侧小于数据的个数。

  • Version 1
class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        n = len(nums)
        ans = [0] * n
        order = []
        for i in range(n-1, -1, -1):
            index = bisect.bisect_left(order, nums[i])
            ans[i] = index
            order.insert(index, nums[i])
        return ans

Reference

  1. https://leetcode.com/problems/count-of-smaller-numbers-after-self/

以上是关于Leetcode 315. Count of Smaller Numbers After Self的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self

Leetcode 315. Count of Smaller Numbers After Self

LeetCode 315. Count of Smaller Numbers After Self(线段树,树状数组)