-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_3761.cpp
More file actions
101 lines (90 loc) · 2.66 KB
/
Copy pathleetcode_3761.cpp
File metadata and controls
101 lines (90 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 3761. Minimum Absolute Distance Between Mirror Pairs
// A mirror pair is a pair of indices (i, j) such that:
// 0 <= i < j < nums.length, and
// reverse(nums[i]) == nums[j], where reverse(x) denotes the integer formed by reversing the digits of x. Leading zeros are omitted after reversing, for example reverse(120) = 21.
// Return the minimum absolute distance between the indices of any mirror pair. The absolute distance between indices i and j is abs(i - j).
// If no mirror pair exists, return -1.
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
typedef long long ll;
template <typename T>
void print(T value) {
cout << value << "\n";
}
template <typename T>
void print_vector(vector<T> value) {
for(auto val: value) {
cout << val << " ";
}
print("");
}
class Solution {
public:
string reverseNum(int num) {
string rev;
int rem, q;
int flag = 1;
while (num)
{
rem = num%10;
num = num/10;
if (rem == 0 && flag) {
continue;
} else {
flag = 0;
rev += rem + '0';
}
}
return rev;
}
int findIndex(vector<int>& nums, int target) {
int start = 0, end = nums.size() - 1;
int idx = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] > target) {
idx = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return idx;
}
int minMirrorPairDistance(vector<int>& nums) {
int minDist = INT_MAX;
int n = nums.size();
unordered_map<string, vector<int>>umap;
for(int i = 0; i < n; i++) {
umap[to_string(nums[i])].push_back(i);
}
int idx;
for(int i = 0; i < n; i++) {
int num = nums[i];
string revnum = reverseNum(num);
// print(num);
// print(revnum);
if (umap.find(revnum) != umap.end()) {
vector<int> &indices = umap[revnum];
// print_vector(indices);
int idx = findIndex(indices, i);
if (idx != -1 && indices[idx] > i) {
minDist = min(minDist, abs(indices[idx] - i));
}
}
// print("===========================");
}
if (minDist == INT_MAX) return -1;
return minDist;
}
};
int main() {
vector<int> nums = {12,21,45,33,54};
Solution sol;
int ans = sol.minMirrorPairDistance(nums);
print(ans);
return 0;
}