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) { |
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 isPowerOfTwo = function(n) { | |
// Powers of two must be positive integers greater than zero | |
if (n < 1) { | |
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 {number} n | |
* @return {number} | |
*/ | |
var soupServings = function(n) { | |
// Optimization: for large n, the probability approaches 1 | |
if (n >= 4800) return 1.0; | |
// Convert mL to units of 25 mL to reduce state space | |
const N = Math.ceil(n / 25); |
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[][]} fruits - 2D grid representing fruits in each room | |
* @return {number} - Maximum fruits collected by the three children | |
*/ | |
var maxCollectedFruits = function(fruits) { | |
const n = fruits.length; | |
// Step 1: Sum the main diagonal fruits (child A from top-left to bottom-right) | |
let diagonalSum = 0; | |
for (let i = 0; i < n; 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
/** | |
* Counts how many fruit types cannot be placed in any basket. | |
* Each fruit must go into the leftmost available basket with enough capacity. | |
* Each basket can hold only one fruit type. | |
* | |
* @param {number[]} fruits - Array of fruit quantities. | |
* @param {number[]} baskets - Array of basket capacities. | |
* @return {number} - Number of unplaced fruit types. | |
*/ | |
var numOfUnplacedFruits = function(fruits, baskets) { |
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[]} fruits | |
* @param {number[]} baskets | |
* @return {number} | |
*/ | |
var numOfUnplacedFruits = function(fruits, baskets) { | |
// Track basket usage using a Set to store used indices | |
const usedBaskets = new Set(); | |
// Track number of unplaced fruits |
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[]} fruits | |
* @return {number} | |
*/ | |
var totalFruit = function(fruits) { | |
let maxFruits = 0; | |
let left = 0; | |
const basket = new Map(); // Holds fruit type and count | |
// Move the right boundary of the window |