248-统计比给定整数小的数的个数

给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。

注意事项

在做此题前,最好先完成 线段树的构造 and 线段树查询 II 这两道题目。

样例

对于数组 [1,2,7,8,5] ,查询 [1,8,5],返回 [0,4,2]

挑战

否用一下三种方法完成以上题目。

  • 仅用循环方法
  • 分类搜索 和 二进制搜索
  • 构建 线段树 和 搜索

标签

LintCode 版权所有 二分法 线段树

方法一(循环 + 二分查找)

首先对 A 排序,然后以 queries 的每个元素为 target ,用二分查找寻找 target 在 A 的下标,下标值即 A 中小于给定 target 的元素的数量

code

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
int sizeA = A.size(), sizeQ = queries.size();
if (sizeA <= 0 && sizeQ > 0) {
return vector<int>(sizeQ, 0);
}
if (sizeA <= 0 && sizeQ <= 0) {
return vector<int>();
} vector<int> result;
sort(A.begin(), A.end());
for (int target : queries) {
int count = lower_bound(A.begin(), A.end(), target) - A.begin();
result.push_back(count);
}
return result;
}
};

方法二(线段树)

使用线段树,代码结果是 MLE(超出内存),不过还是简单介绍一下

使用线段树,线段树的额外属性 count 为 A 中 start 到 end 中小于 target 的元素个数,之后以 queries 的每个元素为 target,分别建立线段树

线段树自底而上构建,参考lintcode-439-线段树的构造 II

code

/**
* Definition of SegmentTreeNode:
*/
class mySegmentTreeNode {
public:
int start, end, count;
mySegmentTreeNode *left, *right;
mySegmentTreeNode(int start, int end, int max) {
this->start = start;
this->end = end;
this->count = count;
this->left = this->right = NULL;
}
}; 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
int sizeA = A.size(), sizeQ = queries.size();
if (sizeA <= 0 && sizeQ > 0) {
return vector<int>(sizeQ, 0);
}
if (sizeA <= 0 && sizeQ <= 0) {
return vector<int>();
}
vector<int> result;
for (int target : queries) {
mySegmentTreeNode *root = build(0, sizeA - 1, A, target);
result.push_back(root->count);
}
return result;
} mySegmentTreeNode * build(int start, int end, vector<int> &nums, int target) {
// write your code here
if (start > end) {
return nullptr;
}
mySegmentTreeNode *root = new mySegmentTreeNode(start, end, 0);
if (start != end) {
root->left = build(start, (start + end) / 2, nums, target);
root->right = build((start + end) / 2 + 1, end, nums, target);
root->count = root->left->count + root->right->count;
delete root->left; // 减小内存占用
delete root->right;
}
else {
if (target > nums[start]) {
root->count = 1;
}
else {
root->count = 0;
}
}
return root;
}
};

最新文章

  1. Beta Daily Scrum 第四天
  2. Python之路【第十一篇续】前端之CSS补充
  3. 20145304 Java第八周学习报告
  4. 《JAVA开发环境的熟悉》实验报告——20145337
  5. Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置
  6. git服务器搭建
  7. const修饰指针
  8. 求质数算法的N种境界[1] - 试除法和初级筛法
  9. PHP防止SQL注入与几种正则表达式讲解
  10. 添加Appicon的方法
  11. Effective C++(11) 自我赋值(a=a)时会发生什么?
  12. doPost或doGet调用出错(状态代码为405) : HTTP method GET is not supported by this URL
  13. JavaScript中call和apply方法的使用
  14. sed常用操作命令
  15. MVC 中url-pattern配置为&quot;/&quot;和&quot;/*&quot;的区别
  16. 【转】matlab的textscan与textread区别
  17. embsysregview 0.26 无法安装的解决方法。
  18. JavaScript数组的五个迭代方法的简单实例
  19. bzoj千题计划200:bzoj3106: [cqoi2013]棋盘游戏
  20. 【C】——itoa 函数的实现

热门文章

  1. 使用for in 循环数据集
  2. 20190305-leetcode题目有效的括号
  3. sqli-labs(less-11-16)
  4. 适合初学Altium Designer的教学视频
  5. Go Web 使用工具
  6. css3动画性能优化--针对移动端卡顿问题
  7. dtree加载慢的问题
  8. dart 自己写一个简单的文件编码器
  9. java一些封装好的常用算法
  10. 纪中OJ 2019.02.15【NOIP提高组】模拟 B 组 梦回三国 比赛题解(第一个)