当前位置:网站首页>(coordinate dynamic programming) lintcode medium 248 · count the number of numbers smaller than a given integer
(coordinate dynamic programming) lintcode medium 248 · count the number of numbers smaller than a given integer
2022-04-21 11:00:00 【White boy&】
subject
describe
Given an array of integers ( Subscript by 0 To n-1, among n Represents the size of the array , The range of values is 0 To 10000), And one. Query list . For every query , I'll give you an integer , Please return the number of elements in the array less than the given integer .
Examples
Examples 1:
Input : array =[1,2,7,8,5] queries =[1,8,5]
Output :[0,4,2]
Examples 2:
Input : array =[3,4,5,8] queries =[2,4]
Output :[0,1]
analysis
The maximum number in the given array is 10000, Then we can use the hash table to count the number of each value , We think if the number to be compared is greater than 10000, Then it is larger than all the numbers in a given array , Here we can use coordinate dynamic programming
Coordinate dynamic programming , The hash table counts the number of values in a given array , state f[i] i Represents the current number to be calculated f[i] The value of represents the ratio i The number of small numbers Transfer equation f[i]=f[i-1]+hash[i-1] For example, we calculate the ratio 10 The number of small numbers Just know better than 9 Small number plus 9 The number of
The maximum value in the given array 10000 therefore f[i] When i>10000 Its value must be f[10001] It can also be understood as larger than all values in a given array
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> countOfSmallerNumber(vector<int> &a, vector<int> &queries) {
vector<int> hash_map(10001,0); // Number of values
for(int i=0;i<a.size();i++)
{
hash_map[a[i]]+=1;
}
vector<int> f(10002,0); // state Represents the number of values smaller than the current value
for(int i=1;i<10002;i++)
f[i]=f[i-1]+hash_map[i-1]; // Last state + The number of previous values
vector<int> ans;
for(int i=0;i<queries.size();i++)
{
int val=queries[i];
if(val>10001)
{
ans.push_back(f[10001]);
continue;
}
ans.push_back(f[val]);
}
return ans;
}
};
int main (void)
{
vector<int> a={
1,2,7,8,5};
vector<int> q={
1,8,5};
vector<int> ans;
Solution s;
ans=s.countOfSmallerNumber(a,q);
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
return 0;
}
summary
It was originally a question of practicing line segment tree , The sudden whim uses coordinate dynamic programming , It is found that this method is the most efficient for this problem
版权声明
本文为[White boy&]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211054070321.html
边栏推荐
- GO 使用channel进行同步 (缓冲channel)
- 【生活中的逻辑谬误】对人不对事和两难陷阱
- js---call,apply,bind
- GDI+中TGPImage从流中加载图像
- MySQL深入学习(三十):数据库的设计规范
- [TIANTI race] l3-005 dustbin distribution (heap optimized version Dijkstra)
- 电机控制-速度环设计
- MySQL modifies the maximum number of connections
- Recursive function C language question type
- [nonlinear control theory] 1_ Lyapunov direct method
猜你喜欢

Suffix array application

犀牛软件插件-rhino插件-visual studio-创建你的第一个插件

Openshift 4 - improve client access API server security

【生活中的逻辑谬误】对人不对事和两难陷阱

AcWing 1737. Transmission (classified discussion)
Construction of go environment

hyperf 执行sql语句,参数会有两个单引号

Convbert: improving Bert with span based dynamic revolution
![[非线性控制理论]1_Lyapunov直接方法](/img/ad/68bceb288d40ae98b60dbb83e0b91d.png)
[非线性控制理论]1_Lyapunov直接方法

pgpool-II 4.3 中文手册 - 入门教程
随机推荐
Matlab GUI --- complex drawing mode (animation demonstration)
后缀数组模版代码解析
Code analysis of AC automata template
后缀数组应用
Uniapp developing official account number one time subscription message
AC自动机
Construction of go environment
TypeError: The view function did not return a valid response. The function either returned none
桶排序 ← C语言实现
AcWing 1749. Block billboard II (classified discussion + enumeration)
OpenShift 4 - 提升客户端访问 API Server 安全
Matlab GUI application - lottery (animation demonstration)
O2oa secondary development - use the open source platform to build a complete OA (3) - development enterprise reimbursement approval
Convbert: improving Bert with span based dynamic revolution
Map与JsonObject区别
第一个GO程序
uniapp 开发公众号一次性订阅消息
[ladder race] is l3-010 a complete binary search tree
万元礼品奖池 玩转「Lighthouse」有奖征文来袭
【openEuler】Failed to download metadata for repo ‘EPOL‘: Cannot d