From 74c5827979ca40a06cca8467df6a955391c3467e Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:48:09 +0530 Subject: [PATCH 01/11] feat: add solutions to lc problem: No.0885 --- .../0885.Spiral Matrix III/Solution.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 solution/0800-0899/0885.Spiral Matrix III/Solution.js diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.js b/solution/0800-0899/0885.Spiral Matrix III/Solution.js new file mode 100644 index 0000000000000..4e068bb419be4 --- /dev/null +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.js @@ -0,0 +1,34 @@ +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function(rows, cols, rStart, cStart) { + let result = []; + let totalCells = rows * cols; + let directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; + let step = 0; + let d = 0; + let r = rStart, c = cStart; + + result.push([r, c]); + + while (result.length < totalCells) { + if (d === 0 || d === 2) step++; + + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + + if (r >= 0 && r < rows && c >= 0 && c < cols) { + result.push([r, c]); + } + } + + d = (d + 1) % 4; + } + + return result; +}; From 25caf403b3ded8467de73d183fb82eaf8ad1867d Mon Sep 17 00:00:00 2001 From: AE-Hertz Date: Thu, 8 Aug 2024 05:43:45 +0000 Subject: [PATCH 02/11] style: format code and docs with prettier --- .../0885.Spiral Matrix III/Solution.js | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.js b/solution/0800-0899/0885.Spiral Matrix III/Solution.js index 4e068bb419be4..79a0f3dee9b3a 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/Solution.js +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.js @@ -5,30 +5,36 @@ * @param {number} cStart * @return {number[][]} */ -var spiralMatrixIII = function(rows, cols, rStart, cStart) { +var spiralMatrixIII = function (rows, cols, rStart, cStart) { let result = []; let totalCells = rows * cols; - let directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; + let directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; let step = 0; - let d = 0; - let r = rStart, c = cStart; - + let d = 0; + let r = rStart, + c = cStart; + result.push([r, c]); - + while (result.length < totalCells) { if (d === 0 || d === 2) step++; - + for (let i = 0; i < step; i++) { r += directions[d][0]; c += directions[d][1]; - + if (r >= 0 && r < rows && c >= 0 && c < cols) { result.push([r, c]); } } - + d = (d + 1) % 4; } - + return result; }; From a759f9c3e488420e0401979f43c80661e03ba864 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 8 Aug 2024 14:10:13 +0800 Subject: [PATCH 03/11] Update Solution.js --- .../0885.Spiral Matrix III/Solution.js | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.js b/solution/0800-0899/0885.Spiral Matrix III/Solution.js index 79a0f3dee9b3a..01f66eeb78e6c 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/Solution.js +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.js @@ -6,9 +6,9 @@ * @return {number[][]} */ var spiralMatrixIII = function (rows, cols, rStart, cStart) { - let result = []; - let totalCells = rows * cols; - let directions = [ + const ans = []; + const totalCells = rows * cols; + const directions = [ [0, 1], [1, 0], [0, -1], @@ -16,25 +16,20 @@ var spiralMatrixIII = function (rows, cols, rStart, cStart) { ]; let step = 0; let d = 0; - let r = rStart, - c = cStart; - - result.push([r, c]); - - while (result.length < totalCells) { - if (d === 0 || d === 2) step++; - + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } for (let i = 0; i < step; i++) { r += directions[d][0]; c += directions[d][1]; - if (r >= 0 && r < rows && c >= 0 && c < cols) { - result.push([r, c]); + ans.push([r, c]); } } - d = (d + 1) % 4; } - - return result; + return ans; }; From 4a49614c5b6e93855cbc0933c13dd206797d41b9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 8 Aug 2024 14:10:57 +0800 Subject: [PATCH 04/11] Update README.md --- .../0885.Spiral Matrix III/README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/solution/0800-0899/0885.Spiral Matrix III/README.md b/solution/0800-0899/0885.Spiral Matrix III/README.md index 746fc2d2703ba..db39ac38dcf8e 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README.md @@ -173,6 +173,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### JavaScript + +```js +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function (rows, cols, rStart, cStart) { + const ans = []; + const totalCells = rows * cols; + const directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + let step = 0; + let d = 0; + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + if (r >= 0 && r < rows && c >= 0 && c < cols) { + ans.push([r, c]); + } + } + d = (d + 1) % 4; + } + return ans; +}; +``` + From 549de7360aa07d6d1889ade7509127502b7e2d8f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 8 Aug 2024 14:11:10 +0800 Subject: [PATCH 05/11] Update README_EN.md --- .../0885.Spiral Matrix III/README_EN.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/solution/0800-0899/0885.Spiral Matrix III/README_EN.md b/solution/0800-0899/0885.Spiral Matrix III/README_EN.md index 42e57339a9b0e..0408075aab7df 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README_EN.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README_EN.md @@ -169,6 +169,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### JavaScript + +```js +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function (rows, cols, rStart, cStart) { + const ans = []; + const totalCells = rows * cols; + const directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + let step = 0; + let d = 0; + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + if (r >= 0 && r < rows && c >= 0 && c < cols) { + ans.push([r, c]); + } + } + d = (d + 1) % 4; + } + return ans; +}; +``` + From a907914ad3ba9b5d2750e94210bda8d9a4b6acd0 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Fri, 9 Aug 2024 11:06:33 +0530 Subject: [PATCH 06/11] feat: add solutions to lc project No. 0840 --- .../0840.Magic Squares In Grid/Solution.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 solution/0800-0899/0840.Magic Squares In Grid/Solution.js diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution.js b/solution/0800-0899/0840.Magic Squares In Grid/Solution.js new file mode 100644 index 0000000000000..c0b1d40f33a6b --- /dev/null +++ b/solution/0800-0899/0840.Magic Squares In Grid/Solution.js @@ -0,0 +1,40 @@ +/** + * @param {number[][]} grid + * @return {number} + */ +var numMagicSquaresInside = function(grid) { + const isMagic = (i, j) => { + const s = new Set(); + for (let x = 0; x < 3; x++) { + for (let y = 0; y < 3; y++) { + const num = grid[i + x][j + y]; + if (num < 1 || num > 9 || s.has(num)) return false; + s.add(num); + } + } + + const rowSum = grid[i][j] + grid[i][j+1] + grid[i][j+2]; + if (rowSum !== grid[i+1][j] + grid[i+1][j+1] + grid[i+1][j+2]) return false; + if (rowSum !== grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2]) return false; + + const colSum1 = grid[i][j] + grid[i+1][j] + grid[i+2][j]; + const colSum2 = grid[i][j+1] + grid[i+1][j+1] + grid[i+2][j+1]; + const colSum3 = grid[i][j+2] + grid[i+1][j+2] + grid[i+2][j+2]; + if (rowSum !== colSum1 || rowSum !== colSum2 || rowSum !== colSum3) return false; + + if (rowSum !== grid[i][j] + grid[i+1][j+1] + grid[i+2][j+2]) return false; + if (rowSum !== grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j]) return false; + + return true; + }; + + let count = 0; + + for (let i = 0; i < grid.length - 2; i++) { + for (let j = 0; j < grid[0].length - 2; j++) { + if (isMagic(i, j)) count++; + } + } + + return count; +}; From 6b698bb5826d5604f4fcc4b89771e6267a69961b Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Fri, 9 Aug 2024 11:20:43 +0530 Subject: [PATCH 07/11] Delete solution/0800-0899/0840.Magic Squares In Grid/Solution.js --- .../0840.Magic Squares In Grid/Solution.js | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 solution/0800-0899/0840.Magic Squares In Grid/Solution.js diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution.js b/solution/0800-0899/0840.Magic Squares In Grid/Solution.js deleted file mode 100644 index c0b1d40f33a6b..0000000000000 --- a/solution/0800-0899/0840.Magic Squares In Grid/Solution.js +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @param {number[][]} grid - * @return {number} - */ -var numMagicSquaresInside = function(grid) { - const isMagic = (i, j) => { - const s = new Set(); - for (let x = 0; x < 3; x++) { - for (let y = 0; y < 3; y++) { - const num = grid[i + x][j + y]; - if (num < 1 || num > 9 || s.has(num)) return false; - s.add(num); - } - } - - const rowSum = grid[i][j] + grid[i][j+1] + grid[i][j+2]; - if (rowSum !== grid[i+1][j] + grid[i+1][j+1] + grid[i+1][j+2]) return false; - if (rowSum !== grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2]) return false; - - const colSum1 = grid[i][j] + grid[i+1][j] + grid[i+2][j]; - const colSum2 = grid[i][j+1] + grid[i+1][j+1] + grid[i+2][j+1]; - const colSum3 = grid[i][j+2] + grid[i+1][j+2] + grid[i+2][j+2]; - if (rowSum !== colSum1 || rowSum !== colSum2 || rowSum !== colSum3) return false; - - if (rowSum !== grid[i][j] + grid[i+1][j+1] + grid[i+2][j+2]) return false; - if (rowSum !== grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j]) return false; - - return true; - }; - - let count = 0; - - for (let i = 0; i < grid.length - 2; i++) { - for (let j = 0; j < grid[0].length - 2; j++) { - if (isMagic(i, j)) count++; - } - } - - return count; -}; From cd8dc691ed41bfafd08fa8345b82daea6bac7c7a Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Sat, 10 Aug 2024 15:39:11 +0530 Subject: [PATCH 08/11] feat: Solution Js for lc no. 959 --- .../0959.Regions Cut By Slashes/Solution.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 solution/0900-0999/0959.Regions Cut By Slashes/Solution.js diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js new file mode 100644 index 0000000000000..253209260861c --- /dev/null +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js @@ -0,0 +1,52 @@ +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = (x) => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; + +} From 074ba4005c95460a34a9b0e057e82335fe80522c Mon Sep 17 00:00:00 2001 From: AE-Hertz Date: Sat, 10 Aug 2024 10:32:59 +0000 Subject: [PATCH 09/11] style: format code and docs with prettier --- solution/0900-0999/0959.Regions Cut By Slashes/Solution.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js index 253209260861c..93275ea7a9b91 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js @@ -4,7 +4,7 @@ */ function regionsBySlashes(grid) { - const find = (x) => { + const find = x => { if (p[x] !== x) { p[x] = find(p[x]); } @@ -48,5 +48,4 @@ function regionsBySlashes(grid) { } return size; - } From 1efac89d980bab0535c09a876c7f371e518d1216 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 10 Aug 2024 20:55:06 +0800 Subject: [PATCH 10/11] Update README.md --- .../0959.Regions Cut By Slashes/README.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README.md b/solution/0900-0999/0959.Regions Cut By Slashes/README.md index 6c7ce33e17e65..313507c727c20 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README.md @@ -279,6 +279,62 @@ func regionsBySlashes(grid []string) int { } ``` +#### JavaScript + +```js +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = x => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` + From d08b3b30c14eb8c623cc050c16a6d4a2fbe8284f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 10 Aug 2024 20:55:29 +0800 Subject: [PATCH 11/11] Update README_EN.md --- .../0959.Regions Cut By Slashes/README_EN.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md index 826f54d8a3194..33990633ac8fb 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md @@ -269,6 +269,62 @@ func regionsBySlashes(grid []string) int { } ``` +#### JavaScript + +```js +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = x => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` +