当前位置:网站首页>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)
边栏推荐
- Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
- Standard library header file
- Ut2011 learning notes
- [LZY learning notes -dive into deep learning] math preparation 2.1-2.4
- 20220606 Mathematics: fraction to decimal
- Leetcode刷题---278
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- 深度学习入门之线性回归(PyTorch)
- 20220602 Mathematics: Excel table column serial number
- Hands on deep learning pytorch version exercise solution - 3.1 linear regression
猜你喜欢

Neural Network Fundamentals (1)

Hands on deep learning pytorch version exercise solution - 2.6 probability

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

Configure opencv in QT Creator

Tensorflow—Neural Style Transfer

Preliminary knowledge of Neural Network Introduction (pytorch)

ECMAScript--》 ES6语法规范 ## Day1

I really want to be a girl. The first step of programming is to wear women's clothes

Ut2013 learning notes

Leetcode - 705 design hash set (Design)
随机推荐
20220606 Mathematics: fraction to decimal
Leetcode skimming ---202
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
20220603 Mathematics: pow (x, n)
Knowledge map enhancement recommendation based on joint non sampling learning
An open source OA office automation system
conda9.0+py2.7+tensorflow1.8.0
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
mysql5.7安装和配置教程(图文超详细版)
High imitation wechat
Ut2015 learning notes
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
Inverse code of string (Jilin University postgraduate entrance examination question)
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Stroke prediction: Bayesian
Secure in mysql8.0 under Windows_ file_ Priv is null solution
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
Multilayer perceptron (pytorch)
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation