Skip to content

Commit d24be09

Browse files
committed
add no.3761:
use hash map
1 parent 63da3d4 commit d24be09

2 files changed

Lines changed: 59 additions & 9 deletions

File tree

javascript/LeetCode/Array/3761.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 3761. Minimum Absolute Distance Between Mirror Pairs
3+
*
4+
* mirror pair = indices(i,j)
5+
* reverse(nums[i] === nums[j]) 若數字前面為0,則省略0
6+
* 回傳最小mirror pair絕對距離 abs(i - j),若無,回傳-1
7+
*
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
var minMirrorPairDistance = function(nums) {
12+
/**
13+
* 陣列元素兩個為一組(i,j),每個元素反轉後跟下一個元素比較是否一致。若一致 abs(index i - index j),取最小結果
14+
*/
15+
// 反轉數字
16+
function reverseNum(x){
17+
let y = 0;
18+
while(x > 0){
19+
y = y * 10 + (x % 10);
20+
x = Math.floor(x / 10);
21+
}
22+
return y;
23+
}
24+
25+
let map = new Map();
26+
let ans = nums.length + 1;
27+
for(let i = 0;i < nums.length;i++){
28+
if(map.has(nums[i])){
29+
ans = Math.min(ans,i - map.get(nums[i]));
30+
}
31+
map.set(reverseNum(nums[i]),i);
32+
}
33+
return ans === nums.length + 1 ? -1 : ans;
34+
};
35+
let nums = [12,21,45,33,54]
36+
/*
37+
Output: 1
38+
Explanation:
39+
The mirror pairs are:
40+
(0, 1) since reverse(nums[0]) = reverse(12) = 21 = nums[1], giving an absolute distance abs(0 - 1) = 1.
41+
(2, 4) since reverse(nums[2]) = reverse(45) = 54 = nums[4], giving an absolute distance abs(2 - 4) = 2.
42+
The minimum absolute distance among all pairs is 1.
43+
*/
44+
console.log(minMirrorPairDistance(nums));

javascript/index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ var getLeastFrequentDigit = function(n) {
14081408
* solution 1. Hash table
14091409
* solution 2. Array
14101410
*/
1411+
14111412
// solution 1.
14121413
// Hash table
14131414
// let nSplitToStr = n.toString().split("");
@@ -1416,15 +1417,18 @@ var getLeastFrequentDigit = function(n) {
14161417
// for(let i = 0;i < nSplitToStr.length;++i) {
14171418
// map.has(nSplitToStr[i]) ? map.set(nSplitToStr[i],map.get(nSplitToStr[i]) + 1) : map.set(nSplitToStr[i],1);
14181419
// }
1420+
// 不斷比較minFreq和value哪個最小,因此minFreq值會一直更新
14191421
// for(const [key,value] of map){
1420-
// minFreq = Math.min(minFreq, value);
1422+
// minFreq = Math.min(minFreq,value);
14211423
// }
1422-
// for(const [key,value] of map) {
1423-
// if(value === minFreq){
1424-
// result = Math.min(result,key);
1425-
// }
1424+
// for(const [key,value] of map){
1425+
// // 最小的value = minFreq
1426+
// if(value === minFreq){
1427+
// // 比較result和key哪個最小,key = 元素
1428+
// result = Math.min(result,key);
1429+
// }
14261430
// }
1427-
// return result;
1431+
// return reuslt;
14281432

14291433
// solution 2.
14301434
// Array.
@@ -1439,7 +1443,7 @@ Output: 2
14391443
Explanation:
14401444
The least frequent digits in n are 7, 2, and 5; each appears only once.
14411445
*/
1442-
console.log(getLeastFrequentDigit(n));
1446+
// console.log(getLeastFrequentDigit(n));
14431447

14441448
/**
14451449
* 3488. Closest Equal Element Queries
@@ -1457,11 +1461,13 @@ console.log(getLeastFrequentDigit(n));
14571461
var solveQueries = function(nums, queries) {
14581462

14591463
};
1460-
let nums = [1,3,1,4,1,3,2], queries = [0,3,5];
1464+
// let nums = [1,3,1,4,1,3,2], queries = [0,3,5];
14611465
/*
14621466
Output: [2,-1,3]
14631467
Explanation:
14641468
Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with the same value is 2, and the distance between them is 2.
14651469
Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
14661470
Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).
1467-
*/
1471+
*/
1472+
1473+

0 commit comments

Comments
 (0)