Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Solution.js #3725

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions solution/0200-0299/0219.Contains Duplicate II/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 32 additions & 0 deletions solution/1800-1899/1861.Rotating the Box/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
};
33 changes: 33 additions & 0 deletions solution/3000-3099/3011.Find if Array Can Be Sorted/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions solution/3100-3199/3163.String Compression III/Solution.js
Original file line number Diff line number Diff line change
@@ -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('');
};
Loading