当前位置:网站首页>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)
边栏推荐
- Leetcode skimming ---10
- Are there any other high imitation projects
- Leetcode - 1172 plate stack (Design - list + small top pile + stack))
- Leetcode刷题---1385
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- 一个30岁的测试员无比挣扎的故事,连躺平都是奢望
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- Implementation of "quick start electronic" window dragging
- 多层感知机(PyTorch)
- 波士顿房价预测(TensorFlow2.9实践)
猜你喜欢

Model evaluation and selection

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)

Ut2012 learning notes

Secure in mysql8.0 under Windows_ file_ Priv is null solution

熵值法求权重

Weight decay (pytorch)

Hands on deep learning pytorch version exercise solution - 2.6 probability

Leetcode - 705 design hash set (Design)

Yolov5 creates and trains its own data set to realize mask wearing detection

Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
随机推荐
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Matrix calculation of Neural Network Introduction (pytoch)
What did I read in order to understand the to do list
Raspberry pie 4B installs yolov5 to achieve real-time target detection
High imitation wechat
Ind FHL first week
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Leetcode - the k-th element in 703 data flow (design priority queue)
Hands on deep learning pytorch version exercise solution - 2.6 probability
安装yolov3(Anaconda)
Free online markdown to write a good resume
多层感知机(PyTorch)
神经网络入门之矩阵计算(Pytorch)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
I really want to be a girl. The first step of programming is to wear women's clothes
Ut2014 supplementary learning notes
QT creator uses OpenCV Pro add
Pytorch ADDA code learning notes
Ut2013 learning notes
Yolov5 creates and trains its own data set to realize mask wearing detection