当前位置:网站首页>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)
边栏推荐
- Drop out (pytoch)
- Synchronous vs asynchronous
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Model selection for neural network introduction (pytorch)
- 六、MySQL之数据定义语言(一)
- 【毕业季】图匮于丰,防俭于逸;治不忘乱,安不忘危。
- 神经网络入门之矩阵计算(Pytorch)
- Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
- QT creator uses OpenCV Pro add
- Leetcode刷题---1385
猜你喜欢

Leetcode - the k-th element in 703 data flow (design priority queue)

ECMAScript -- "ES6 syntax specification # Day1

丢弃法Dropout(Pytorch)

六、MySQL之数据定义语言(一)

Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression

Raspberry pie 4B installs yolov5 to achieve real-time target detection

Judging the connectivity of undirected graphs by the method of similar Union and set search

ThreadLocal原理及使用场景

深度学习入门之线性代数(PyTorch)

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
随机推荐
High imitation Netease cloud music
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Model selection for neural network introduction (pytorch)
Leetcode skimming ---10
CSDN, I'm coming!
Leetcode刷题---278
Leetcode - 706 design hash mapping (Design)*
Codeup: word replacement
Boston house price forecast (tensorflow2.9 practice)
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Jetson TX2 刷机
High imitation bosom friend manke comic app
A complete answer sheet recognition system
2018 y7000 upgrade hard disk + migrate and upgrade black apple
Jetson TX2 brush machine
Neural Network Fundamentals (1)
Stroke prediction: Bayesian
Matrix calculation of Neural Network Introduction (pytoch)
Leetcode刷题---217