当前位置:网站首页>Leetcode skimming ---1385
Leetcode skimming ---1385
2022-07-03 10:35:00 【Long time no see 0327】
subject : Here are two arrays of integers arr1 ,arr2 And an integer d , Please return the distance between two arrays .[ Distance value ] Number of elements defined as meeting this distance requirement : For elements arr1[i] , There are no elements arr2[j] Satisfy |arr1[i] - arr2[j] <= d .
Input :arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
Output :2
Direction one : simulation
class Solution {
public:
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
int cnt = 0;
for (auto &x: arr1) {
bool ok = true;
for (auto &y: arr2) {
ok &= (abs(x - y) > d);
}
cnt += ok;
}
return cnt;
}
};
Complexity analysis
Time complexity :O(n x m)
Spatial complexity :O(1)
class Solution {
public:
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
sort(arr2.begin(), arr2.end());
int cnt = 0;
for (auto &x: arr1) {
unsigned p = lower_bound(arr2.begin(), arr2.end(), x) - arr2.begin();
bool ok = true;
if (p < arr2.size()) {
ok &= (arr2[p] - x > d);
}
if (p - 1 >= 0 && p - 1 <= arr2.size()) {
ok &= (x - arr2[p - 1] > d);
}
cnt += ok;
}
return cnt;
}
};
Complexity analysis
Time complexity :O(nlogm)
Spatial complexity :O(1)
边栏推荐
- Ind yff first week
- Ut2014 supplementary learning notes
- Jetson TX2 brush machine
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- 2018 y7000 upgrade hard disk + migrate and upgrade black apple
- 六、MySQL之数据定义语言(一)
- Are there any other high imitation projects
- [LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
猜你喜欢
High imitation Netease cloud music
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Implementation of "quick start electronic" window dragging
Out of the box high color background system
多层感知机(PyTorch)
Ut2016 learning notes
A complete answer sheet recognition system
Free online markdown to write a good resume
Tensorflow—Neural Style Transfer
丢弃法Dropout(Pytorch)
随机推荐
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
Ut2011 learning notes
Ut2017 learning notes
A super cool background permission management system
Out of the box high color background system
神经网络入门之模型选择(PyTorch)
2-program logic
Model evaluation and selection
Leetcode刷题---374
重写波士顿房价预测任务(使用飞桨paddlepaddle)
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
2018 y7000 upgrade hard disk + migrate and upgrade black apple
Leetcode刷题---217
20220604 Mathematics: square root of X
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
安装yolov3(Anaconda)
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow