zoukankan      html  css  js  c++  java
  • 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 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.
    
    Have you met this question in a real interview? Yes
    Example
    For array [1,2,7,8,5], and queries [1,8,5], return [0,4,2]
    
    Note
    We suggest you finish problem Segment Tree Build and Segment Tree Query II first.
    
    Challenge
    Could you use three ways to do it.
    
    Just loop
    Sort and binary search
    Build Segment Tree and Search.

    count of Range Sum 很像, 维护一个leftsize, 记录左子树节点个数, 为方便起见,再维护一个count,记录重复节点

    construct BST, time complexity: O(N) construct tree + O(logN) queries

     1 public class Solution {
     2    /**
     3      * @param A: An integer array
     4      * @return: The number of element in the array that 
     5      *          are smaller that the given integer
     6      */
     7      
     8     class TreeNode {
     9         int value;
    10         int count;
    11         int leftsize;
    12         TreeNode left;
    13         TreeNode right;
    14         public TreeNode(int value) {
    15             this.value = value;
    16             this.count = 1;
    17             this.left = null;
    18             this.right = null;
    19             this.leftsize = 0;
    20         }
    21     } 
    22     
    23     public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
    24         // write your code here
    25         ArrayList<Integer> res = new ArrayList<Integer>();
    26         if (A==null || queries==null || queries.length==0)
    27             return res;
    28         if (A.length == 0) {
    29             for (int i=0; i<queries.length; i++)
    30                 res.add(0);
    31             return res;
    32         }
    33         TreeNode root = new TreeNode(A[0]);
    34         for (int i=1; i<A.length; i++) {
    35             insert(root, A[i]);
    36         }
    37         for (int query : queries) {
    38             res.add(queryTree(root, query));
    39         }
    40         return res;
    41     }
    42     
    43     public TreeNode insert(TreeNode cur, int value) {
    44         if (cur == null) {
    45             cur = new TreeNode(value);
    46         }
    47         else if (cur.value == value) {
    48             cur.count++;
    49         }
    50         else if (cur.value > value) {
    51             cur.leftsize++;
    52             cur.left = insert(cur.left, value);
    53         }
    54         else {
    55             cur.right = insert(cur.right, value);
    56         }
    57         return cur;
    58     }
    59     
    60     public int queryTree(TreeNode cur, int target) {
    61         if (cur == null) return 0;
    62         else if (cur.value == target) return cur.leftsize;
    63         else if (cur.value > target) {
    64             return queryTree(cur.left, target);
    65         }
    66         else {
    67             return cur.leftsize + cur.count + queryTree(cur.right, target);
    68         }
    69     }
    70 }
  • 相关阅读:
    Day22:异常处理、socke基于TCP协议编程
    Day21:面向对象的软件开发、反射、对象的内置方法
    Day20:绑定方法与非绑定办法、多态和多态性
    Day19:继承实现的原理、子类中调用父类的方法、封装
    Day18:类的抽象、类的组合应用
    Day17:类的继承、派生、组合和接口
    Day16:面向对象编程——类和对象
    数据结构
    python爬虫篇之 性能相关
    scrapy-redis
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5176693.html
Copyright © 2011-2022 走看看