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

 public class Solution {
/**
* @param A: An integer array
* @return: The number of element in the array that
* are smaller that the given integer
*/ class TreeNode {
int value;
int count;
int leftsize;
TreeNode left;
TreeNode right;
public TreeNode(int value) {
this.value = value;
this.count = 1;
this.left = null;
this.right = null;
this.leftsize = 0;
}
} public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
if (A==null || queries==null || queries.length==0)
return res;
if (A.length == 0) {
for (int i=0; i<queries.length; i++)
res.add(0);
return res;
}
TreeNode root = new TreeNode(A[0]);
for (int i=1; i<A.length; i++) {
insert(root, A[i]);
}
for (int query : queries) {
res.add(queryTree(root, query));
}
return res;
} public TreeNode insert(TreeNode cur, int value) {
if (cur == null) {
cur = new TreeNode(value);
}
else if (cur.value == value) {
cur.count++;
}
else if (cur.value > value) {
cur.leftsize++;
cur.left = insert(cur.left, value);
}
else {
cur.right = insert(cur.right, value);
}
return cur;
} public int queryTree(TreeNode cur, int target) {
if (cur == null) return 0;
else if (cur.value == target) return cur.leftsize;
else if (cur.value > target) {
return queryTree(cur.left, target);
}
else {
return cur.leftsize + cur.count + queryTree(cur.right, target);
}
}
}

Lintcode: Count of Smaller Number的更多相关文章

  1. LintCode "Count of Smaller Number before itself"

    Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...

  2. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  3. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  4. Lintcode248 Count of Smaller Number solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...

  5. 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 1 ...

  6. [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self

    You are given an integer array nums and you have to return a new countsarray. The counts array has t ...

  7. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  8. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  9. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

随机推荐

  1. font awesome

    http://stackoverflow.com/questions/21406538/how-to-use-font-awesome-icons-from-node-modules

  2. Intel Visual Fortran Compiler 11调用lapack库实现并行多处理计算

    采用fortran进行数值计算的朋友们都应该听说过大名鼎鼎的lapack库,我就不多做介绍了,在此,我只是介绍一个编译好的lapack二进制包ACML(AMD Core Math Library),并 ...

  3. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  4. 【转】深入浅出 JavaScript 中的 this

    Java 等面向对象的语言中,this 关键字的含义是明确且具体的,即指代当前对象.一般在编译期确定下来,或称为编译期绑定.而在 JavaScript 中,this 是动态绑定,或称为运行期绑定的,这 ...

  5. android硬件调试之无法识别android设备解决办法

    DDMS 中无法识别华为荣耀六手机,  用豌豆荚开始显示无法连接,  用豌豆荚安装完驱动后,就可以连接了 http://www.zhihu.com/question/30588024 http://w ...

  6. C#程序读取数据库中包含null的列的值

    private void btn2_Click(object sender, RoutedEventArgs e)         {             using (SqlConnection ...

  7. 【指标测试】影响IOPS的几个重要因素

    1. 读写方式 顺序读写的IOPS要比随机读写的IOPS高.100%顺序读写来讲,顺序读要高于顺序写.100%随机读写来讲,随机读要高于随机写.小块读写的IOPS要比大块读写高.需要根据实际的应用程序 ...

  8. Mysql-5.6.30卸载

    Mysql-5.6.30卸载 一.删除相关文件 rm  -rf  /var/lib/mysql/mysql   (删除数据文件) rm  -f  /root/.mysql_secure    (删除缺 ...

  9. you need to upgrade the working copy first

    is too old (format 29) to work with client version '1.9.4 (r1740329)' (expects format 31) 2016年09月18 ...

  10. Linux权限值问题

    0660:从左向右:第一位:(我不清楚,也没有用过)第二位:当前用户的经权限:6=110(二进制),每一位分别对就 可读,可写,可执行,,6说明当前用户可读可写不可执行第三位:group组用户,6的意 ...