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[]} nums | |
| * @param {number} k | |
| * @return {boolean} | |
| */ | |
| var kLengthApart = function(nums, k) { | |
| // Keep track of the index of the last '1' we saw | |
| let lastOneIndex = -1; | |
| // Loop through the array |
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 numSub = function(s) { | |
| const MOD = 1_000_000_007; // Large prime for modulo operations | |
| let ans = 0; // Final answer accumulator | |
| let run = 0; // Current streak length of consecutive '1's | |
| for (let i = 0; i < s.length; i++) { |
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} | |
| */ | |
| let numberOfSubstrings = function (s) { | |
| const n = s.length; | |
| // dp[i] stores the nearest index <= i where a '0' occurs | |
| // (or -1 if none). This helps us quickly jump backwards | |
| // to substrings that include more zeros. |
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[][]} queries | |
| * @return {number[][]} | |
| */ | |
| var rangeAddQueries = function(n, queries) { | |
| // Step 1: Initialize an n x n matrix filled with 0s | |
| let mat = Array.from({ length: n }, () => Array(n).fill(0)); | |
| // Step 2: Process each query |
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
| /** | |
| * Calculates the maximum number of operations that can be performed | |
| * on a binary string `s`. An operation is defined as pairing a '1' | |
| * with the end of a block of consecutive '0's. | |
| * | |
| * @param {string} s - Input binary string consisting of '0' and '1' | |
| * @return {number} - Maximum number of operations possible | |
| */ | |
| var maxOperations = function (s) { | |
| const n = s.length; // Length of 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[]} nums | |
| * @return {number} | |
| */ | |
| var minOperations = function(nums) { | |
| // Step 1: Quick checks for trivial cases | |
| // If the array already contains a 1, spreading it is possible. | |
| // If the gcd of the entire array > 1, it's impossible to ever reach 1. | |
| const gcd = (a, b) => { |
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[]} strs | |
| * @param {number} m | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var findMaxForm = function(strs, m, n) { | |
| // Step 1: Initialize a 2D DP array | |
| // dp[i][j] = max number of strings we can form with i zeros and j ones | |
| let dp = Array.from({ length: m + 1 }, () => Array(n + 1).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
| /** | |
| * Calculates the minimum number of operations needed to build a strictly increasing stack | |
| * from the input array, ignoring zeros and removing elements that break the order. | |
| * | |
| * @param {number[]} nums - Array of integers to process. | |
| * @return {number} - Minimum number of operations performed. | |
| */ | |
| var minOperations = function(nums) { | |
| const stack = []; // Stack to maintain increasing sequence | |
| let operations = 0; // Counter for operations performed |
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} num1 | |
| * @param {number} num2 | |
| * @return {number} | |
| */ | |
| var countOperations = function(num1, num2) { | |
| // initialize a counter to keep track of the number of operations | |
| let operations = 0; | |
| // Continue looping until either num1 or num2 becomes 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
| /** | |
| * Calculates the minimum number of operations to reduce a number `n` to 0 | |
| * using a specific bitwise operation rule: | |
| * - You can flip the lowest 1-bit and all bits to its right. | |
| * | |
| * This function uses a recursive approach based on the properties of Gray code. | |
| * | |
| * @param {number} n - The input number. | |
| * @return {number} - Minimum number of operations to reduce `n` to 0. | |
| */ |
NewerOlder