当前位置:网站首页>leetcode:1200. 最小绝对差
leetcode:1200. 最小绝对差
2022-07-05 09:43:00 【uncle_ll】
1200. 最小绝对差
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/minimum-absolute-difference
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
示例 1:
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
示例 2:
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
示例 3:
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]
提示:
- 2 <= arr.length <= 1 0 5 10^5 105
- − 1 0 6 -10^6 −106 <= arr[i] <= 1 0 6 10^6 106
解法
- 排序+遍历: 排序后相邻元素的差就是该元素的最小的绝对差,排序之后进行遍历,如果绝对差小于最小绝对差,更新绝对差,更新结果;如果等于最小绝对差,结果中增加一个结果即可;大于的话不进行处理;
代码实现
排序+遍历
python实现
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++实现
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;
}
};
复杂度分析
- 时间复杂度: O ( N l o g N ) O(NlogN) O(NlogN) 排序耗时
- 空间复杂度: O ( l o g N ) O(logN) O(logN) 排序需要的空间
边栏推荐
- 把欧拉的创新带向世界 SUSE 要做那个引路人
- [system design] index monitoring and alarm system
- [200 opencv routines] 219 Add digital watermark (blind watermark)
- CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
- 剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...
- 卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
- The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
- Energy momentum: how to achieve carbon neutralization in the power industry?
- QT VT100 parser
- Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
猜你喜欢
Data visualization platform based on template configuration
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
Application of data modeling based on wide table
How to get the STW (pause) time of GC (garbage collector)?
Charm of code language
能源势动:电力行业的碳中和该如何实现?
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
Apache DolphinScheduler 入门(一篇就够了)
随机推荐
RMS to EAP is simply implemented through mqtt
Jupiter notebook shortcut key
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
MySQL字符类型学习笔记
Cent7 Oracle database installation error
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
[200 opencv routines] 219 Add digital watermark (blind watermark)
Kotlin compose and native nesting
Comment obtenir le temps STW du GC (collecteur d'ordures)?
钉钉、企微、飞书学会赚钱了吗?
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
MySQL数字类型学习笔记
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
How to use sqlcipher tool to decrypt encrypted database under Windows system
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
程序员如何活成自己喜欢的模样?
isEmpty 和 isBlank 的用法区别
Tdengine connector goes online Google Data Studio app store