π¨βπ»
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 zeroFilledSubarray = function(nums) { | |
let total = 0; // Final count of zero-filled subarrays | |
let count = 0; // Tracks length of current zero streak | |
for (let i = 0; i < nums.length; i++) { | |
if (nums[i] === 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[]} cards | |
* @return {boolean} | |
*/ | |
var judgePoint24 = function(cards) { | |
// Convert all numbers to floats for real division | |
const nums = cards.map(num => num * 1.0); | |
function backtrack(arr) { | |
// Base case: only one number left, check if it's close to 24 |
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} k | |
* @param {number} maxPts | |
* @return {number} | |
*/ | |
var new21Game = function(n, k, maxPts) { | |
// Edge case: if k == 0, Alice doesn't draw any cards, so score is 0 (always < n) | |
// Or if n >= k + maxPts, Alice can reach any score < n with certainty | |
if (k === 0 || n >= k + maxPts) return 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
/** | |
* @param {number} num | |
* @return {number} | |
*/ | |
var maximum69Number = function(num) { | |
// Convert the number into an array of digits | |
const digits = num.toString().split(''); | |
let maxNum = num; |
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 {boolean} | |
*/ | |
var isPowerOfFour = function(n) { | |
// Powers of four must be positive | |
if (n <= 0) return false; | |
// Check if n is a power of two: only one bit is set | |
if ((n & (n - 1)) !== 0) return false; |
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} num | |
* @return {string} | |
*/ | |
var largestGoodInteger = function(num) { | |
// Match all substrings of three identical digits using a regular expression | |
const matches = num.match(/(\d)\1{2}/g); | |
// If no matches are found, return an empty string | |
if (!matches) return ''; |
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 {boolean} | |
*/ | |
var isPowerOfThree = function(n) { | |
// Powers of 3 are always positive (3^0 = 1, 3^1 = 3, etc.) | |
if (n < 1) return false; | |
// Keep dividing n by 3 as long it's it's divisible | |
while (n % 3 === 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} n | |
* @param {number} x | |
* @return {number} | |
*/ | |
var numberOfWays = function(n, x) { | |
const MOD = 1e9 + 7; | |
// Memoization cache: key = `${total},${current}` | |
const memo = new Map(); |
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 productQueries = function(n, queries) { | |
const MOD = 1e9 + 7; | |
// Step 1: Decompose n into powers of 2 using its binary representation | |
const powers = []; |
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 {boolean} | |
*/ | |
var reorderedPowerOf2 = function(n) { | |
// Convert the number to an array of digit characters | |
const digits = n.toString().split(''); | |
// Helper function to generate all unique permutations of the digits | |
function getPermutations(arr) { |
NewerOlder