Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions javascript/LeetCode/Array/1200.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 1200. Minimum Absolute Difference
*
* 給一個無重複元素的數字陣列,找出兩個元素間相差最小的元素並將它們歸類為一組,回傳陣列為二維陣列,必須遞增方式排序[a,b]
* a < b
* b - a = 每組最小相等值
*
* @param {number[]} arr
* @return {number[][]}
*/
var minimumAbsDifference = function(arr) {
arr.sort((a,b) => a - b);
let min = Infinity;
let res = [];
for(let i = 1;i < arr.length;++i) {
let diff = arr[i] - arr[i-1];
if(diff < min){
min = diff;
res = [i - 1];
}else if(diff === min){
res.push(i - 1);
}
}
return res.map(i => [arr[i], arr[i + 1]]);
};
// let nums = [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.
let nums = [-17,46,63,81,-101,-91,121,-2,112,-15,-65,-96,6,-139];
// [[-17,15]]
console.log(minimumAbsDifference(nums));
38 changes: 38 additions & 0 deletions javascript/LeetCode/Array/2974.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 2974. Minimum Number Game
*
* nums.length = even(偶數),每一輪選手(Alice and Bob )皆須:
* 第一步,Alice先從陣列中移除最小的元素一次,之後換Bob做同樣的事情(移除第一小&第二小的元素)
* 第二步,Bob將移除的元素加進空陣列arr中,再換Alice做同樣的事情(添加第二小元素&第一小元素)
* 重複上述步驟直到nums變成空陣列為止,回傳最終的arr
*
* @param {number[]} nums
* @return {number[]} arr
*/
var numberGame = function(nums) {
let arr = [];
nums.sort((a,b) => a - b);
// 兩個兩個比較並交換
for(let i = 0;i < nums.length;i+=2) {
// 有可能有同樣的元素,所以得<=
if(nums[i] <= nums[i+1]){
let temp = nums[i];
arr[i] = nums[i + 1];
arr[i + 1] = temp;
}
}
return arr;

// solution 2.
// nums.sort((a,b) => a - b);
// // 兩個兩個比較並交換
// for(let i = 0;i < nums.length;i+=2) {
// [nums[i],nums[i+1]] = [nums[i+1],nums[i]]
// }
// return nums;
};
let nums = [5,4,2,3];
// Output: [3,2,5,4]
// Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
// At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
console.log(numberGame(nums));
34 changes: 34 additions & 0 deletions javascript/LeetCode/Array/3314.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 3314. Construct the Minimum Bitwise Array I
*
* prime number = 1 & itself.
* ans[i] 與 ans[i] + 1 的位元或運算等於 nums[i],即 ans[i] OR (ans[i] + 1) == nums[i]。
*
* @param {number[]} nums
* @return {number[]}
*/
var minBitwiseArray = function(nums) {
let ans = new Array(nums.length);

for(let i = 0;i < nums.length;++i) {
let maybe = -1;
for(let j = 1;j < nums[i];++j) {
if((j | (j + 1)) === nums[i]){
maybe = j;
break;
}
}
ans[i] = maybe;
}
return ans;
};
let nums = [2,3,5,7];
/**
* [-1,1,4,3]
Explanation:
For i = 0, as there is no value for ans[0] that satisfies ans[0] OR (ans[0] + 1) = 2, so ans[0] = -1.
For i = 1, the smallest ans[1] that satisfies ans[1] OR (ans[1] + 1) = 3 is 1, because 1 OR (1 + 1) = 3.
For i = 2, the smallest ans[2] that satisfies ans[2] OR (ans[2] + 1) = 5 is 4, because 4 OR (4 + 1) = 5.
For i = 3, the smallest ans[3] that satisfies ans[3] OR (ans[3] + 1) = 7 is 3, because 3 OR (3 + 1) = 7.
*/
console.log(minBitwiseArray(nums));
30 changes: 30 additions & 0 deletions javascript/LeetCode/Array/3379.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 3379. Transformed Array
*
* nums is circular,所以不管往左或往右,都有可能回到原點
*
* nums[i] > 0 => index i往右移nums[i]步至nums[i]的位置,並將result[i]設成index i值
* nums[i] < 0 => index i往左移nums[i]步至abs(nums[i])的位置,並將result[i]設成index i值
* nums[i] === 0 => result[i] = nums[i]
*
* @param {number[]} nums
* @return {number[]}
*/
var constructTransformedArray = function(nums) {
let res = [];
for(let i = 0;i < nums.length;++i) {
// 計算要往左或右,在哪個index
res[i] = nums[((i + nums[i]) % nums.length + nums.length) % nums.length];
}
return res;
};
let nums = [3,-2,1,1];
/*
Output: [1,1,1,3]
Explanation:
For nums[0] that is equal to 3, If we move 3 steps to right, we reach nums[3]. So result[0] should be 1.
For nums[1] that is equal to -2, If we move 2 steps to left, we reach nums[3]. So result[1] should be 1.
For nums[2] that is equal to 1, If we move 1 step to right, we reach nums[3]. So result[2] should be 1.
For nums[3] that is equal to 1, If we move 1 step to right, we reach nums[0]. So result[3] should be 3.
*/
console.log(constructTransformedArray(nums));
28 changes: 28 additions & 0 deletions javascript/LeetCode/Array/3634.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 3634. Minimum Removals to Balance Array
*
* balanced條件 = 最大元素 <= 最小元素 * k值
* 可移除任一元素,回傳要移除幾個元素才能達成balanced這條件
*
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minRemoval = function(nums, k) {
nums.sort((a,b) => a - b);
let i = 0;
let count = 0;
for(let j = 0;j < nums.length;++j) {
// 2 pointers.i & j
while(nums[j] > nums[i] * k){
i++;
}
count = Math.max(count, j - i + 1);
}
return nums.length - count;
};
let nums = [1,6,2,9], k = 3;
// 2
// Remove nums[0] = 1 and nums[3] = 9 to get nums = [6, 2].
// Now max = 6, min = 2 and max <= min * k as 6 <= 2 * 3. Thus, the answer is 2.
console.log(minRemoval(nums,k));
29 changes: 29 additions & 0 deletions javascript/LeetCode/Array/3736.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 3736. Minimum Moves to Equal Array Elements III
*
* 參數為數值陣列,在一次操作中可將任一元素+1
* 回傳須移動幾次才能將所有元素都變得一樣
*
* @param {number[]} nums
* @return {number}
*/
var minMoves = function(nums) {
// 要先知道nums中最大值是多少,這樣就能知道其他元素跟最大值差多少
let count = 0;
let maxEle = Math.max(...nums);
for(let i = 0;i < nums.length;++i) {
count += Math.abs(maxEle - nums[i]);
}
return count;
};
let nums = [2,1,3];
/*
Output: 3
Explanation:
To make all elements equal:
Increase nums[0] = 2 by 1 to make it 3.
Increase nums[1] = 1 by 1 to make it 2.
Increase nums[1] = 2 by 1 to make it 3.
Now, all elements of nums are equal to 3. The minimum total moves is 3.
*/
console.log(minMoves(nums))
92 changes: 92 additions & 0 deletions javascript/LeetCode/Array/3838.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 3838. Weighted Word Mapping
*
* 0 = z;1 = y;2 = x....;25 = a,26個字母倒著
* 將每個元素字母重量加總後%26得出的A值,將A值與26個字母倒著的value做比對,取對應的key並以字串回傳
*
* @param {string[]} words
* @param {number[]} weights
* @return {string}
*/
var mapWordWeights = function(words, weights) {
// 解法1,使用map
// 可能遇到的狀況:key(字母)、value(數字)重複出現
let alp = generateAlphabet();
let sumWeights = [];

for (const element of words) {
let countLen = 0;
for(let i = 0;i < element.length;++i) {
countLen += weights[element.charCodeAt(i) - 'a'.charCodeAt()]
}
// modulo 26
sumWeights.push(countLen % 26);
}
// 方法1
// const result = sumWeights.map(num => {
// const found = [...alp.entries()]
// .find(([key, value]) => value === num);
// return found ? found[0] : null;
// });

// 方法2 反向 Map
const reverseMap = new Map(
[...alp.entries()].map(([k, v]) => [v, k])
);

const result = sumWeights.map(num => reverseMap.get(num) ?? null);

return result.join("")


/**
* 產生26個英文字母
* a = 26,b = 25 ....
* @returns obj
*/
function generateAlphabet(){
let start = "a";
let end = "z";
let alp = new Map();
let range = 25;
let i = start.charCodeAt(0), j = end.charCodeAt(0);
for (; i <= j; ++i) {
alp.set(String.fromCharCode(i),range--);
}
return alp;
}

};

