当前位置:网站首页>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)
边栏推荐
- Judging the connectivity of undirected graphs by the method of similar Union and set search
- Ind FXL first week
- Notes - regular expressions
- Ut2017 learning notes
- Powshell's set location: unable to find a solution to the problem of accepting actual parameters
- A complete answer sheet recognition system
- An open source OA office automation system
- Hands on deep learning pytorch version exercise solution - 3.1 linear regression
- Leetcode - 706 design hash mapping (Design)*
- Data preprocessing - Data Mining 1
猜你喜欢

7、 Data definition language of MySQL (2)

I really want to be a girl. The first step of programming is to wear women's clothes
![[LZY learning notes -dive into deep learning] math preparation 2.5-2.7](/img/57/579357f1a07dbe179f355c4a80ae27.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7

神经网络入门之预备知识(PyTorch)

Configure opencv in QT Creator

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

LeetCode - 715. Range module (TreeSet)*****

ThreadLocal原理及使用场景
![[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation](/img/02/35e73a43c4ad26fee9ecf180593464.jpg)
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation

【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
随机推荐
Weight decay (pytorch)
Softmax 回归(PyTorch)
Judging the connectivity of undirected graphs by the method of similar Union and set search
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
Leetcode刷题---367
Realize an online examination system from zero
Preliminary knowledge of Neural Network Introduction (pytorch)
一步教你溯源【钓鱼邮件】的IP地址
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
Tensorflow—Neural Style Transfer
Policy gradient Method of Deep Reinforcement learning (Part One)
Step 1: teach you to trace the IP address of [phishing email]
I really want to be a girl. The first step of programming is to wear women's clothes
ECMAScript -- "ES6 syntax specification # Day1
Ind yff first week
Install yolov3 (Anaconda)
Timo background management system
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
Ind kwf first week