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 }
时间: 2024-12-28 21:29:57

Lintcode: Count of Smaller Number的相关文章

[LintCode] Count of Smaller Number before itself

Count of Smaller Number before itself Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

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 that the given integer. hint:

[LeetCode][JavaScript]Count of Smaller Numbers After Self

Count of Smaller Numbers After Self You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Given nums = [5,

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description You are given an integer array nums and you have to return a new counts array. The counts array has the property where $counts[i]$

剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解

剑指Offer 面试题36:数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 例如, 在数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6),(7,5),(7,4),(6,4)和(5,4),输出5. 提交网址: http://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188 或 htt

Count of Smaller Numbers After Self -- LeetCode

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Given nums = [5, 2, 6, 1] To the right of 5 there are

Count of Smaller Numbers After Self

题目: You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Given nums = [5, 2, 6, 1] To the right of 5 there

315. Count of Smaller Numbers After Self

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Given nums = [5, 2, 6, 1] To the right of 5 there are