From e876366bde620bad1c37cae2bae18065424346f5 Mon Sep 17 00:00:00 2001 From: sjbaroi1998 Date: Fri, 7 Apr 2023 23:48:51 +0530 Subject: [PATCH] Adding JS Solutions For Problem No 219, 658 and 1343. --- README.md | 6 ++-- javascript/0219-contains-duplicate-ii.js | 29 +++++++++++++++++++ javascript/0658-find-k-closest-elements.js | 22 ++++++++++++++ ...rage-greater-than-or-equal-to-threshold.js | 23 +++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 javascript/0219-contains-duplicate-ii.js create mode 100644 javascript/0658-find-k-closest-elements.js create mode 100644 javascript/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js diff --git a/README.md b/README.md index 5d7de88e9..419f2441b 100644 --- a/README.md +++ b/README.md @@ -104,15 +104,15 @@ If you would like to have collaborator permissions on the repo to merge your own Problem | C | C++ | C# | Dart | GO | Java | JS | Kotlin | Python | Ruby | Rust | Scala | Swift | TS ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- [0121 - Best Time to Buy And Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
[✔️](c%2F0121-best-time-to-buy-and-sell-stock.c)
|
[✔️](cpp%2F0121-best-time-to-buy-and-sell-stock.cpp)
|
[✔️](csharp%2F0121-best-time-to-buy-and-sell-stock.cs)
|
|
[✔️](go%2F0121-best-time-to-buy-and-sell-stock.go)
|
[✔️](java%2F0121-best-time-to-buy-and-sell-stock.java)
|
[✔️](javascript%2F0121-best-time-to-buy-and-sell-stock.js)
|
[✔️](kotlin%2F0121-best-time-to-buy-and-sell-stock.kt)
|
[✔️](python%2F0121-best-time-to-buy-and-sell-stock.py)
|
[✔️](ruby%2F0121-best-time-to-buy-and-sell-stock.rb)
|
[✔️](rust%2F0121-best-time-to-buy-and-sell-stock.rs)
|
|
[✔️](swift%2F0121-best-time-to-buy-and-sell-stock.swift)
|
[✔️](typescript%2F0121-best-time-to-buy-and-sell-stock.ts)
-[0219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) |
|
[✔️](cpp%2F0219-Contains-Duplicate-II.cpp)
|
|
|
|
|
|
[✔️](kotlin%2F0219-contains-duplicate-ii.kt)
|
[✔️](python%2F0219-contains-duplicate-ii.py)
|
|
|
|
|
-[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/) |
|
|
|
|
|
|
|
[✔️](kotlin%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.kt)
|
[✔️](python%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py)
|
|
|
|
|
+[0219 - Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) |
|
[✔️](cpp%2F0219-Contains-Duplicate-II.cpp)
|
|
|
|
|
[✔️](javascript%2F0219-contains-duplicate-ii.js)
|
[✔️](kotlin%2F0219-contains-duplicate-ii.kt)
|
[✔️](python%2F0219-contains-duplicate-ii.py)
|
|
|
|
|
+[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/) |
|
|
|
|
|
|
[✔️](javascript%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js)
|
[✔️](kotlin%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.kt)
|
[✔️](python%2F1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py)
|
|
|
|
|
[0003 - Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) |
[✔️](c%2F0003-longest-substring-without-repeating-characters.c)
|
[✔️](cpp%2F0003-longest-substring-without-repeating-characters.cpp)
|
[✔️](csharp%2F0003-longest-substring-without-repeating-characters.cs)
|
|
[✔️](go%2F0003-longest-substring-without-repeating-characters.go)
|
[✔️](java%2F0003-longest-substring-without-repeating-characters.java)
|
[✔️](javascript%2F0003-longest-substring-without-repeating-characters.js)
|
[✔️](kotlin%2F0003-longest-substring-without-repeating-characters.kt)
|
[✔️](python%2F0003-longest-substring-without-repeating-characters.py)
|
[✔️](ruby%2F0003-longest-substring-without-repeating-characters.rb)
|
[✔️](rust%2F0003-longest-substring-without-repeating-characters.rs)
|
[✔️](scala%2F0003-longest-substring-without-repeating-characters.scala)
|
[✔️](swift%2F0003-longest-substring-without-repeating-characters.swift)
|
[✔️](typescript%2F0003-longest-substring-without-repeating-characters.ts)
[0424 - Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) |
[✔️](c%2F0424-longest-repeating-character-replacement.c)
|
[✔️](cpp%2F0424-longest-repeating-character-replacement.cpp)
|
[✔️](csharp%2F0424-longest-repeating-character-replacement.cs)
|
|
[✔️](go%2F0424-longest-repeating-character-replacement.go)
|
[✔️](java%2F0424-longest-repeating-character-replacement.java)
|
[✔️](javascript%2F0424-longest-repeating-character-replacement.js)
|
[✔️](kotlin%2F0424-longest-repeating-character-replacement.kt)
|
[✔️](python%2F0424-longest-repeating-character-replacement.py)
|
[✔️](ruby%2F0424-longest-repeating-character-replacement.rb)
|
[✔️](rust%2F0424-longest-repeating-character-replacement.rs)
|
|
[✔️](swift%2F0424-longest-repeating-character-replacement.swift)
|
[✔️](typescript%2F0424-longest-repeating-character-replacement.ts)
[0567 - Permutation In String](https://leetcode.com/problems/permutation-in-string/) |
[✔️](c%2F0567-permutation-in-string.c)
|
[✔️](cpp%2F0567-permutation-in-string.cpp)
|
[✔️](csharp%2F0567-permutation-in-string.cs)
|
|
[✔️](go%2F0567-permutation-in-string.go)
|
[✔️](java%2F0567-permutation-in-string.java)
|
[✔️](javascript%2F0567-permutation-in-string.js)
|
[✔️](kotlin%2F0567-permutation-in-string.kt)
|
[✔️](python%2F0567-permutation-in-string.py)
|
|
[✔️](rust%2F0567-permutation-in-string.rs)
|
|
|
[✔️](typescript%2F0567-permutation-in-string.ts)
[1838 - Frequency of The Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) |
[✔️](c%2F1838-frequency-of-the-most-frequent-element.c)
|
[✔️](cpp%2F1838-frequency-of-the-most-frequent-element.cpp)
|
[✔️](csharp%2F1838-Frequency-Of-The-Most-Frequent-Element.cs)
|
|
[✔️](go%2F1838-frequency-of-the-most-frequent-element.go)
|
|
[✔️](javascript%2F1838-frequency-of-the-most-frequent-element.js)
|
|
|
|
[✔️](rust%2F1838-frequency-of-the-most-frequent-element.rs)
|
|
|
[✔️](typescript%2F1838-frequency-of-the-most-frequent-element.ts)
[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/) |
|
|
|
|
|
|
[✔️](javascript%2F1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js)
|
|
|
|
|
|
|
[✔️](typescript%2F1888-minimum-number-of-flips-to-make-the-binary-string-alternating.ts)
[0209 - Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) |
[✔️](c%2F0209-minimum-size-subarray-sum.c)
|
[✔️](cpp%2F0209-minimum-size-subarray-sum.cpp)
|
|
|
[✔️](go%2F0209-minimum-size-subarray-sum.go)
|
|
[✔️](javascript%2F0209-minimum-size-subarray-sum.js)
|
|
|
|
|
|
|
-[0658 - Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) |
|
|
|
|
|
|
|
[✔️](kotlin%2F0658-find-k-closest-elements.kt)
|
[✔️](python%2F0658-find-k-closest-elements.py)
|
|
|
|
|
[✔️](typescript%2F0658-find-k-closest-elements.ts)
+[0658 - Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) |
|
|
|
|
|
|
[✔️](javascript%2F0658-find-k-closest-elements.js)
|
[✔️](kotlin%2F0658-find-k-closest-elements.kt)
|
[✔️](python%2F0658-find-k-closest-elements.py)
|
|
|
|
|
[✔️](typescript%2F0658-find-k-closest-elements.ts)
[0076 - Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) |
[✔️](c%2F0076-minimum-window-substring.c)
|
[✔️](cpp%2F0076-minimum-window-substring.cpp)
|
[✔️](csharp%2F0076-minimum-window-substring.cs)
|
|
[✔️](go%2F0076-minimum-window-substring.go)
|
[✔️](java%2F0076-minimum-window-substring.java)
|
[✔️](javascript%2F0076-minimum-window-substring.js)
|
[✔️](kotlin%2F0076-minimum-window-substring.kt)
|
[✔️](python%2F0076-minimum-window-substring.py)
|
[✔️](ruby%2F0076-minimum-window-substring.rb)
|
[✔️](rust%2F0076-minimum-window-substring.rs)
|
|
[✔️](swift%2F0076-minimum-window-substring.swift)
|
[✔️](typescript%2F0076-minimum-window-substring.ts)
[0239 - Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) |
|
[✔️](cpp%2F0239-sliding-window-maximum.cpp)
|
[✔️](csharp%2F0239-sliding-window-maximum.cs)
|
|
[✔️](go%2F0239-sliding-window-maximum.go)
|
[✔️](java%2F0239-sliding-window-maximum.java)
|
[✔️](javascript%2F0239-sliding-window-maximum.js)
|
[✔️](kotlin%2F0239-sliding-window-maximum.kt)
|
[✔️](python%2F0239-sliding-window-maximum.py)
|
[✔️](ruby%2F0239-sliding-window-maximum.rb)
|
|
|
|
[✔️](typescript%2F0239-sliding-window-maximum.ts)
diff --git a/javascript/0219-contains-duplicate-ii.js b/javascript/0219-contains-duplicate-ii.js new file mode 100644 index 000000000..633e77fa9 --- /dev/null +++ b/javascript/0219-contains-duplicate-ii.js @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var containsNearbyDuplicate = function (nums, k) { + let leftPtr = 0; + let rightPtr = 0; + let windowHashMap = new Map(); + while (rightPtr < nums.length) { + if (windowHashMap.has(nums[rightPtr])) { + return true; + } else { + windowHashMap.set(nums[rightPtr], 1); + } + rightPtr++; + if (rightPtr - leftPtr > k) { + let leftPtrValue = windowHashMap.get(nums[leftPtr]); + leftPtrValue--; + if (leftPtrValue == 0) { + windowHashMap.delete(nums[leftPtr]); + } else { + windowHashMap.set(nums[leftPtr], leftPtrValue); + } + leftPtr++; + } + } + return false; +}; diff --git a/javascript/0658-find-k-closest-elements.js b/javascript/0658-find-k-closest-elements.js new file mode 100644 index 000000000..c8033dbbb --- /dev/null +++ b/javascript/0658-find-k-closest-elements.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} arr + * @param {number} k + * @param {number} x + * @return {number[]} + */ +var findClosestElements = function (arr, k, x) { + let [leftPtr, rightPtr] = [0, arr.length - k]; + while (leftPtr < rightPtr) { + /* This is basically rightPtr+leftPtr/2 written differently to + avoid any overflow incase it happens. + */ + let mid = parseInt(rightPtr + (leftPtr - rightPtr) / 2); + + if (x - arr[mid] > arr[mid + k] - x) { + leftPtr = mid + 1; + } else { + rightPtr = mid; + } + } + return arr.slice(leftPtr, leftPtr + k); +}; diff --git a/javascript/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js b/javascript/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js new file mode 100644 index 000000000..969062e79 --- /dev/null +++ b/javascript/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} arr + * @param {number} k + * @param {number} threshold + * @return {number} + */ +var numOfSubarrays = function (arr, k, threshold) { + let [leftPtr, rightPtr, result, windowSum] = [0, 0, 0, 0]; + while (rightPtr < arr.length) { + windowSum += arr[rightPtr]; + if (rightPtr - leftPtr >= k - 1) { + if (rightPtr - leftPtr > k - 1) { + windowSum -= arr[leftPtr]; + leftPtr++; + } + if (windowSum / k >= threshold) { + result++; + } + } + rightPtr++; + } + return result; +};