当前位置:网站首页>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 cluster中从节点读取不了数据(error) MOVED
- Is it difficult to apply for a job after graduation? "Hundreds of days and tens of millions" online recruitment activities to solve your problems
- Question and answer 45: application of performance probe monitoring principle node JS probe
- 871. Minimum Number of Refueling Stops
- Zcmu--1390: queue problem (1)
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
- pytorch训练进程被中断了
- ACID事务理论
- spark调优(一):从hql转向代码
- 网络五连鞭
猜你喜欢

1个插件搞定网页中的广告

Evolution of multi-objective sorting model for classified tab commodity flow

redis主从中的Master自动选举之Sentinel哨兵机制

The ninth Operation Committee meeting of dragon lizard community was successfully held

中非 钻石副石怎么镶嵌,才能既安全又好看?

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
![[crawler] bugs encountered by wasm](/img/29/6782bda4c149b7b2b334238936e211.png)
[crawler] bugs encountered by wasm

【使用TensorRT通过ONNX部署Pytorch项目】

liunx禁ping 详解traceroute的不同用法

13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
随机推荐
【Win11 多用户同时登录远程桌面配置方法】
SLAM 01. Modeling of human recognition Environment & path
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
TSQL – identity column, guid, sequence
[leetcode] wild card matching
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
Empêcher le navigateur de reculer
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
简单解决redis cluster中从节点读取不了数据(error) MOVED
Unity xlua monoproxy mono proxy class
CDGA|数据治理不得不坚持的六个原则
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
11.(地图数据篇)OSM数据如何下载使用
Redis集群(主从)脑裂及解决方案
【Office】Excel中IF函数的8种用法
How to make your products as expensive as possible
我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
12. (map data) cesium city building map
石油化工企业安全生产智能化管控系统平台建设思考和建议
1个插件搞定网页中的广告