当前位置:网站首页>1200.Minimum Absolute Difference
1200.Minimum Absolute Difference
2022-07-07 03:26:00 【SUNNY_ CHANGQI】
The description of the problem
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
return a list of parirs in ascending order (with respect to pairs), each pair [a, b] follows.
1. a, b are from arr
2. a < b
3. b - a equals to the minimum absolute difference of any two elements in arr.
an example
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-absolute-difference
The solution 1
The intuition
Leverage the loop nesting to traveral all the diffVal and store them into a map and log the minimum difference value. In the end, sort out the return nesting loop.
The corresponding codes
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <map>
using namespace std;
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>> res;
map<int, vector<vector<int>>> m;
int minDiff = INT_MAX;
for (int i = 0; i < arr.size(); i++) {
for (int j = i + 1; j < arr.size(); j++) {
int diff = abs(arr[i] - arr[j]);
minDiff = min(minDiff, diff);
vector<int> t;
if (arr[i] < arr[j]) {
t.push_back(arr[i]);
t.push_back(arr[j]);
} else {
t.push_back(arr[j]);
t.push_back(arr[i]);
}
m[diff].push_back(t);
}
}
res = m[minDiff];
// sort res by first element
sort(res.begin(), res.end(), [](vector<int>& a, vector<int>& b) {
return a[0] < b[0];
});
return res;
}
};
int main()
{
Solution s;
vector<int> arr = {
4, 2, 1, 3};
vector<vector<int>> res = s.minimumAbsDifference(arr);
for (auto& v : res) {
for (auto& i : v) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
The result

The solution 2
firstly sort out the elements in arr in asecending order. traversal the arr to find the minimum difference value. Lastly, traversal the second time.
The codes
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <map>
using namespace std;
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>> res;
int minDiff = INT_MAX;
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size() - 1; i++) {
int diff = arr[i + 1] - arr[i];
minDiff = min(minDiff, diff);
}
for (auto it = arr.begin(); it != arr.end() - 1; it++) {
if (abs(*(it + 1) - *it) == minDiff) {
res.push_back({
*it, *(it + 1)});
}
}
return res;
}
};
int main()
{
Solution s;
vector<int> arr = {
4, 2, 1, 3};
vector<vector<int>> res = s.minimumAbsDifference(arr);
for (auto& v : res) {
for (auto& i : v) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
The corresponding results

The more optimized solution
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <map>
using namespace std;
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>> res;
int minDiff = INT_MAX;
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size() - 1; i++) {
int diff = arr[i + 1] - arr[i];
if (diff < minDiff) {
minDiff = diff;
res.clear();
res.push_back({
arr[i], arr[i + 1]});
} else if (diff == minDiff) {
res.push_back({
arr[i], arr[i + 1]});
}
}
return res;
}
};
int main()
{
Solution s;
vector<int> arr = {
4, 2, 1, 3};
vector<vector<int>> res = s.minimumAbsDifference(arr);
for (auto& v : res) {
for (auto& i : v) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
The corresponding result

边栏推荐
- Experience design details
- Set WiFi automatic connection for raspberry pie
- CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
- Matlab Error (Matrix dimensions must agree)
- Simple bubble sort
- How to replace the backbone of the model
- 25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
- Variables, process control and cursors (MySQL)
- Jerry's phonebook acquisition [chapter]
- leetcode
猜你喜欢

ubuntu20安裝redisjson記錄

Household appliance industry under the "retail is king": what is the industry consensus?

VHDL实现任意大小矩阵乘法运算
![[cpk-ra6m4 development board environment construction based on RT thread studio]](/img/08/9a847c73d6da6fc74d84af56897752.png)
[cpk-ra6m4 development board environment construction based on RT thread studio]

小程序能运行在自有App中,且实现直播和连麦?

Appx代码签名指南

Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application

变量、流程控制与游标(MySQL)

Stored procedures and functions (MySQL)

Flutter3.0, the applet is not only run across mobile applications
随机推荐
“去虚向实”大潮下,百度智能云向实而生
HDU 4337 King Arthur&#39; S Knights it outputs a Hamiltonian circuit
Set static IP for raspberry pie
HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
CMB's written test - quantitative relationship
Graphical tools package yolov5 and generate executable files exe
Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
小程序能运行在自有App中,且实现直播和连麦?
Matlab Error (Matrix dimensions must agree)
Lab1 configuration script
注意力机制原理
SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
枚举通用接口&枚举使用规范
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
密码学系列之:在线证书状态协议OCSP详解
Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
Cryptography series: detailed explanation of online certificate status protocol OCSP
mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
Jerry's FM mode mono or stereo selection setting [chapter]