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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- jeecg的cq查询方式
@RequestMapping(params = "datagrid") public void datagrid(TbStudentDepEntity tbStudentD ...
- 在 mac os 上搭建 git server
前言:之前学习了如何使用 git 后,一直想搭建一个本机搭建一个 git server 的,一开始不知道走了弯路用了 gitosis,折腾了我好几天都没配置好.昨晚查资料发现 gitosis 早就过时 ...
- windows下Qt5.1.0配置android环境搭建 good
1.首先下载好需要配置的软件: 1>Qt 5.1.0 for Android (Windows 32-bit, 716 MB)(Info)下载地址: http://qt-project.org/ ...
- Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good
http://blog.csdn.net/goforwardtostep/article/details/53614830
- 转NodeJS的npm模块版本号 模式解析
npm 中的模块版本都需要遵循 semver 2.0 的语义化版本规则. 版本格式:主版本号.次版本号.修订号,版本号递增规则如下: 主版本号:当你做了不兼容的API 修改, 次版本号:当你做了向下兼 ...
- C/C++链表操作(面试)
1.为了反转这个单链表,我们先让头结点的next域指向结点2,再让结点1的next域指向结点3,最后将结点2的next域指向结点1,就完成了第一次交换,顺序就变成了Header-结点2-结点1-结点3 ...
- 说说怎么写clean code
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...
- EntityFramework更新数据
1.TryUpdateModel 使用很方便,但实际更新数据的过程还是先select,再update.另外发现一个问题,对于input的type类型file的字段,无法使用TryUpdateModel ...
- partial类与[MetadataType(typeof(类名))]有什么区别?
在MVC的Model中,我们可以定义很多与视图相关的元数据,这些元数据对我们开发视图起着相当重要的作用,特别是在数据验证方面.这些元数据一般情况下我们是不会定义在业务实体(或持久化实体)上面,所以很多 ...
- 配置 android环境
1.如上图,下载最新adt-bundle: http://developer.android.com/sdk/index.html 里面集成了 eclipse,SDK,SDK Manager. 2 ...