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
| /** | |
| * @param {number} n | |
| * @param {number[][]} edges | |
| * @param {number} k | |
| * @return {number} | |
| */ | |
| var maxStability = function (n, edges, k) { | |
| // ----------------------------- | |
| // Disjoint Set Union (Union-Find) | |
| // ----------------------------- |
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
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var bitwiseComplement = function(n) { | |
| // Edge case: |
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
| /** | |
| * @param {number} zero | |
| * @param {number} one | |
| * @param {number} limit | |
| * @return {number} | |
| */ | |
| var numberOfStableArrays = function(zero, one, limit) { | |
| const MOD = 1_000_000_007; | |
| // dp0[x][y] = ways using x zeros, y ones, ending with 0 |
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
| /** | |
| * @param {number} zero | |
| * @param {number} one | |
| * @param {number} limit | |
| * @return {number} | |
| */ | |
| var numberOfStableArrays = function (zero, one, limit) { | |
| const MOD = 1_000_000_007; | |
| // ------------------------------------------------------------ |
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
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var minFlips = function(s) { | |
| const n = s.length; | |
| const s2 = s + s; | |
| // Build alternating patterns of length 2n | |
| let alt0 = "", alt1 = ""; |
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
| /** | |
| * @param {string} s | |
| * @return {boolean} | |
| */ | |
| var checkOnesSegment = function(s) { | |
| // This flag will turn true once we hit the first '0' | |
| // after the initial block of ones. | |
| let seenZero = false; | |
| // Loop through each character in the string |
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
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var minOperations = function(s) { | |
| // cost0 = number of flips needed if we force pattern "010101..." | |
| // cost1 = number of flips needed if we force pattern "101010..." | |
| let cost0 = 0; | |
| let cost1 = 0; |
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
| /** | |
| * @param {number[][]} mat | |
| * @return {number} | |
| */ | |
| var numSpecial = function(mat) { | |
| const m = mat.length; | |
| const n = mat[0].length; | |
| // Count how many 1s appear in each row | |
| const rowCount = new Array(m).fill(0); |
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
| /** | |
| * @param {number[][]} grid | |
| * @return {number} | |
| */ | |
| var minSwaps = function(grid) { | |
| const n = grid.length; | |
| // Step 1: For each row, compute the index of the rightmost 1. | |
| // If a row has no 1s, store -1. | |
| const rightmost = new Array(n).fill(0); |
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
| /** | |
| * @param {string} n | |
| * @return {number} | |
| */ | |
| var minPartitions = function(n) { | |
| // We want the minimum number of deci-binary numbers needed. | |
| // A deci-binary number can only contribute 0 or 1 to each digit. | |
| // Therefore, the number of deci-binary numbers required is equal | |
| // to the maximum digit in the string. |
NewerOlder