LintCode 248 [Count the Smaller Number]

原题

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

对于数组 [1,2,7,8,5] ,查询 [1,8,5],返回 [0,4,2]

解题思路

  • 方法一,将数组快速排序,然后对于某个数x,二分查找最后一个x-1的位置
  • 方法二,建立值型线段树
  • 对于方法二要注意查找x-1可能出现负值,要做判断,x-1 < 0直接res.append(0)

完整代码

# 方法一
class Solution:
    """
    @param A: A list of integer
    @return: The number of element in the array that 
             are smaller that the given integer
    """
    def countOfSmallerNumber(self, A, queries):
        A.sort()
        res = []
        for q in queries:
            res.append(self.SmallerNumber(A, q))
        return res
        
    def SmallerNumber(self, L, num):
        if len(L) == 0 or L[-1] < num:
            return len(L)
            
        start, end = 0, len(L) - 1
        while start + 1 < end:
            mid = start + (end - start) / 2
            if L[mid] >= num:
                end = mid
            else:
                start = mid
        
        if L[start] >= num:
            return start
        if L[end] >= num:
            return end

# 方法二
class segmentTreeNode:
    def __init__(self, start, end, count):
        self.start, self.end, self.count = start, end, count
        self.left, self.right = None, None
        
class Solution:
    """
    @param A: A list of integer
    @return: The number of element in the array that 
             are smaller that the given integer
    """
    def build(self, start, end):
        if start == end:
            root = segmentTreeNode(start, end, 0)
            return root
            
        root = segmentTreeNode(start, end, 0)
        if start != end:
            mid = (start + end) / 2
            root.left = self.build(start, mid)
            root.right = self.build(mid + 1, end)
        return root
        
    def query(self, root, start, end):
        if root.start == start and root.end == end:
            return root.count
            
        mid = root.start + (root.end - root.start) / 2
        leftCount, rightCount = 0, 0
        if start <= mid:
            if mid < end:
                leftCount = self.query(root.left, start, mid)
            else:
                leftCount = self.query(root.left, start, end)
        if mid < end:
            if start <= mid:
                rightCount = self.query(root.right, mid + 1, end)
            else:
                rightCount = self.query(root.right, start, end)
        return leftCount + rightCount
        
    def modify(self, root, index, value):
        if root.start == index and root.end == index:
            root.count += value
            return
        
        mid = root.start + (root.end - root.start) / 2
        if root.start <= index and index <= mid:
            self.modify(root.left, index, value)
        
        if mid < index and index <= root.end:
            self.modify(root.right, index, value)
        
        root.count = root.left.count + root.right.count
        
    def countOfSmallerNumber(self, A, queries):
        root = self.build(0, 1000)
        
        for num in A:
            self.modify(root, num, 1)
            
        res = []
        for q in queries:
            ans = 0
            if q > 0:
                ans = self.query(root, 0, q - 1)
            res.append(ans)
        
        return res
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近在写个性化推荐的论文,经常用到Python来处理数据,被pandas和numpy中的数据选取和索引问题绕的比较...
    shuhanrainbow阅读 4,607评论 6 19
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,997评论 19 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 1、用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回。 2、用C语言实现函数void ...
    希崽家的小哲阅读 6,360评论 0 12
  • 昨天下完班后,几个战友打来电话约我出去坐坐,一聊就是以前在部队上的事。其实这个世界上“战友情,兄弟情”是无法用...
    df872c2ae1f1阅读 1,406评论 0 3