Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / longestSubarray.js
Created August 24, 2025 16:38
Given a binary array nums, you should delete one element from it. Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.
/**
* 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
@tatsuyax25
tatsuyax25 / minimumSum.js
Created August 23, 2025 16:17
You are given a 2D binary array grid. You need to find 3 non-overlapping rectangles having non-zero areas with horizontal and vertical sides such that all the 1's in grid lie inside these rectangles. Return the minimum possible sum of the area of th
/**
* 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();
@tatsuyax25
tatsuyax25 / minimumSum.js
Created August 23, 2025 16:17
You are given a 2D binary array grid. You need to find 3 non-overlapping rectangles having non-zero areas with horizontal and vertical sides such that all the 1's in grid lie inside these rectangles. Return the minimum possible sum of the area of th
/**
* 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();
@tatsuyax25
tatsuyax25 / minimumArea.js
Created August 22, 2025 17:19
You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1's in grid lie inside this rectangle. Return the minimum possible area of the rectangle.
/**
* @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
@tatsuyax25
tatsuyax25 / numSubmat.js
Last active August 21, 2025 16:38
Given an m x n binary matrix mat, return the number of submatrices that have all ones.
/**
* @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
@tatsuyax25
tatsuyax25 / zeroFilledSubarray.js
Created August 19, 2025 20:30
Given an integer array nums, return the number of subarrays filled with 0. A subarray is a contiguous non-empty sequence of elements within an array.
/**
* @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) {
@tatsuyax25
tatsuyax25 / judgePoint24.js
Last active August 18, 2025 16:26
You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parenthes
/**
* @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
@tatsuyax25
tatsuyax25 / new21Game.js
Created August 17, 2025 17:09
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where ma
/**
* @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;
@tatsuyax25
tatsuyax25 / maximum69Number.js
Created August 16, 2025 17:52
You are given a positive integer num consisting only of digits 6 and 9. Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
/**
* @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;
@tatsuyax25
tatsuyax25 / isPowerOfFour.js
Created August 15, 2025 16:20
Given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is a power of four, if there exists an integer x such that n == 4x.
/**
* @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;