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 000000000000..bf68ed04ea3e --- /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; +}; 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 000000000000..bb65c90f1e9c --- /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; +} 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 000000000000..9cb07c66a2d1 --- /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; +}; 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 000000000000..cf9552ee8e88 --- /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; +} 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 000000000000..9b08e0d39f6f --- /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(''); +};