当前位置:网站首页>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)
边栏推荐
- 实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- Ind FHL first week
- Leetcode刷题---263
- Yolov5 creates and trains its own data set to realize mask wearing detection
- Ind kwf first week
- Introduction to deep learning linear algebra (pytorch)
- 20220605 Mathematics: divide two numbers
- The imitation of jd.com e-commerce project is coming
- 一个30岁的测试员无比挣扎的故事,连躺平都是奢望
猜你喜欢

Timo background management system

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

mysql5.7安装和配置教程(图文超详细版)

Model selection for neural network introduction (pytorch)

Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing

Ut2016 learning notes

Policy gradient Method of Deep Reinforcement learning (Part One)

Data preprocessing - Data Mining 1

Install yolov3 (Anaconda)

Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
随机推荐
Notes - regular expressions
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Data preprocessing - Data Mining 1
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
20220603 Mathematics: pow (x, n)
Softmax regression (pytorch)
Leetcode skimming ---10
Standard library header file
ECMAScript--》 ES6语法规范 ## Day1
安装yolov3(Anaconda)
波士顿房价预测(TensorFlow2.9实践)
2018 Lenovo y7000 black apple external display scheme
conda9.0+py2.7+tensorflow1.8.0
Ut2015 learning notes
Inverse code of string (Jilin University postgraduate entrance examination question)
Leetcode - 705 design hash set (Design)
20220607 others: sum of two integers
Leetcode刷题---283