/**
* 解法2。沒有另寫涵式產生26個英文字母
* 此法較快
*
* @param {*} words
* @param {*} weights
* @returns
*/
var mapWordWeights2 = function(words, weights) {
let sumWeights = [];

for (const element of words) {
let countLen = 0;
for(let i = 0;i < element.length;++i) {
countLen += weights[element.charCodeAt(i) - 'a'.charCodeAt()]
}
// String.fromCharCode(ascii code) => ascii code to char.
sumWeights.push(String.fromCharCode('z'.charCodeAt() - countLen % 26));
}
return sumWeights.join("")
}
let list = ["abcd","def","xyz"], weights = [5,3,12,14,1,2,3,2,10,6,6,9,7,8,7,10,8,9,6,9,9,8,3,7,7,2];
/*
Output: "rij"
Explanation:
The weight of "abcd" is 5 + 3 + 12 + 14 = 34. The result modulo 26 is 34 % 26 = 8, which maps to 'r'.
The weight of "def" is 14 + 1 + 2 = 17. The result modulo 26 is 17 % 26 = 17, which maps to 'i'.
The weight of "xyz" is 7 + 7 + 2 = 16. The result modulo 26 is 16 % 26 = 16, which maps to 'j'.
Thus, the string formed by concatenating the mapped characters is "rij".
*/
console.log(mapWordWeights(list,weights));
console.log(mapWordWeights2(list,weights));
37 changes: 37 additions & 0 deletions javascript/LeetCode/String/345.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 345. Reverse Vowels of a String
*
* 找出所有母音(不分大小寫),其餘子音維持原位,唯獨反轉母音
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
let vowels = 'aeiouAEIOU';
let splitS = s.split("");
let i = 0, j = s.length - 1;
while(i < j){
while(i < j && vowels.indexOf(splitS[i]) == -1){
i++;
}
while(i < j && vowels.indexOf(splitS[j]) == -1) {
j--;
}
// 交換母音
let chars = splitS[i];
splitS[i] = splitS[j];
splitS[j] = chars;

// 2 pointers
i++;
j--;
}
return splitS.join("");
};
let s = "IceCreAm";
/**
* Output: "AceCreIm"
* Explanation:
* The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".
*
*/
console.log(reverseVowels(s));
25 changes: 25 additions & 0 deletions javascript/LeetCode/String/3856.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 3856. Trim Trailing Vowels
*
* 移除s中後半部的母音
*
* @param {string} s
* @return {string}
*/
var trimTrailingVowels = function(s) {
/**
* 從後面開始檢查每個字元是否是母音,若是,則移除並繼續往前找直到非母音為止
*/

let splitS = s.split("").reverse();
let i = 0;
while(splitS[i] === 'a' || splitS[i] === 'e' || splitS[i] === 'i' || splitS[i] === 'o' || splitS[i] === 'u'){
splitS.shift();
}
return splitS.reverse().join("");
};
// let s = "idea";
//"id"
// let s = "day";
let s = "aeiou";
console.log(trimTrailingVowels(s));
Loading
Loading