Skip to content

Commit e550273

Browse files
committed
2006. Count Number of Pairs With Absolute Difference K
1 parent 62cff8d commit e550273

7 files changed

+164
-3
lines changed

Easy/20. Valid Parentheses.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ https://leetcode.com/problems/valid-parentheses/
44
55
*/
66

7-
/* TIME COMPLEXITY IS O(logn) */
7+
/* TIME COMPLEXITY IS O(N) */
88
/*
99
Example 1:
1010
@@ -54,5 +54,4 @@ var isValid = function (s) {
5454
}
5555
return contain.length > 0 ? "false" : "true";
5656
};
57-
var str = "()";
58-
console.log(isValid(str));
57+
console.log(isValid("({"));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
2006. Count Number of Pairs With Absolute Difference K
3+
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/
4+
*/
5+
6+
/**
7+
* @param {number[]} nums
8+
* @param {number} k
9+
* @return {number}
10+
*/
11+
12+
/* METHOD 1 TIME COMPLEXITY O(NLogN) */
13+
var countKDifference = function (nums, k) {
14+
var count = 0; // Create a count
15+
for (let i = 0; i < nums.length; i++) {
16+
// here if the current index value is 2
17+
// then 2 + 2 = 4
18+
// 2 - 2 = 0
19+
// if 4 and 0 is exists in next slice array then count how many the total number of accurence in that array.
20+
var val1 = nums[i] - k;
21+
var val2 = nums[i] + k;
22+
// slice array and count that number
23+
nums.slice(i + 1).forEach((v) => ((v == val1 || v == val2) && count++));
24+
}
25+
return count;
26+
};
27+
28+
/* METHOD 2 TIME COMPLEXITY O(NLogN) */
29+
var countKDifference2 = function (nums, k) {
30+
var count = 0;
31+
for (let i = 0; i < nums.length; i++) {
32+
for (let j = 1; j < nums.length; j++) {
33+
// direct check with constraints.
34+
if (i < j && (Math.abs(nums[i] - nums[j]) == k)) {
35+
count++;
36+
}
37+
}
38+
}
39+
return count;
40+
};
41+
42+
43+
44+
console.log(countKDifference([3, 2, 1, 5, 4], 2));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
function removeDuplicates(nums) {
22
return Array.from(new Set(nums));
33
};
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
10+
/* TIME COMPLEXITY O(N) */
11+
12+
// nums is sorted in non-decreasing order.
13+
14+
var removeDuplicates = function (nums) {
15+
for (let i = 0; i < nums.length; i++) {
16+
if (nums[i] === nums[i + 1]) { // If the next element is same than remove from the array
17+
nums.splice(i, 1);
18+
i--;
19+
}
20+
}
21+
return nums.length;
22+
};
23+
424
console.log(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]));

Easy/414. Third Maximum Number.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var thirdMax = function (nums) {
6+
nums.sort((a, b) => { return b - a });
7+
var set = new Set(nums);
8+
nums = [...set];
9+
return nums[2] === undefined ? nums[0] : nums[2];
10+
11+
};
12+
13+
console.log(thirdMax([3, 2, 1, 4]));
14+
console.log(thirdMax([2, 2, 3, 1]));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
448. Find All Numbers Disappeared in an Array
3+
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
4+
*/
5+
6+
7+
/**
8+
* @param {number[]} nums
9+
* @return {number[]}
10+
*/
11+
12+
13+
/*
14+
Method 1 TIME COMPEXITY O(N)
15+
*/
16+
var findDisappearedNumbers = function (nums) {
17+
var set = new Set(nums); // make a set
18+
var ansArray = []; // create a extra array which store the ans array
19+
for (let i = 1; i <= nums.length; i++) {
20+
if (!set.has(i)) { // check that if the set contain Ith element then dont't add it to the ans Array
21+
ansArray.push(i);
22+
}
23+
}
24+
return ansArray;
25+
};
26+
27+
/* Method 2 TIME COMPLEXITY O(N) */
28+
29+
var findDisappearedNumbers2 = function (nums) {
30+
var ansArray = [];
31+
for (let i = 1; i <= nums.length; i++) {
32+
if (!nums.includes(i)) {
33+
ansArray.push(i);
34+
}
35+
}
36+
return ansArray;
37+
};
38+
39+
console.log(findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1]));

Easy/485. Max Consecutive Ones.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
485. Max Consecutive Ones
3+
https://leetcode.com/problems/max-consecutive-ones/
4+
*/
5+
6+
/* TIME COMPLEXITY O(N) */
7+
8+
/**
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var findMaxConsecutiveOnes = function (nums) {
13+
var max = 0; // Create a max which contain recent count 1 value
14+
var maxSoFar = 0; // Create a maxSoFar which contain heighest max value
15+
for (let i = 0; i < nums.length; i++) {
16+
if (nums[i] === 1) { // if the current value is 1 increment the max
17+
max++;
18+
} else {
19+
if (max > maxSoFar) { // when the max is greater then the mazSoFar modify the maxSoFar
20+
maxSoFar = max;
21+
}
22+
max = 0; // When the condition is false modify the max to 0
23+
}
24+
}
25+
return max > maxSoFar ? max : maxSoFar; // Here If the max greater then the maxSoFar return max else maxSoFae
26+
};
27+
console.log(findMaxConsecutiveOnes([1, 0, 1, 1, 0, 1]));

Easy/509. Fibonacci Number.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
509. Fibonacci Number
3+
https://leetcode.com/problems/fibonacci-number/
4+
5+
*/
6+
7+
/* TIME COMPLEXITY O(N) */
8+
9+
/**
10+
* @param {number} n
11+
* @return {number}
12+
*/
13+
var fib = function (n) {
14+
if (n == 0) return 0; // 1 Base case.
15+
if (n == 1) return 1; // 2 Base case.
16+
return fib(n - 1) + fib(n - 2);
17+
};
18+
console.log(fib(1));

0 commit comments

Comments
 (0)