-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: Solution Js for lc no. 959 #3396
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
74c5827
feat: add solutions to lc problem: No.0885
AE-Hertz 18cd1ac
Merge pull request #2 from AE-Hertz/AE-Hertz-patch-solution-LC--0885
AE-Hertz 25caf40
style: format code and docs with prettier
AE-Hertz a759f9c
Update Solution.js
yanglbme 4a49614
Update README.md
yanglbme 549de73
Update README_EN.md
yanglbme 56fbe9e
Merge branch 'doocs:main' into main
AE-Hertz a907914
feat: add solutions to lc project No. 0840
AE-Hertz 5a244fc
Merge pull request #3 from AE-Hertz/AE-Hertz-patch-1
AE-Hertz 6b698bb
Delete solution/0800-0899/0840.Magic Squares In Grid/Solution.js
AE-Hertz d7972c3
Merge branch 'doocs:main' into main
AE-Hertz cd8dc69
feat: Solution Js for lc no. 959
AE-Hertz 074ba40
style: format code and docs with prettier
AE-Hertz 1efac89
Update README.md
yanglbme d08b3b3
Update README_EN.md
yanglbme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
solution/0900-0999/0959.Regions Cut By Slashes/Solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.