Skip to content

Commit 23b98f4

Browse files
authored
Merge pull request #150 from clingoram/mavis
解題
2 parents 5f8df17 + eb2f206 commit 23b98f4

36 files changed

Lines changed: 1101 additions & 12 deletions

javascript/LeetCode/Array/1534.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1534. Count Good Triplets
3+
*
4+
* good triplets:
5+
* 0 <= i < j < k < arr.length
6+
* |arr[i] - arr[j]| <= a
7+
* |arr[j] - arr[k]| <= b
8+
* |arr[i] - arr[k]| <= c
9+
*
10+
* @param {number[]} arr
11+
* @param {number} a
12+
* @param {number} b
13+
* @param {number} c
14+
* @return {number}
15+
*/
16+
var countGoodTriplets = function(arr, a, b, c) {
17+
let ans = 0;
18+
for(let i = 0;i < arr.length;i++) {
19+
for(let j = i+1;j < arr.length;j++) {
20+
for(let k = j+1;k < arr.length;k++) {
21+
if(Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c){
22+
ans++;
23+
}
24+
}
25+
}
26+
}
27+
return ans;
28+
};
29+
let arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3;
30+
// Output: 4
31+
// Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
32+
console.log(countGoodTriplets(arr,a,b,c));

javascript/LeetCode/Array/1913.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,20 @@ var maxProductDifference = function (nums) {
4848
// return (max[0] * max[1]) - (min[0] * min[1]);
4949

5050
// solution 2.Runtime took 100 ms
51-
nums.sort((a, b) => a - b);
52-
let length = nums.length;
53-
return (nums[length - 1] * nums[length - 2]) - (nums[0] * nums[1]);
51+
// nums.sort((a, b) => a - b);
52+
// let length = nums.length;
53+
// return (nums[length - 1] * nums[length - 2]) - (nums[0] * nums[1]);
54+
55+
56+
// solution 3.Update in 2025/10/2.Runtime took 73 ms.
57+
// pair a = 兩個最大數; pair b = 兩個最小數
58+
// sort 由大至小
59+
nums.sort((a,b) => b - a);
60+
// 取得第一組兩個最大數並相乘
61+
let pairA = nums.slice(0,2).reduce((acc, curr) => acc * curr, 1);
62+
// 取得最後面兩個最小數並相乘
63+
let pairB = nums.slice(nums.length - 2,nums.length).reduce((acc, curr) => acc * curr, 1);
64+
return Math.abs(pairA - pairB);
5465
};
5566
const nums = [5, 6, 2, 7, 4];
5667
// 34 => (6*7)-(2*4)=34

javascript/LeetCode/Array/2169.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2169. Count Operations to Obtain Zero
3+
*
4+
* 一次操作中,若nums1 > nums2,則nums1 = nums1 - nums2
5+
* nums1 < nums2, nums2 = nums1 - nums2
6+
* 計算要幾次才能使得nums1 = 0 or num2 = 0
7+
*
8+
* @param {number} num1
9+
* @param {number} num2
10+
* @return {number}
11+
*/
12+
var countOperations = function(num1, num2) {
13+
// solution 1.
14+
// let ans = 0;
15+
// while(num1 !== 0 && num2 !== 0){
16+
// ans += Math.floor(num1 / num2);
17+
// num1 %= num2;
18+
// [num1,num2] = [num2,num1];
19+
// }
20+
// return ans;
21+
22+
// solution 2.
23+
let ans = 0;
24+
while(num1 !== 0 && num2 !== 0){
25+
ans += Math.floor(num1 / num2);
26+
num1 %= num2;
27+
// swap nums1 and nums2
28+
let temp = num1;
29+
num1 = num2;
30+
num2 = temp;
31+
}
32+
return ans;
33+
};
34+
let num1 = 2, num2 = 3;
35+
// Output: 3
36+
// Explanation:
37+
// - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
38+
// - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
39+
// - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
40+
// Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
41+
// So the total number of operations required is 3.
42+
console.log(countOperations(num1,num2))

javascript/LeetCode/Array/2273.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 2273. Find Resultant Array After Removing Anagrams
3+
*
4+
* @param {string[]} words
5+
* @return {string[]}
6+
*/
7+
var removeAnagrams = function(words) {
8+
let res = [];
9+
let prevStr = "";
10+
for(let i = 0;i < words.length;++i) {
11+
let s = words[i].split("").sort().join("");
12+
13+
if (s !== prevStr) {
14+
res.push(words[i]);
15+
prevStr = s;
16+
}
17+
}
18+
return res;
19+
};
20+
let w = ["abba","baba","bbaa","cd","cd"];
21+
// ["abba","cd"]
22+
console.log(removeAnagrams(w));

javascript/LeetCode/Array/228.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 228. Summary Ranges
3+
*
4+
* 參數為數值陣列,若元素是連續且唯一值,則形成一個range,從第一個連續數值到某一元素,若非連續,則直接push進result array
5+
*
6+
* @param {number[]} nums
7+
* @return {string[]}
8+
*/
9+
var summaryRanges = function(nums) {
10+
let res = [];
11+
let i = 0;
12+
while (i < nums.length) {
13+
let start = i;
14+
while (i + 1 < nums.length && nums[i + 1] === nums[i] + 1) {
15+
i++;
16+
}
17+
if (start === i) {
18+
res.push(nums[start].toString());
19+
} else {
20+
res.push(nums[start] + "->" + nums[i]);
21+
}
22+
i++;
23+
}
24+
return res;
25+
};
26+
let nums = [0,1,2,4,5,7];
27+
// ["0->2","4->5","7"]
28+
console.log(summaryRanges(nums));

