Lintcode: Count of Smaller Number

本文介绍了一种使用构造二叉搜索树(BST)的方法来解决数组中元素计数问题,并通过实例展示了如何实现。文章还提供了三种实现方式:简单循环、排序加二分查找以及构建段树进行搜索。
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 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/5176693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值