当前位置:网站首页>leetcode:1200. Minimum absolute difference
leetcode:1200. Minimum absolute difference
2022-07-05 11:36:00 【uncle_ ll】
1200. Minimum absolute difference
source : Power button (LeetCode)
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 <= 1 0 5 10^5 105
- − 1 0 6 -10^6 −106 <= arr[i] <= 1 0 6 10^6 106
solution
- Sort + Traverse : The difference between adjacent elements after sorting is the smallest absolute difference of the element , Traverse after sorting , If the absolute difference is less than the minimum absolute difference , Update absolute difference , Update results ; If it is equal to the minimum absolute difference , Add one result to the result ; If it is larger than, it will not be processed ;
Code implementation
Sort + Traverse
python Realization
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
n = len(arr)
res = []
arr.sort()
min_diff = float('inf')
for i in range(1, n):
if arr[i] - arr[i-1] < min_diff:
res = [[arr[i-1], arr[i]]]
min_diff = arr[i] - arr[i-1]
elif arr[i] - arr[i-1] == min_diff:
res.append([arr[i-1], arr[i]])
return res
c++ Realization
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
vector<vector<int>> res;
sort(arr.begin(), arr.end());
int min_diff = INT_MAX;
for (int i=1; i<n; i++) {
if (arr[i]-arr[i-1] < min_diff){
res = {
{
arr[i-1], arr[i]}};
min_diff = arr[i] - arr[i-1];
}
else if (arr[i]-arr[i-1] == min_diff)
res.push_back({
arr[i-1], arr[i]});
}
return res;
}
};
Complexity analysis
- Time complexity : O ( N l o g N ) O(NlogN) O(NlogN) Sorting time
- Spatial complexity : O ( l o g N ) O(logN) O(logN) Space required for sorting
边栏推荐
- redis的持久化机制原理
- I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
- 2048游戏逻辑
- DDoS attack principle, the phenomenon of being attacked by DDoS
- pytorch-多层感知机MLP
- Solve the grpc connection problem. Dial succeeds with transientfailure
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- 百问百答第45期:应用性能探针监测原理-node JS 探针
- splunk配置163邮箱告警
- 解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
猜你喜欢

Oneforall installation and use

MySQL 巨坑:update 更新慎用影响行数做判断!!!

pytorch-多层感知机MLP

How to make your products as expensive as possible

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

谜语1

CDGA|数据治理不得不坚持的六个原则

《增长黑客》阅读笔记
![[office] eight usages of if function in Excel](/img/ce/ea481ab947b25937a28ab5540ce323.png)
[office] eight usages of if function in Excel

Pytorch training process was interrupted
随机推荐
Idea set the number of open file windows
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
spark调优(一):从hql转向代码
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
871. Minimum Number of Refueling Stops
pytorch-softmax回归
边缘计算如何与物联网结合在一起?
COMSOL--三维随便画--扫掠
SLAM 01. Modeling of human recognition Environment & path
【上采样方式-OpenCV插值】
技术分享 | 常见接口协议解析
阻止瀏覽器後退操作
Go language learning notes - first acquaintance with go language
c#操作xml文件
Startup process of uboot:
Harbor镜像仓库搭建
管理多个Instagram帐户防关联小技巧大分享
Guys, I tested three threads to write to three MySQL tables at the same time. Each thread writes 100000 pieces of data respectively, using F
龙蜥社区第九次运营委员会会议顺利召开
Shell script file traversal STR to array string splicing