Skip to content

Commit e876366

Browse files
committed
Adding JS Solutions For Problem No 219, 658 and 1343.
1 parent 96e744c commit e876366

4 files changed

+77
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ If you would like to have collaborator permissions on the repo to merge your own
104104
<sub>Problem</sub> | <sub>C</sub> | <sub>C++</sub> | <sub>C#</sub> | <sub>Dart</sub> | <sub>GO</sub> | <sub>Java</sub> | <sub>JS</sub> | <sub>Kotlin</sub> | <sub>Python</sub> | <sub>Ruby</sub> | <sub>Rust</sub> | <sub>Scala</sub> | <sub>Swift</sub> | <sub>TS</sub>
105105
---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ----
106106
<sub>[0121 - Best Time to Buy And Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)</sub> | <sub><div align='center'>[✔️](c%2F0121-best-time-to-buy-and-sell-stock.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0121-best-time-to-buy-and-sell-stock.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0121-best-time-to-buy-and-sell-stock.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0121-best-time-to-buy-and-sell-stock.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0121-best-time-to-buy-and-sell-stock.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0121-best-time-to-buy-and-sell-stock.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0121-best-time-to-buy-and-sell-stock.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0121-best-time-to-buy-and-sell-stock.py)</div></sub> | <sub><div align='center'>[✔️](ruby%2F0121-best-time-to-buy-and-sell-stock.rb)</div></sub> | <sub><div align='center'>[✔️](rust%2F0121-best-time-to-buy-and-sell-stock.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](swift%2F0121-best-time-to-buy-and-sell-stock.swift)</div></sub> | <sub><div align='center'>[✔️](typescript%2F0121-best-time-to-buy-and-sell-stock.ts)</div></sub>
107-
<sub>[0219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0219-Contains-Duplicate-II.cpp)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'></div></sub> | <sub><div align='center'>[✔️](kotlin%2F0219-contains-duplicate-ii.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0219-contains-duplicate-ii.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
108-
<sub>[1343 - Number of Sub Arrays of Size K and Avg Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](kotlin%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
107+
<sub>[0219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0219-Contains-Duplicate-II.cpp)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F0219-contains-duplicate-ii.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0219-contains-duplicate-ii.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0219-contains-duplicate-ii.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
108+
<sub>[1343 - Number of Sub Arrays of Size K and Avg Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
109109
<sub>[0003 - Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)</sub> | <sub><div align='center'>[✔️](c%2F0003-longest-substring-without-repeating-characters.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0003-longest-substring-without-repeating-characters.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0003-longest-substring-without-repeating-characters.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0003-longest-substring-without-repeating-characters.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0003-longest-substring-without-repeating-characters.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0003-longest-substring-without-repeating-characters.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0003-longest-substring-without-repeating-characters.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0003-longest-substring-without-repeating-characters.py)</div></sub> | <sub><div align='center'>[✔️](ruby%2F0003-longest-substring-without-repeating-characters.rb)</div></sub> | <sub><div align='center'>[✔️](rust%2F0003-longest-substring-without-repeating-characters.rs)</div></sub> | <sub><div align='center'>[✔️](scala%2F0003-longest-substring-without-repeating-characters.scala)</div></sub> | <sub><div align='center'>[✔️](swift%2F0003-longest-substring-without-repeating-characters.swift)</div></sub> | <sub><div align='center'>[✔️](typescript%2F0003-longest-substring-without-repeating-characters.ts)</div></sub>
110110
<sub>[0424 - Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)</sub> | <sub><div align='center'>[✔️](c%2F0424-longest-repeating-character-replacement.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0424-longest-repeating-character-replacement.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0424-longest-repeating-character-replacement.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0424-longest-repeating-character-replacement.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0424-longest-repeating-character-replacement.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0424-longest-repeating-character-replacement.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0424-longest-repeating-character-replacement.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0424-longest-repeating-character-replacement.py)</div></sub> | <sub><div align='center'>[✔️](ruby%2F0424-longest-repeating-character-replacement.rb)</div></sub> | <sub><div align='center'>[✔️](rust%2F0424-longest-repeating-character-replacement.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](swift%2F0424-longest-repeating-character-replacement.swift)</div></sub> | <sub><div align='center'>[✔️](typescript%2F0424-longest-repeating-character-replacement.ts)</div></sub>
111111
<sub>[0567 - Permutation In String](https://leetcode.com/problems/permutation-in-string/)</sub> | <sub><div align='center'>[✔️](c%2F0567-permutation-in-string.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0567-permutation-in-string.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0567-permutation-in-string.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0567-permutation-in-string.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0567-permutation-in-string.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0567-permutation-in-string.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0567-permutation-in-string.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0567-permutation-in-string.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](rust%2F0567-permutation-in-string.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0567-permutation-in-string.ts)</div></sub>
112112
<sub>[1838 - Frequency of The Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/)</sub> | <sub><div align='center'>[✔️](c%2F1838-frequency-of-the-most-frequent-element.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F1838-frequency-of-the-most-frequent-element.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F1838-Frequency-Of-The-Most-Frequent-Element.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F1838-frequency-of-the-most-frequent-element.go)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F1838-frequency-of-the-most-frequent-element.js)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](rust%2F1838-frequency-of-the-most-frequent-element.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F1838-frequency-of-the-most-frequent-element.ts)</div></sub>
113113
<sub>[1888 - Minimum Number of Flips to Make The Binary String Alternating](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F1888-minimum-number-of-flips-to-make-the-binary-string-alternating.ts)</div></sub>
114114
<sub>[0209 - Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)</sub> | <sub><div align='center'>[✔️](c%2F0209-minimum-size-subarray-sum.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0209-minimum-size-subarray-sum.cpp)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0209-minimum-size-subarray-sum.go)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F0209-minimum-size-subarray-sum.js)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
115-
<sub>[0658 - Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'></div></sub> | <sub><div align='center'>[✔️](kotlin%2F0658-find-k-closest-elements.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0658-find-k-closest-elements.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0658-find-k-closest-elements.ts)</div></sub>
115+
<sub>[0658 - Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F0658-find-k-closest-elements.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0658-find-k-closest-elements.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0658-find-k-closest-elements.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0658-find-k-closest-elements.ts)</div></sub>
116116
<sub>[0076 - Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)</sub> | <sub><div align='center'>[✔️](c%2F0076-minimum-window-substring.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0076-minimum-window-substring.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0076-minimum-window-substring.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0076-minimum-window-substring.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0076-minimum-window-substring.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0076-minimum-window-substring.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0076-minimum-window-substring.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0076-minimum-window-substring.py)</div></sub> | <sub><div align='center'>[✔️](ruby%2F0076-minimum-window-substring.rb)</div></sub> | <sub><div align='center'>[✔️](rust%2F0076-minimum-window-substring.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](swift%2F0076-minimum-window-substring.swift)</div></sub> | <sub><div align='center'>[✔️](typescript%2F0076-minimum-window-substring.ts)</div></sub>
117117
<sub>[0239 - Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0239-sliding-window-maximum.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0239-sliding-window-maximum.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0239-sliding-window-maximum.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0239-sliding-window-maximum.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0239-sliding-window-maximum.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0239-sliding-window-maximum.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0239-sliding-window-maximum.py)</div></sub> | <sub><div align='center'>[✔️](ruby%2F0239-sliding-window-maximum.rb)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0239-sliding-window-maximum.ts)</div></sub>
118118

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
var containsNearbyDuplicate = function (nums, k) {
7+
let leftPtr = 0;
8+
let rightPtr = 0;
9+
let windowHashMap = new Map();
10+
while (rightPtr < nums.length) {
11+
if (windowHashMap.has(nums[rightPtr])) {
12+
return true;
13+
} else {
14+
windowHashMap.set(nums[rightPtr], 1);
15+
}
16+
rightPtr++;
17+
if (rightPtr - leftPtr > k) {
18+
let leftPtrValue = windowHashMap.get(nums[leftPtr]);
19+
leftPtrValue--;
20+
if (leftPtrValue == 0) {
21+
windowHashMap.delete(nums[leftPtr]);
22+
} else {
23+
windowHashMap.set(nums[leftPtr], leftPtrValue);
24+
}
25+
leftPtr++;
26+
}
27+
}
28+
return false;
29+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} k
4+
* @param {number} x
5+
* @return {number[]}
6+
*/
7+
var findClosestElements = function (arr, k, x) {
8+
let [leftPtr, rightPtr] = [0, arr.length - k];
9+
while (leftPtr < rightPtr) {
10+
/* This is basically rightPtr+leftPtr/2 written differently to
11+
avoid any overflow incase it happens.
12+
*/
13+
let mid = parseInt(rightPtr + (leftPtr - rightPtr) / 2);
14+
15+
if (x - arr[mid] > arr[mid + k] - x) {
16+
leftPtr = mid + 1;
17+
} else {
18+
rightPtr = mid;
19+
}
20+
}
21+
return arr.slice(leftPtr, leftPtr + k);
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} k
4+
* @param {number} threshold
5+
* @return {number}
6+
*/
7+
var numOfSubarrays = function (arr, k, threshold) {
8+
let [leftPtr, rightPtr, result, windowSum] = [0, 0, 0, 0];
9+
while (rightPtr < arr.length) {
10+
windowSum += arr[rightPtr];
11+
if (rightPtr - leftPtr >= k - 1) {
12+
if (rightPtr - leftPtr > k - 1) {
13+
windowSum -= arr[leftPtr];
14+
leftPtr++;
15+
}
16+
if (windowSum / k >= threshold) {
17+
result++;
18+
}
19+
}
20+
rightPtr++;
21+
}
22+
return result;
23+
};

0 commit comments

Comments
 (0)