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 7 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;
};
47 changes: 47 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @param {number[][]} pairs
* @return {number[][]}
*/
var validArrangement = function(pairs) {
const graph = new Map();
const indegree = new Map();
const outdegree = new Map();

for (const [start, end] of pairs) {
if (!graph.has(start)) graph.set(start, []);
graph.get(start).push(end);

outdegree.set(start, (outdegree.get(start) || 0) + 1);
indegree.set(end, (indegree.get(end) || 0) + 1);
}

let startNode = pairs[0][0];
for (const [node, out] of outdegree) {
const inCount = indegree.get(node) || 0;
if (out - inCount === 1) {
startNode = node;
break;
}
}

const result = [];
const stack = [startNode];

while (stack.length) {
const node = stack[stack.length - 1];
if (graph.has(node) && graph.get(node).length > 0) {
stack.push(graph.get(node).pop());
} else {
result.push(stack.pop());
}
}

result.reverse();

const output = [];
for (let i = 1; i < result.length; i++) {
output.push([result[i - 1], result[i]]);
}

return output;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class MinHeap {
constructor() {
this.heap = [];
}

push(element) {
this.heap.push(element);
this._heapifyUp();
}

pop() {
if (this.size() === 1) return this.heap.pop();
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this._heapifyDown();
return top;
}

peek() {
return this.heap[0];
}

size() {
return this.heap.length;
}

_heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[index][0] >= this.heap[parentIndex][0]) break;
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
}
}

_heapifyDown() {
let index = 0;
const length = this.heap.length;
while (true) {
const leftChild = 2 * index + 1;
const rightChild = 2 * index + 2;
let smallest = index;

if (leftChild < length && this.heap[leftChild][0] < this.heap[smallest][0]) {
smallest = leftChild;
}
if (rightChild < length && this.heap[rightChild][0] < this.heap[smallest][0]) {
smallest = rightChild;
}
if (smallest === index) break;

[this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
index = smallest;
}
}
}

/**
* @param {number[][]} grid
* @return {number}
*/
var minimumTime = function(grid) {
const m = grid.length;
const n = grid[0].length;

if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const dist = Array.from({ length: m }, () => Array(n).fill(Infinity));
dist[0][0] = 0;

const pq = new MinHeap();
pq.push([0, 0, 0]);

const directions = [-1, 0, 1, 0, -1];

while (pq.size() > 0) {
const [t, i, j] = pq.pop();

if (i === m - 1 && j === n - 1) return t;

for (let d = 0; d < 4; d++) {
const x = i + directions[d];
const y = j + directions[d + 1];

if (x >= 0 && x < m && y >= 0 && y < n) {
let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + (grid[x][y] - nt) % 2;
}

if (nt < dist[x][y]) {
dist[x][y] = nt;
pq.push([nt, x, y]);
}
}
}
}

return -1;
};
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('');
};