lintcode-248-统计比给定整数小的数的个数
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;
}
};
lintcode-248-统计比给定整数小的数的个数的更多相关文章
- lintcode - 统计比给定整数小的数的个数(两种方法)
class Solution { public: /* * @param A: An integer array * @param queries: The query list * @return: ...
- Lintcode---统计比给定整数小的数的个数
给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量. ...
- 求序列A中每个数的左边比它小的数的个数(树状数组)
给定一个有N个正整数的序列A(N<=10^5,A[i]<=10^5),对序列中的每一个数,求出序列中它左边比它小的数的个数. 思路:树状数组的经典应用(裸题) #include <i ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)
#include <iostream> #include <algorithm> #include <cstring> using namespace std ; ...
- UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)
题目链接:http://acm.upc.edu.cn/problem.php?id=2224 题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A< ...
- hdu 4417 区间内比h小的数 线段树
题意求区间内比h小的数的个数 将所有的询问离线读入之后,按H从小到大排序.然后对于所有的结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,然后就是一个区间和. 2015-07-27:专 ...
- Python实现在给定整数序列中找到和为100的所有数字组合
摘要: 使用Python在给定整数序列中找到和为100的所有数字组合.可以学习贪婪算法及递归技巧. 难度: 初级 问题 给定一个整数序列,要求将这些整数的和尽可能拼成 100. 比如 [17, 1 ...
- 课堂练习:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。
题目 1 给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 2 要求: (1) 写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数.例如 f(12) ...
随机推荐
- CDH部署(以5.7.5为例)
博客园首发,转载请注明出处https://www.cnblogs.com/tzxxh/p/9120020.html 一.准备工作(下面的内容括号内写master的表示仅在master节点执行,all代 ...
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- 扫描算法(SCAN)——磁盘调度管理
原创 上一篇博客写了最短寻道优先算法(SSTF)——磁盘调度管理:http://www.cnblogs.com/chiweiming/p/9073312.html 此篇介绍扫描算法(SCAN)——磁盘 ...
- Go语言中其他数据与字符串类型的转换
1 概述 Go语言是强类型语言,因此总会需要将字符串转成需要的类型.比如整型和字符串转换,字符串和布尔型的转换等.本文就介绍如何完成这些转换,以下是Go语言关于字符串转换的整理说明,主要是与切片类型的 ...
- 20155305乔磊2016-2017-2《Java程序设计》第二周学习总结
20155305乔磊 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 第三章学习了基本类型 整数(short.int.long) 字节(byte) 浮点数(f ...
- sql中的制表符、换行符、回车符,问题
前一阵子用excel导入资源,使用join时发现匹配项为0赶紧用left join看看情况,发现无法链接表. 后来觉得可能是换行的问题,发现还真是,于是就在数据库里删除不想要的字符了,当然,一定要养成 ...
- WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定
原文:WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM ...
- Android Sdk Manager更新
现在Android Sdk Manager无法更新了,什么原因大家都知道,即使使用Goagent效果也不理想. 目前Goagent使用的3.2.2 修改C:\Windows\System32\driv ...
- day 1类 对象 属性 方法
1. 解决吃啤酒鸭的问题 第一种方式(面向过程): 1)养鸭子 2)鸭子长成 3)杀 4)作料 5)烹饪 6)吃 7)卒 第二种方式(面向对象): 1)找个卖啤酒鸭的人 2)给钱 交易 3)吃 4)胖 ...
- 初始CSS模板
/*开始 初始CSS模板 开始*/ body, div, address, blockquote, iframe, ul, ol, dl, dt, dd, li, dl, h1, h2, h3, h4 ...