当前位置:网站首页>LeetCode 1200. Minimum absolute difference
LeetCode 1200. Minimum absolute difference
2022-07-05 06:10:00 【Tisfy】
【LetMeFly】1200. Minimum absolute difference
Force button topic link :https://leetcode.cn/problems/minimum-absolute-difference/
Here's an array of integers arr
, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .
Example 1:
Input :arr = [4,2,1,3] Output :[[1,2],[2,3],[3,4]]
Example 2:
Input :arr = [1,3,6,10,15] Output :[[1,3]]
Example 3:
Input :arr = [3,8,-10,23,19,-4,-14,27] Output :[[-14,-10],[19,23],[23,27]]
Tips :
2 <= arr.length <= 10^5
-10^6 <= arr[i] <= 10^6
Method 1 : Sort
The data range of this question is 1 0 5 10^5 105, So it's impossible to O ( n 2 ) O(n^2) O(n2) violence
It's not hard , because After ordering “ The element pair with the smallest absolute difference ” It must be next to
So we can sort directly , Then traverse twice
For the first time, find the minimum value of only difference in absolute value , For the second time, the difference between the absolute values is equal to that of the minimum value pair Just put it in the answer .
- Time complexity O ( n log n ) O(n\log n) O(nlogn), among n n n It's an array arr \textit{arr} arr The length of
- Spatial complexity O ( log n ) O(\log n) O(logn), All are ordered
AC Code
C++
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
sort(arr.begin(), arr.end());
int m = arr[1] - arr[0];
for (int i = 1; i < arr.size(); i++) {
m = min(m, arr[i] - arr[i - 1]);
}
vector<vector<int>> ans;
for (int i = 1; i < arr.size(); i++) {
if (arr[i] - arr[i - 1] == m) {
ans.push_back({
arr[i - 1], arr[i]});
}
}
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125609898
边栏推荐
- QT判断界面当前点击的按钮和当前鼠标坐标
- Simple knapsack, queue and stack with deque
- SQLMAP使用教程(一)
- 1039 Course List for Student
- CF1637E Best Pair
- Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
- Daily question 1984 Minimum difference in student scores
- Liunx starts redis
- Common optimization methods
- 1041 Be Unique
猜你喜欢
Solution to game 10 of the personal field
Individual game 12
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
SQLMAP使用教程(一)
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
实时时钟 (RTC)
传统数据库逐渐“难适应”,云原生数据库脱颖而出
[practical skills] how to do a good job in technical training?
智慧工地“水电能耗在线监测系统”
Some common problems in the assessment of network engineers: WLAN, BGP, switch
随机推荐
数据可视化图表总结(一)
927. Trisection simulation
[rust notes] 16 input and output (Part 1)
Flutter Web 硬件键盘监听
Simply sort out the types of sockets
Over fitting and regularization
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
leetcode-6110:网格图中递增路径的数目
【Rust 笔记】16-输入与输出(上)
[cloud native] record of feign custom configuration of microservices
Appium自动化测试基础 — Appium测试环境搭建总结
6. Logistic model
API related to TCP connection
SPI 详解
leetcode-3:无重复字符的最长子串
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
927. 三等分 模拟
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
1.14 - 流水线