Skip to content

Commit 63da3d4

Browse files
committed
solved no.3663 which use hash map
1 parent a63a166 commit 63da3d4

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

javascript/index.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,26 +1395,42 @@ Explanation: The robot moves left twice. It ends up two "moves" to the left of t
13951395
/**
13961396
* 3663. Find The Least Frequent Digit
13971397
*
1398-
* 參數為一整數n,找出在其十進位表示中出現頻率最低的數字。如果多個數字的出現頻率相同,則選擇最小的那個數字。
1399-
* 以整數形式傳回所選的數字。
1398+
* 參數為一整數n,找出在其十進位表示中出現頻率最低的數字。如果多個數字的出現頻率相同,則選擇最小的元素。
14001399
* 數字x的出現頻率是指它在n的十進位表示法中的出現次數
14011400
*
14021401
* @param {number} n
14031402
* @return {number}
14041403
*/
14051404
var getLeastFrequentDigit = function(n) {
14061405
/**
1407-
* 找出n中以十進位出現次數最少的數字,若有好幾個數字出現次數一樣,則回傳最小的整數
1406+
* 依據每個數字出現的次數找出出現次數最少的元素,若有好幾個數字出現次數相同,回傳最小的那個元素。
14081407
*
14091408
* solution 1. Hash table
14101409
* solution 2. Array
14111410
*/
1412-
let nSplitToStr = n.toString().split("");
1413-
let map = new Map();
1414-
for(let i = 0;i < nSplitToStr.length;++i) {
1415-
map.has(nSplitToStr[i]) ? map.set(nSplitToStr[i],map.get(nSplitToStr[i])+1) : map.set(nSplitToStr[i],1);
1416-
}
1417-
console.log(map);
1411+
// solution 1.
1412+
// Hash table
1413+
// let nSplitToStr = n.toString().split("");
1414+
// let map = new Map();
1415+
// let minFreq = Infinity,result = 10;
1416+
// for(let i = 0;i < nSplitToStr.length;++i) {
1417+
// map.has(nSplitToStr[i]) ? map.set(nSplitToStr[i],map.get(nSplitToStr[i]) + 1) : map.set(nSplitToStr[i],1);
1418+
// }
1419+
// for(const [key,value] of map){
1420+
// minFreq = Math.min(minFreq, value);
1421+
// }
1422+
// for(const [key,value] of map) {
1423+
// if(value === minFreq){
1424+
// result = Math.min(result,key);
1425+
// }
1426+
// }
1427+
// return result;
1428+
1429+
// solution 2.
1430+
// Array.
1431+
let hash = new Array(10).fill(0);
1432+
let ans = 0,minFreq = 0;
1433+
console.log(hash)
14181434

14191435
};
14201436
let n = 723344511;
@@ -1423,5 +1439,29 @@ Output: 2
14231439
Explanation:
14241440
The least frequent digits in n are 7, 2, and 5; each appears only once.
14251441
*/
1426-
// console.log(getLeastFrequentDigit(n));
1442+
console.log(getLeastFrequentDigit(n));
1443+
1444+
/**
1445+
* 3488. Closest Equal Element Queries
1446+
*
1447+
* 2 array:
1448+
* queries.
1449+
* circular array: nums.
14271450
1451+
* min distance between the element at index queries[i] and any other index j: nums[j] === nums[queries[i]]
1452+
* same size aas queries where answer[i]
1453+
* @param {number[]} nums
1454+
* @param {number[]} queries
1455+
* @return {number[]}
1456+
*/
1457+
var solveQueries = function(nums, queries) {
1458+
1459+
};
1460+
let nums = [1,3,1,4,1,3,2], queries = [0,3,5];
1461+
/*
1462+
Output: [2,-1,3]
1463+
Explanation:
1464+
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.
1465+
Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
1466+
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+
*/

0 commit comments

Comments
 (0)