Skip to content

Commit 4c0685c

Browse files
committed
add practice in JS
1 parent b47223e commit 4c0685c

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

javascript/LeetCode/Array/3542.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 3542. Minimum Operations to Convert All Elements to Zero
3+
*
4+
* 計算將所有的元素變成0最少需要幾次操作
5+
* 一次操作中,選子陣列「i,j] (0 <= i <= j < nums.length),並將
6+
* [start index i,end index j]
7+
*
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
var minOperations = function(nums) {
12+
let ans = 0;
13+
let arr = [];
14+
for(let i = 0;i < nums.length;++i) {
15+
// 檢查塞入的 element 有沒有 break monoStack 的單線程(遞增or遞減)
16+
while(arr.length && arr[arr.length - 1] > nums[i]){
17+
// 如果有,把 stack 元素先做調整
18+
arr.pop();
19+
}
20+
if(nums[i] === 0){
21+
continue;
22+
}
23+
if(!arr.length || arr[arr.length - 1] < nums[i]) {
24+
ans++;
25+
arr.push(nums[i]);
26+
}
27+
}
28+
return ans;
29+
};
30+
let nums = [3,1,2,1];
31+
/*
32+
3
33+
Select subarray [1,3] (which is [1,2,1]), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in [3,0,2,0].
34+
Select subarray [2,2] (which is [2]), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in [3,0,0,0].
35+
Select subarray [0,0] (which is [3]), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in [0,0,0,0].
36+
Thus, the minimum number of operations required is 3.
37+
*/
38+
console.log(minOperations(nums));

javascript/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,43 @@ var findXSum = function(nums, k, x) {
10711071
}
10721072

10731073
};
1074-
let nums = [1,1,2,2,3,4,2,3], k = 6, x = 2;
1074+
// let nums = [1,1,2,2,3,4,2,3], k = 6, x = 2;
10751075
// [6,10,12]
10761076
// For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.
10771077
// For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.
10781078
// For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.
1079-
console.log(findXSum(nums,k,x));
1079+
// console.log(findXSum(nums,k,x));
1080+
1081+
1082+
/**
1083+
* 2169. Count Operations to Obtain Zero
1084+
*
1085+
* 一次操作中,若nums1 >= nums2,則nums1 = nums1 - nums2,否則nums2 = nums1 - nums2
1086+
* 計算要幾次才能使得nums1 = 0 or num2 = 0
1087+
*
1088+
* @param {number} num1
1089+
* @param {number} num2
1090+
* @return {number}
1091+
*/
1092+
var countOperations = function(num1, num2) {
1093+
let ans = 0;
1094+
while(num1 !== 0 || num2 !== 0){
1095+
if(num1 > num2){
1096+
num1 = num1 - num2;
1097+
ans++;
1098+
}else{
1099+
num2 = num1 - num2;
1100+
ans++;
1101+
}
1102+
}
1103+
return ans;
1104+
};
1105+
let num1 = 2, num2 = 3
1106+
// Output: 3
1107+
// Explanation:
1108+
// - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
1109+
// - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
1110+
// - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
1111+
// Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
1112+
// So the total number of operations required is 3.
1113+
console.log(countOperations(num1,num2))

0 commit comments

Comments
 (0)