当前位置:网站首页>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
边栏推荐
- 7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
- Install esxi 6.0 interactively
- 中非 钻石副石怎么镶嵌,才能既安全又好看?
- 解决grpc连接问题Dial成功状态为TransientFailure
- Manage multiple instagram accounts and share anti Association tips
- [LeetCode] Wildcard Matching 外卡匹配
- View all processes of multiple machines
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- Cron expression (seven subexpressions)
- POJ 3176-Cow Bowling(DP||记忆化搜索)
猜你喜欢
Go language learning notes - analyze the first program
Redis集群(主从)脑裂及解决方案
中非 钻石副石怎么镶嵌,才能既安全又好看?
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
iTOP-3568开发板NPU使用安装RKNN Toolkit Lite2
AutoCAD -- mask command, how to use CAD to locally enlarge drawings
【云原生 | Kubernetes篇】Ingress案例实战(十三)
[crawler] Charles unknown error
Harbor镜像仓库搭建
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
随机推荐
comsol--三维图形随便画----回转
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
1个插件搞定网页中的广告
管理多个Instagram帐户防关联小技巧大分享
中非 钻石副石怎么镶嵌,才能既安全又好看?
简单解决redis cluster中从节点读取不了数据(error) MOVED
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
idea设置打开文件窗口个数
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
Solve the problem of slow access to foreign public static resources
How to make your products as expensive as possible
Technology sharing | common interface protocol analysis
splunk配置163邮箱告警
How can edge computing be combined with the Internet of things?
871. Minimum Number of Refueling Stops
ACID事务理论
I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
ZCMU--1390: 队列问题(1)
【使用TensorRT通过ONNX部署Pytorch项目】