From 01da5d1376ea8b4c7fa70693573b2e25302e04a9 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:04:32 +0530 Subject: [PATCH 1/5] Create Solution.js --- .../3163.String Compression III/Solution.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/3100-3199/3163.String Compression III/Solution.js diff --git a/solution/3100-3199/3163.String Compression III/Solution.js b/solution/3100-3199/3163.String Compression III/Solution.js new file mode 100644 index 0000000000000..9b08e0d39f6fd --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.js @@ -0,0 +1,22 @@ +/** + * @param {string} word + * @return {string} + */ +var compressedString = function(word) { + const ans = []; + const n = word.length; + for (let i = 0; i < n;) { + let j = i + 1; + while (j < n && word[j] === word[i]) { + ++j; + } + let k = j - i; + while (k) { + const x = Math.min(k, 9); + ans.push(x + word[i]); + k -= x; + } + i = j; + } + return ans.join(''); +}; From d07ffafab86bdb29e8753dd569fba2f10a0357a1 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:23:02 +0530 Subject: [PATCH 2/5] Create Solution.js --- .../0219.Contains Duplicate II/Solution.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solution/0200-0299/0219.Contains Duplicate II/Solution.js diff --git a/solution/0200-0299/0219.Contains Duplicate II/Solution.js b/solution/0200-0299/0219.Contains Duplicate II/Solution.js new file mode 100644 index 0000000000000..bf68ed04ea3ec --- /dev/null +++ b/solution/0200-0299/0219.Contains Duplicate II/Solution.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var containsNearbyDuplicate = function(nums, k) { + const d = new Map(); + for (let i = 0; i < nums.length; ++i) { + if (d.has(nums[i]) && i - d.get(nums[i]) <= k) { + return true; + } + d.set(nums[i], i); + } + return false; +}; From 358c95aa2b2b9883249469a1027934968e8e4630 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:22:35 +0530 Subject: [PATCH 3/5] Create Solution.js --- .../Solution.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 solution/3000-3099/3011.Find if Array Can Be Sorted/Solution.js diff --git a/solution/3000-3099/3011.Find if Array Can Be Sorted/Solution.js b/solution/3000-3099/3011.Find if Array Can Be Sorted/Solution.js new file mode 100644 index 0000000000000..cf9552ee8e884 --- /dev/null +++ b/solution/3000-3099/3011.Find if Array Can Be Sorted/Solution.js @@ -0,0 +1,33 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var canSortArray = function(nums) { + let preMx = 0; + const n = nums.length; + for (let i = 0; i < n; ) { + const cnt = bitCount(nums[i]); + let j = i + 1; + let [mi, mx] = [nums[i], nums[i]]; + while (j < n && bitCount(nums[j]) === cnt) { + mi = Math.min(mi, nums[j]); + mx = Math.max(mx, nums[j]); + j++; + } + if (preMx > mi) { + return false; + } + preMx = mx; + i = j; + } + return true; +}; + +const bitCount = (i) => { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} From e48cc3252dce96aec668aceaa9eb76412ad50ce2 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Fri, 15 Nov 2024 10:07:31 +0530 Subject: [PATCH 4/5] Create Solution.js --- .../Solution.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted/Solution.js diff --git a/solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted/Solution.js b/solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted/Solution.js new file mode 100644 index 0000000000000..bb65c90f1e9c9 --- /dev/null +++ b/solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted/Solution.js @@ -0,0 +1,41 @@ +/** + * @param {number[]} arr + * @return {number} + */ +var findLengthOfShortestSubarray = function(arr) { + const n = arr.length; + let i = 0, j = n - 1; + + while (i + 1 < n && arr[i] <= arr[i + 1]) { + i++; + } + + while (j - 1 >= 0 && arr[j - 1] <= arr[j]) { + j--; + } + + if (i >= j) { + return 0; + } + + let ans = Math.min(n - i - 1, j); + + for (let l = 0; l <= i; l++) { + const r = bisectLeft(arr, arr[l], j, n); + ans = Math.min(ans, r - l - 1); + } + + return ans; +}; + +let bisectLeft = (arr, x, lo, hi)=>{ + while (lo < hi) { + const mid = Math.floor((lo + hi) / 2); + if (arr[mid] < x) { + lo = mid + 1; + } else { + hi = mid; + } + } + return lo; +} From 4c07d50368bcb4d068eb9b743e413d68f4a26bc8 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Sat, 23 Nov 2024 10:39:35 +0530 Subject: [PATCH 5/5] Create Solution.js --- .../1861.Rotating the Box/Solution.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 solution/1800-1899/1861.Rotating the Box/Solution.js diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.js b/solution/1800-1899/1861.Rotating the Box/Solution.js new file mode 100644 index 0000000000000..9cb07c66a2d16 --- /dev/null +++ b/solution/1800-1899/1861.Rotating the Box/Solution.js @@ -0,0 +1,32 @@ +/** + * @param {character[][]} box + * @return {character[][]} + */ +var rotateTheBox = function(box) { + const m = box.length; + const n = box[0].length; + const ans = Array.from({ length: n }, () => Array(m).fill(null)); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans[j][m - i - 1] = box[i][j]; + } + } + + for (let j = 0; j < m; j++) { + const q = []; + for (let i = n - 1; i >= 0; i--) { + if (ans[i][j] === '*') { + q.length = 0; + } else if (ans[i][j] === '.') { + q.push(i); + } else if (q.length > 0) { + ans[q.shift()][j] = '#'; + ans[i][j] = '.'; + q.push(i); + } + } + } + + return ans; +};