π¨βπ»
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
/** | |
* Finds the length of the longest subarray containing only 1s | |
* after deleting exactly one element (which can be a 0). | |
* | |
* @param {number[]} nums - Array of binary digits (0s and 1s) | |
* @return {number} - Length of the longest valid subarray | |
*/ | |
var longestSubarray = function(nums) { | |
let maxLength = 0; // Tracks the maximum length found | |
let left = 0; // Left pointer of the sliding window |
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
/** | |
* Finds the minimum total area of 3 non-overlapping rectangles | |
* that cover all the 1s in a binary grid. | |
* @param {number[][]} grid | |
* @return {number} | |
*/ | |
var minimumSum = function (grid) { | |
const m = grid.length; | |
const n = grid[0].length; | |
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
/** | |
* Finds the minimum total area of 3 non-overlapping rectangles | |
* that cover all the 1s in a binary grid. | |
* @param {number[][]} grid | |
* @return {number} | |
*/ | |
var minimumSum = function (grid) { | |
const m = grid.length; | |
const n = grid[0].length; | |
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[][]} grid | |
* @return {number} | |
*/ | |
var minimumArea = function(grid) { | |
// Initialize bounds to extreme values | |
let minRow = Infinity, maxRow = -Infinity; | |
let minCol = Infinity, maxCol = -Infinity; | |
// Traverse the grid to find the bounding box of all 1s |
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 numSubmat = function(mat) { | |
const rows = mat.length; | |
const cols = mat[0].length; | |
let total = 0; | |
// Step 1: Preprocess each row to build horizontal histograms |
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; |
NewerOlder