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
32 changes: 32 additions & 0 deletions javascript/LeetCode/Array/1534.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 1534. Count Good Triplets
*
* good triplets:
* 0 <= i < j < k < arr.length
* |arr[i] - arr[j]| <= a
* |arr[j] - arr[k]| <= b
* |arr[i] - arr[k]| <= c
*
* @param {number[]} arr
* @param {number} a
* @param {number} b
* @param {number} c
* @return {number}
*/
var countGoodTriplets = function(arr, a, b, c) {
let ans = 0;
for(let i = 0;i < arr.length;i++) {
for(let j = i+1;j < arr.length;j++) {
for(let k = j+1;k < arr.length;k++) {
if(Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c){
ans++;
}
}
}
}
return ans;
};
let arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3;
// Output: 4
// Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
console.log(countGoodTriplets(arr,a,b,c));
17 changes: 14 additions & 3 deletions javascript/LeetCode/Array/1913.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ var maxProductDifference = function (nums) {
// return (max[0] * max[1]) - (min[0] * min[1]);

// solution 2.Runtime took 100 ms
nums.sort((a, b) => a - b);
let length = nums.length;
return (nums[length - 1] * nums[length - 2]) - (nums[0] * nums[1]);
// nums.sort((a, b) => a - b);
// let length = nums.length;
// return (nums[length - 1] * nums[length - 2]) - (nums[0] * nums[1]);


// solution 3.Update in 2025/10/2.Runtime took 73 ms.
// pair a = 兩個最大數; pair b = 兩個最小數
// sort 由大至小
nums.sort((a,b) => b - a);
// 取得第一組兩個最大數並相乘
let pairA = nums.slice(0,2).reduce((acc, curr) => acc * curr, 1);
// 取得最後面兩個最小數並相乘
let pairB = nums.slice(nums.length - 2,nums.length).reduce((acc, curr) => acc * curr, 1);
return Math.abs(pairA - pairB);
};
const nums = [5, 6, 2, 7, 4];
// 34 => (6*7)-(2*4)=34
Expand Down
42 changes: 42 additions & 0 deletions javascript/LeetCode/Array/2169.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 2169. Count Operations to Obtain Zero
*
* 一次操作中,若nums1 > nums2,則nums1 = nums1 - nums2
* nums1 < nums2, nums2 = nums1 - nums2
* 計算要幾次才能使得nums1 = 0 or num2 = 0
*
* @param {number} num1
* @param {number} num2
* @return {number}
*/
var countOperations = function(num1, num2) {
// solution 1.
// let ans = 0;
// while(num1 !== 0 && num2 !== 0){
// ans += Math.floor(num1 / num2);
// num1 %= num2;
// [num1,num2] = [num2,num1];
// }
// return ans;

// solution 2.
let ans = 0;
while(num1 !== 0 && num2 !== 0){
ans += Math.floor(num1 / num2);
num1 %= num2;
// swap nums1 and nums2
let temp = num1;
num1 = num2;
num2 = temp;
}
return ans;
};
let num1 = 2, num2 = 3;
// Output: 3
// Explanation:
// - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
// - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
// - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
// Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
// So the total number of operations required is 3.
console.log(countOperations(num1,num2))
22 changes: 22 additions & 0 deletions javascript/LeetCode/Array/2273.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 2273. Find Resultant Array After Removing Anagrams
*
* @param {string[]} words
* @return {string[]}
*/
var removeAnagrams = function(words) {
let res = [];
let prevStr = "";
for(let i = 0;i < words.length;++i) {
let s = words[i].split("").sort().join("");

if (s !== prevStr) {
res.push(words[i]);
prevStr = s;
}
}
return res;
};
let w = ["abba","baba","bbaa","cd","cd"];
// ["abba","cd"]
console.log(removeAnagrams(w));
28 changes: 28 additions & 0 deletions javascript/LeetCode/Array/228.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 228. Summary Ranges
*
* 參數為數值陣列,若元素是連續且唯一值,則形成一個range,從第一個連續數值到某一元素,若非連續,則直接push進result array
*
* @param {number[]} nums
* @return {string[]}
*/
var summaryRanges = function(nums) {
let res = [];
let i = 0;
while (i < nums.length) {
let start = i;
while (i + 1 < nums.length && nums[i + 1] === nums[i] + 1) {
i++;
}
if (start === i) {
res.push(nums[start].toString());
} else {
res.push(nums[start] + "->" + nums[i]);
}
i++;
}
return res;
};
let nums = [0,1,2,4,5,7];
// ["0->2","4->5","7"]
console.log(summaryRanges(nums));
39 changes: 39 additions & 0 deletions javascript/LeetCode/Array/2460.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 2460. Apply Operations to an Array
*
* 操作n - 1次
* 若nums[i] === nums[i +1],則將nums[i] * 2,將nums[i+1]改成0
* 完成所有操作,將所有的0移到陣列最後面
* 回傳陣列
* @param {number[]} nums
* @return {number[]}
*/
var applyOperations = function(nums) {
let j = 0;
for(let i = 0;i < nums.length - 1;++i) {
if(nums[i] === nums[i+1]){
nums[i] *= 2;
nums[i + 1] = 0;
}
}
for(let i = 0;i < nums.length;++i) {
if(nums[i] !== 0){
nums[j] = nums[i];
j++;
}
}
while (j < nums.length) {
nums[j++] = 0;
}
return nums;
};
let nums = [1,2,2,1,1,0]
// Output: [1,4,2,0,0,0]
// Explanation: We do the following operations:
// i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
// 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].
// i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
// 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].
// 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].
// After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
console.log(applyOperations(nums));
30 changes: 30 additions & 0 deletions javascript/LeetCode/Array/2598.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 2598. Smallest Missing Non-negative Integer After Operations
*
* 在一次操作中,可以從任一元素中,加value或減value
* @param {number[]} nums
* @param {number} value
* @return {number}
*/
var findSmallestInteger = function(nums, value) {
const mp = new Array(value).fill(0);
for(let i = 0;i < nums.length;i++) {
const v = ((nums[i] % value) + value) % value;
mp[v]++;
}
// console.log(mp)
let mex = 0;
while(mp[mex % value] > 0){
mp[mex % value]--;
mex++;
}
return mex;
};
let nums = [1,-10,7,13,6,8], value = 5;
// Output: 4
// Explanation: One can achieve this result by applying the following operations:
// - Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
// - Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
// - Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
// The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
console.log(findSmallestInteger(nums,value));
24 changes: 24 additions & 0 deletions javascript/LeetCode/Array/3038.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 3038. Maximum Number of Operations With the Same Score I
*
* 以陣列前兩個元素為一組做加總為一步驟,檢查下一組的加總是否跟前一組一樣,若不是,則回傳結果是一樣的步驟有幾個
* 要能夠取得連續加總值一樣
*
* @param {number[]} nums
* @return {number}
*/
var maxOperations = function(nums) {
let count = 1;
let firstTwoEleSum = nums[0] + nums[1];
for(let i = 2;i < nums.length - 1;i+=2) {
if(nums[i] + nums[i+1] === firstTwoEleSum){
count++;
}else{
break;
}
}
return count;
};
let nums = [1,5,3,3,4,1,3,2,2,3];
// 2
console.log(maxOperations(nums));
38 changes: 38 additions & 0 deletions javascript/LeetCode/Array/3289.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 3289. The Two Sneaky Numbers of Digitville
*
* 參數為數值陣列,回傳元素出現次數大於2的元素。回傳的陣列每個必須是唯一值
*
* @param {number[]} nums
* @return {number[]}
*/
var getSneakyNumbers = function(nums) {
// element出現次數 >= 2才能被保留
// 回傳的陣列元素必須是唯一值
// let map = new Map();
// let ans = [];
// for(let i = 0;i < nums.length;++i) {
// map.has(nums[i]) ? map.set(nums[i],map.get(nums[i])+ 1) : map.set(nums[i],1);
// }
// for(const [key,value] of map) {
// if(value >= 2){
// ans.push(key);
// }
// }
// return ans;

// solution 2.
let set = new Set();
let result = [];
for(let i = 0;i < nums.length;i++) {
if(set.has(nums[i])){
result.push(nums[i]);
}else{
set.add(nums[i]);
}
}
return result;
};
let nums = [0,1,1,0];
// [0,1]
console.log(getSneakyNumbers(nums));
32 changes: 32 additions & 0 deletions javascript/LeetCode/Array/3350.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 3350. Adjacent Increasing Subarrays Detection II
*
* 找出連續increase的子陣列,最大長度為?
* 子陣列有2組
*
* starting at indices a and b (a < b)
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function(nums) {
let current = 1, previous = 0,ans = 0;
for(let i = 1;i < nums.length;++i) {
if(nums[i - 1] < nums[i]){
current++;
}else{
previous = current;
current = 1;
}
ans = Math.max(ans, Math.min(previous, current));
ans = Math.max(ans, Math.floor(current / 2));
}
return ans;
};
let nums = [2,5,7,8,9,2,3,4,3,1];
/**
* 3
* The subarray starting at index 2 is [7, 8, 9], which is strictly increasing.
* The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing.
* These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
*/
console.log(maxIncreasingSubarrays(nums));
48 changes: 48 additions & 0 deletions javascript/LeetCode/Array/3354.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 3354. Make Array Elements Equal to Zero
*
* curr = index,nums[index] == 0
* 若curr超過範圍[0,nums.length - 1] 操作結束
* 若nums[index] == 0,則curr 增加(往右),反之則curr減少(往左)
* nums[index] > 0 ,nums[current] - 1且左右反轉
*
* @param {number[]} nums
* @return {number}
*/
var countValidSelections = function(nums) {
// you need to find the sum of all the numbers to the left of where nums[i]==0 and the sum of all the numbers to the right of that point.
// If you need more help, look at the detailed explanation in this comment.
let ans = 0;
let sum = nums.reduce((a,b) => a + b,0);
let left = 0,right = sum;
for(let i = 0;i < nums.length;++i) {
if(nums[i] === 0){
if(left - right >= 0 && left - right <= 1) {
ans++;
}
if(right - left >= 0 && right - left <= 1) {
ans++;
}
}else{
left += nums[i];
right -= nums[i];
}
}
return ans;
};
let nums = [1,0,2,0,3];
/**
* 2
* The only possible valid selections are the following:
Choose curr = 3, and a movement direction to the left.
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] ->
[1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] ->
[1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] ->
[0,0,0,0,1] -> [0,0,0,0,0].

Choose curr = 3, and a movement direction to the right.
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] ->
[1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] ->
[1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
*/
console.log(countValidSelections(nums));
Loading