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
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的更多相关文章
- LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- 使用ngrok
使用ngrok让微信公众平台通过80端口访问本机 首先声明我是用java-tomcat来研究微信公众平台的. 微信公众平台要成为开发者,需要填写接口配置信息中的“URL”和“Token”这两项(参见: ...
- Memcached原理深度分析详解
Memcached是 danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能.关于这个东 西,相信很多人都用过,本文意在通 ...
- the core or essence of a computer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...
- java 操作数据库
package foo;import java.sql.*; public class JdbcDemo { private static Connection conn; private stati ...
- 【翻译】How To Tango With Django 1.5.4 第一章
1.概览 这本书的目的就是为了给你提供Django实战开发的指导,这本书主要是为学生设计的,它提供了开发并运行第一个web应用程序的详细的指导步骤,并且指导你怎么将它发布到web服务器上. 本书就是为 ...
- Java程序设计的基本原则
Java程序设计的基本原则-1 1.面向对象 这是java编程里面大家公认的第一原则 2.优先使用对象组合而非类继承 3.分层 最典型的三层架构,表现层-->逻辑层-->数据层 表现层功能 ...
- 窗口类型(Widget, Window, Dialog, Desktop, SubWindow等等)
http://doc.qt.io/qt-5/qwidget.html#windowFlags-prop http://doc.qt.io/qt-5/qtwidgets-widgets-windowfl ...
- DiG HOWTO How to use dig to query DNS name servers.
Contents Introduction Understanding the default output What can I discover? How do I … Get a short a ...
- NAT原理与NAT穿越
最近在看东西的时候发现很多网络程序中都需要NAT穿越,特意在此总结一下. 先做一个约定: 内网A中有:A1(192.168.0.8).A2(192.168.0.9)两用户 网关X1(一个NAT设备)有 ...
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication解决办法
mysqlnd是个好东西.不仅可以提高与mysql数据库通信的效率,而且也可以方便的设置一些超时.如,连接超时,查询超时.但是,使用mysqlnd的时候,有个地方需要注意.就是服务端的密码格式不能使用 ...