lintcode - 统计比给定整数小的数的个数(两种方法)
class Solution {
public:
/*
* @param A: An integer array
* @param queries: The query list
* @return: The number of element in the array that are smaller that the given integer
*/
vector<int> countOfSmallerNumber(vector<int> A, vector<int> queries) {
// write your code here
vector<int> ans;
sort(A.begin(), A.end());
for(int i = ; i < queries.size(); ++i){
int tmp = lower_bound(A.begin(), A.end(), queries[i]) - A.begin();
ans.push_back(tmp);
}
return ans;
}
};
二分查找的方法
/*
把A数组各个数字出现的次数放进线段树里,然后每次查询 0到A[i] - 1的个数,就是小于A[i]的个数
*/
class Solution {
public:
/*
* @param A: An integer array
* @param queries: The query list
* @return: The number of element in the array that are smaller that the given integer
*/
//const int inf = -0x3f3f3f3f;
class SegmentTree{
public:
SegmentTree* left;
SegmentTree* right;
int start;
int end;
int sum;
SegmentTree(int start, int end, int sum){
this -> start = start;
this -> end = end;
this -> left = NULL;
this -> right = NULL;
this -> sum = sum;
}
};
int build(SegmentTree* &root, int left, int right, vector<int> &st){
if(left > right) return ;
root -> start = left;
root -> end = right; if(left == right) root -> sum = st[left];
else {
int mid = (left + right) / ;
root -> left = new SegmentTree(left, mid, );
root -> right = new SegmentTree(mid+, right, );
root -> sum = build(root -> left, left, mid, st) + build(root -> right, mid + , right, st);
}
return root -> sum;
}
int query(SegmentTree* &root, int left, int right){
if(root == NULL || left > right) return ;
if(left <= root -> start && root -> end <= right){
return root -> sum;
} int mid = (root -> start + root -> end) / ;
if(left > mid){
return query(root -> right, left, right);
} else if(right <= mid){
return query(root -> left, left, right);
} else {
return query(root -> left, left, mid) + query(root -> right, mid+, right);
} }
vector<int> countOfSmallerNumber(vector<int> A, vector<int> queries) {
// write your code here
vector<int> ans;
if(A.size() == ){
for(int i = ; i < queries.size(); ++i){
ans.push_back();
}
return ans;
}
sort(A.begin(), A.end());
vector<int> st;
st.resize(A[A.size() - ] + , );
for(int i = ; i < A.size(); ++i){
st[A[i]] += ;
}
SegmentTree* root = new SegmentTree(, , );
build(root, , A[A.size() - ], st);
for(int i = ; i < queries.size(); ++i){
int tmp = query(root, , queries[i] - );
ans.push_back(tmp);
}
delete(root);
return ans;
} };
线段树
lintcode - 统计比给定整数小的数的个数(两种方法)的更多相关文章
- lintcode-248-统计比给定整数小的数的个数
248-统计比给定整数小的数的个数 给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返 ...
- Lintcode---统计比给定整数小的数的个数
给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量. ...
- 小程序渲染html的两种方法
一.使用文档自带的原生API rich-text, nodes属性直接绑定需要渲染的html内容即可,文档参见这里:https://developers.weixin.qq.com/miniprog ...
- 求序列A中每个数的左边比它小的数的个数(树状数组)
给定一个有N个正整数的序列A(N<=10^5,A[i]<=10^5),对序列中的每一个数,求出序列中它左边比它小的数的个数. 思路:树状数组的经典应用(裸题) #include <i ...
- C#统计给定的文本中字符出现的次数,使用循环和递归两种方法
前几天看了一个.net程序员面试题目,题目是”统计给定的文本中字符出现的次数,使用循环和递归两种方法“. 下面是我对这个题目的解法: 1.使用循环: /// <summary> /// 使 ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- jmeter--参数化的四种方法
本文转自:http://www.cnblogs.com/imyalost/p/6229355.html 参数化是自动化测试脚本的一种常用技巧.简单来说,参数化的一般用法就是将脚本中的某些输入使用参数来 ...
- c语言实现两数交换的三种方法
实现变量的值互相交换的三种不同方法 方法一:利用第三个变量来实现数值的交换 int tmp; tmp = a; a = b; b = tmp; 此方法直观,简易.不易出错,推荐使用 方法二:利用两个变 ...
- HBase统计表行数(RowCount)的四种方法
背景:对于其他数据存储系统来说,统计表的行数是再基本不过的操作了,一般实现都非常简单:但对于HBase这种key-value存储结构的列式数据库,统计 RowCount 的方法却有好几种不同的花样,并 ...
随机推荐
- Markdown简要规则
We believe that writing is about content, about what you want to say – not about fancy formatting. 我 ...
- 使用线程池优化Echo模型
在上一篇文章中 http://www.cnblogs.com/gosaint/p/8492356.html 我阐述了使用线程为每一个客户端创建一个工作线程来负责任务的执行.但是会存在如 ...
- 详解CSS display:inline-block的应用(转)
详解CSS display:inline-block的应用 阅读目录 基础知识 inline-block的问题 inline-block的应用 总结 本文详细描述了display:inline-b ...
- 使用ServerSocket建立聊天服务器(一)
-------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...
- [转]JQuery 如何选择带有多个class的元素
比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次过滤 ...
- HiveServer2的配置使用
HiveServer2的配置和使用 hive-site.xml配置 hiveserver2的配置 <property> <name>hive.support.concurren ...
- java中是如何解决编码问题的,比如char类型的对象是如何存储的呢?
主题句:每个编码形式将字符从字符集转换为编码数据. 说白了一个代码点就是一个Unicode字符.代码单元就是代码点的集合. 字符视图 要了解字符集标准,您必须能区分三种不同的字符视图: 字符集(字符的 ...
- ZROI2018普转提day1t1
传送门 分析 我们先二分一下最终的平均值mid,然后让序列中的每一个数都减去这个mid,之后用新序列的前缀和建一棵线段树,枚举起点i,然后求出此时在i+L-1~i+R-1范围内的前缀和的最大值,用这个 ...
- Mat_类
Mat_类是对 Mat 类的一个包装,其定义如下: template<typename _Tp> class Mat_ : public Mat { public: //只定义了几 ...
- scala中的注解
scala中很多注解实现java中关键字的用法 @volatile注解标记为易失的:@transient注解将字段标记为瞬态的:@strictfp注解对应strictfp修饰符:@native注解标记 ...