Lintcode: Count of Smaller Number

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于Lintcode: Count of Smaller Number的主要内容,如果未能解决你的问题,请参考以下文章

LintCode Triangle Count

LintCode: Maximum Depth of Binary Tree

lintcode-easy-Count and Say

lintcode-easy-Length of Last Word

lintcode-easy-Number of Islands

lintcode-easy-Maximum Depth of Binary Tree