javascript/LeetCode/Array/2460.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2460. Apply Operations to an Array
3+
*
4+
* 操作n - 1次
5+
* 若nums[i] === nums[i +1],則將nums[i] * 2,將nums[i+1]改成0
6+
* 完成所有操作,將所有的0移到陣列最後面
7+
* 回傳陣列
8+
* @param {number[]} nums
9+
* @return {number[]}
10+
*/
11+
var applyOperations = function(nums) {
12+
let j = 0;
13+
for(let i = 0;i < nums.length - 1;++i) {
14+
if(nums[i] === nums[i+1]){
15+
nums[i] *= 2;
16+
nums[i + 1] = 0;
17+
}
18+
}
19+
for(let i = 0;i < nums.length;++i) {
20+
if(nums[i] !== 0){
21+
nums[j] = nums[i];
22+
j++;
23+
}
24+
}
25+
while (j < nums.length) {
26+
nums[j++] = 0;
27+
}
28+
return nums;
29+
};
30+
let nums = [1,2,2,1,1,0]
31+
// Output: [1,4,2,0,0,0]
32+
// Explanation: We do the following operations:
33+
// i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
34+
// i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
35+
// i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
36+
// i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
37+
// i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
38+
// After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
39+
console.log(applyOperations(nums));

javascript/LeetCode/Array/2598.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 2598. Smallest Missing Non-negative Integer After Operations
3+
*
4+
* 在一次操作中,可以從任一元素中,加value或減value
5+
* @param {number[]} nums
6+
* @param {number} value
7+
* @return {number}
8+
*/
9+
var findSmallestInteger = function(nums, value) {
10+
const mp = new Array(value).fill(0);
11+
for(let i = 0;i < nums.length;i++) {
12+
const v = ((nums[i] % value) + value) % value;
13+
mp[v]++;
14+
}
15+
// console.log(mp)
16+
let mex = 0;
17+
while(mp[mex % value] > 0){
18+
mp[mex % value]--;
19+
mex++;
20+
}
21+
return mex;
22+
};
23+
let nums = [1,-10,7,13,6,8], value = 5;
24+
// Output: 4
25+
// Explanation: One can achieve this result by applying the following operations:
26+
// - Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
27+
// - Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
28+
// - Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
29+
// The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
30+
console.log(findSmallestInteger(nums,value));

javascript/LeetCode/Array/3038.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 3038. Maximum Number of Operations With the Same Score I
3+
*
4+
* 以陣列前兩個元素為一組做加總為一步驟,檢查下一組的加總是否跟前一組一樣,若不是,則回傳結果是一樣的步驟有幾個
5+
* 要能夠取得連續加總值一樣
6+
*
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var maxOperations = function(nums) {
11+
let count = 1;
12+
let firstTwoEleSum = nums[0] + nums[1];
13+
for(let i = 2;i < nums.length - 1;i+=2) {
14+
if(nums[i] + nums[i+1] === firstTwoEleSum){
15+
count++;
16+
}else{
17+
break;
18+
}
19+
}
20+
return count;
21+
};
22+
let nums = [1,5,3,3,4,1,3,2,2,3];
23+
// 2
24+
console.log(maxOperations(nums));

javascript/LeetCode/Array/3289.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 3289. The Two Sneaky Numbers of Digitville
3+
*
4+
* 參數為數值陣列,回傳元素出現次數大於2的元素。回傳的陣列每個必須是唯一值
5+
*
6+
* @param {number[]} nums
7+
* @return {number[]}
8+
*/
9+
var getSneakyNumbers = function(nums) {
10+
// element出現次數 >= 2才能被保留
11+
// 回傳的陣列元素必須是唯一值
12+
// let map = new Map();
13+
// let ans = [];
14+
// for(let i = 0;i < nums.length;++i) {
15+
// map.has(nums[i]) ? map.set(nums[i],map.get(nums[i])+ 1) : map.set(nums[i],1);
16+
// }
17+
// for(const [key,value] of map) {
18+
// if(value >= 2){
19+
// ans.push(key);
20+
// }
21+
// }
22+
// return ans;
23+
24+
// solution 2.
25+
let set = new Set();
26+
let result = [];
27+
for(let i = 0;i < nums.length;i++) {
28+
if(set.has(nums[i])){
29+
result.push(nums[i]);
30+
}else{
31+
set.add(nums[i]);
32+
}
33+
}
34+
return result;
35+
};
36+
let nums = [0,1,1,0];
37+
// [0,1]
38+
console.log(getSneakyNumbers(nums));

javascript/LeetCode/Array/3350.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 3350. Adjacent Increasing Subarrays Detection II
3+
*
4+
* 找出連續increase的子陣列,最大長度為?
5+
* 子陣列有2組
6+
*
7+
* starting at indices a and b (a < b)
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
var maxIncreasingSubarrays = function(nums) {
12+
let current = 1, previous = 0,ans = 0;
13+
for(let i = 1;i < nums.length;++i) {
14+
if(nums[i - 1] < nums[i]){
15+
current++;
16+
}else{
17+
previous = current;
18+
current = 1;
19+
}
20+
ans = Math.max(ans, Math.min(previous, current));
21+
ans = Math.max(ans, Math.floor(current / 2));
22+
}
23+
return ans;
24+
};
25+
let nums = [2,5,7,8,9,2,3,4,3,1];
26+
/**
27+
* 3
28+
* The subarray starting at index 2 is [7, 8, 9], which is strictly increasing.
29+
* The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing.
30+
* These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
31+
*/
32+
console.log(maxIncreasingSubarrays(nums));

0 commit comments

Comments
 (0)