Skip to content

Commit c33b0f4

Browse files
committed
add 1848
1 parent 789ae65 commit c33b0f4

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

javascript/LeetCode/Array/1848.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 1848. Minimum Distance to the Target Element
3+
*
4+
* @param {number[]} nums
5+
* @param {number} target
6+
* @param {number} start
7+
* @return {number}
8+
*/
9+
var getMinDistance = function(nums, target, start) {
10+
/**
11+
* nums[i] === target
12+
* 找最小的abs(i - start)
13+
*/
14+
let minDistance = Infinity;
15+
for(let i = 0;i < nums.length;++i) {
16+
if(nums[i] === target){
17+
minDistance = Math.min(minDistance,Math.abs(i - start));
18+
}
19+
}
20+
return minDistance;
21+
};
22+
// let nums = [1,2,3,4,5], target = 5, start = 3
23+
// Output: 1
24+
// Explanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.
25+
let nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 9;
26+
// 0
27+
console.log(getMinDistance(nums,target,start));

javascript/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,3 +1392,30 @@ Explanation: The robot moves left twice. It ends up two "moves" to the left of t
13921392
*/
13931393
// console.log(judgeCircle(moves));
13941394

1395+
/**
1396+
* 3663. Find The Least Frequent Digit
1397+
*
1398+
* @param {number} n
1399+
* @return {number}
1400+
*/
1401+
var getLeastFrequentDigit = function(n) {
1402+
/**
1403+
* 找出n中出現次數最少的數字有幾次,若有好幾個,則回傳最小數字
1404+
*
1405+
* hash table
1406+
*/
1407+
let nSplitToStr = n.toString().split("");
1408+
let map = new Map();
1409+
for(let i = 0;i < nSplitToStr.length;++i) {
1410+
map.has(nSplitToStr[i]) ? map.set(nSplitToStr[i],map.get(nSplitToStr[i])+1) : map.set(nSplitToStr[i],1);
1411+
}
1412+
console.log(map);
1413+
1414+
};
1415+
let n = 723344511;
1416+
/*
1417+
Output: 2
1418+
Explanation:
1419+
The least frequent digits in n are 7, 2, and 5; each appears only once.
1420+
*/
1421+
console.log(getLeastFrequentDigit(n));

0 commit comments

Comments
 (0)