当前位置:网站首页>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
边栏推荐
- [crawler] Charles unknown error
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- How does redis implement multiple zones?
- Dspic33ep clock initialization program
- 【无标题】
- 7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
- POJ 3176 cow bowling (DP | memory search)
- 【Win11 多用户同时登录远程桌面配置方法】
- Advanced technology management - what is the physical, mental and mental strength of managers
- Proof of the thinking of Hanoi Tower problem
猜你喜欢

【Office】Excel中IF函数的8种用法

redis的持久化机制原理
![[crawler] bugs encountered by wasm](/img/29/6782bda4c149b7b2b334238936e211.png)
[crawler] bugs encountered by wasm

How to make your products as expensive as possible

How to protect user privacy without password authentication?

How can China Africa diamond accessory stones be inlaid to be safe and beautiful?

go语言学习笔记-分析第一个程序

【爬虫】wasm遇到的bug

IPv6与IPv4的区别 网信办等三部推进IPv6规模部署

COMSOL -- three-dimensional graphics random drawing -- rotation
随机推荐
【爬虫】charles unknown错误
【Office】Excel中IF函数的8种用法
splunk配置163邮箱告警
MySQL 巨坑:update 更新慎用影响行数做判断!!!
How to make your products as expensive as possible
Empêcher le navigateur de reculer
Redis集群(主从)脑裂及解决方案
COMSOL--三维图形的建立
简单解决redis cluster中从节点读取不了数据(error) MOVED
【上采样方式-OpenCV插值】
【 YOLOv3中Loss部分计算】
Harbor image warehouse construction
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
Advanced technology management - what is the physical, mental and mental strength of managers
《增长黑客》阅读笔记
TSQL – identity column, guid, sequence
What does cross-border e-commerce mean? What do you mainly do? What are the business models?
iTOP-3568开发板NPU使用安装RKNN Toolkit Lite2
redis主从模式
查看多台机器所有进程