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. |
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} | |
| */ | |
| var concatenatedBinary = function(n) { | |
| const MOD = 1_000_000_007; | |
| let ans = 0; | |
| let bitLength = 0; | |
| let pow2 = 1; // this will always store 2^bitLength % MOD |
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 | |
| * @param {number} k | |
| * @return {number} | |
| * | |
| * This solution is based on analyzing how many zeros can be flipped per operation. | |
| * Each operation flips exactly k bits, so the parity of the zero count changes | |
| * depending on k. The editorial shows that the minimum number of operations can be | |
| * computed using two candidate formulas depending on parity constraints. | |
| */ |
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[]} arr | |
| * @return {number[]} | |
| */ | |
| var sortByBits = function(arr) { | |
| // Helper: count how many 1-bits are in the binary representation | |
| function bitCount(n) { | |
| // Convert to binary string and count '1' characters | |
| return n.toString(2).split('1').length - 1; | |
| } |
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
| /** | |
| * Definition for a binary tree node. | |
| * function TreeNode(val, left, right) { | |
| * this.val = (val===undefined ? 0 : val) | |
| * this.left = (left===undefined ? null : left) | |
| * this.right = (right===undefined ? null : right) | |
| * } | |
| */ | |
| /** | |
| * @param {TreeNode} root |
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 | |
| * @param {number} k | |
| * @return {boolean} | |
| */ | |
| var hasAllCodes = function(s, k) { | |
| // Total number of binary codes of length k | |
| const needed = 1 << k; // same as Math.pow(2, k) | |
| // Quick fail: if s is too short to contain all codes |
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} | |
| */ | |
| var binaryGap = function(n) { | |
| // Convert the number to its binary string representation. | |
| // Example: 22 -> "10110" | |
| const binary = n.toString(2); | |
| // This will store the index of the *previous* '1' we saw. |
NewerOlder