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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- lvs简单配置
负载均衡服务器将会用到两块网卡,eth0为公网地址(此处将局域网ip作为公网地址),IP地址为192.168.1.104,eth0:1,IP地址为192.168.2.1在负载均衡器上添加一个ip别名, ...
- NSQ:分布式的实时消息平台
NSQ是一个基于Go语言的分布式实时消息平台,它基于MIT开源协议发布,代码托管在GitHub,其当前最新版本是0.3.1版.NSQ可用于大规模系统中的实时消息服务,并且每天能够处理数亿级别的消息,其 ...
- textView截取字符串-医生工作台1期
textfield截取字符串 ios7 会崩溃 解: 之前的写法是这样的 正确的写法: 先判断markedTextRange是否为nil, markedTextRange这个属性是啥意思呢 表 ...
- Tomcat7 安装StartSSL证书笔记
1.Tomcat-Native安装 使用StartSSL,Tomcat必须用apr方式启动(apr方式对于静态的内容,比默认的bio效率要高很多倍) Windows下tomcat-native安装 直 ...
- 一个比较轻巧好用的js分页插件,可ajax可url
var pageNav = pageNav || {}; pageNav.fn = null; pageNav.pre = "pre"; pageNav.next = " ...
- 【Demo】微信上墙
先看看微信墙效果图 使用简单说明 关于微信公众号 回复 "上墙",点击授权文章进行授权 回复"#上墙内容" 即可发表上墙消息了 查看微信墙列表,点击这里 原文地 ...
- string strSQL = "Select * From Employees;Select * from Customers";执行两次查询
SqlCommand对象的字符串SQL命令可以做多个,以查询为例,用到SqlDataReader的一些方法,如ExecuteReader(),Read()(一条命令内的移动至下一记录),NextRes ...
- 数据库里any 和 all 的区别
any 是任意一个all 是所有 比如select * from student where 班级='01' and age > all (select age from student whe ...
- NRF51822之pstorage介绍
This information applies to the following SoftDevices: S110, S120, S130, S310 Introduction Persisten ...
- QWidget 键盘事件 焦点(QApplication源码)
在Qt中,键盘事件和QWidget的focus密不可分:一般来说,一个拥有焦点(focus)的QWidget或者grabKeyboard()的QWidget才可以接受键盘事件. 键盘事件派发给谁? 如 ...