Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / addBinary.js
Created February 15, 2026 00:45
Given two binary strings a and b, return their sum as a binary string.
/**
* Adds two binary strings and returns their binary sum.
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
let result = ""; // final binary string (built from right to left)
let carry = 0; // carry bit (0 or 1)
@tatsuyax25
tatsuyax25 / champagneTower.js
Created February 14, 2026 20:05
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row. Each glass holds one cup of champagne. Then, some champagne is poured into the first glass at the top. When the topmost gl
/**
* @param {number} poured - Total cups of champagne poured into the top glass
* @param {number} query_row - Row of the target glass (0-indexed)
* @param {number} query_glass - Column of the target glass (0-indexed)
* @return {number} - Fullness of the target glass (0 to 1)
*/
var champagneTower = function(poured, query_row, query_glass) {
// We only need up to 100 rows, so create a 2D array of zeros.
// dp[r][c] represents the *amount of champagne that arrives* at glass (r, c),
// not capped at 1.
@tatsuyax25
tatsuyax25 / longestBalanced.js
Created February 13, 2026 18:44
You are given a string s consisting only of the characters 'a', 'b', and 'c'. A substring of s is called balanced if all distinct characters in the substring appear the same number of times. Return the length of the longest balanced substring of s.
/**
* @param {string} s
* @return {number}
*/
var longestBalanced = function(s) {
const n = s.length;
let ans = 0;
// 1) Case: only one distinct character (runs like "aaaa")
ans = Math.max(ans, longestSingleCharRun(s));
@tatsuyax25
tatsuyax25 / longestBalanced.js
Created February 12, 2026 21:15
You are given a string s consisting of lowercase English letters. A substring of s is called balanced if all distinct characters in the substring appear the same number of times. Return the length of the longest balanced substring of s.
/**
* @param {string} s
* @return {number}
*/
var longestBalanced = function(s) {
const n = s.length;
let maxLen = 0;
// Try every possible starting index i
for (let i = 0; i < n; i++) {
@tatsuyax25
tatsuyax25 / longestBalanced.js
Last active February 11, 2026 19:50
You are given an integer array nums. A subarray is called balanced if the number of distinct even numbers in the subarray is equal to the number of distinct odd numbers. Return the length of the longest balanced subarray.
/**
* Longest Balanced Subarray:
* A subarray is balanced if the number of DISTINCT even values
* equals the number of DISTINCT odd values.
*
* This solution uses:
* - value compression
* - tracking first occurrences of each value
* - a segment tree with lazy propagation
* - sliding left boundary
@tatsuyax25
tatsuyax25 / longestBalanced.js
Created February 10, 2026 22:20
You are given an integer array nums. A subarray is called balanced if the number of distinct even numbers in the subarray is equal to the number of distinct odd numbers. Return the length of the longest balanced subarray.
/**
* @param {number[]} nums
* @return {number}
*/
var longestBalanced = function(nums) {
const n = nums.length;
let maxLen = 0;
// Fix a starting index i
for (let i = 0; i < n; i++) {
@tatsuyax25
tatsuyax25 / isBalanced.js
Created February 8, 2026 19:54
Given a binary tree, determine if it is height-balanced.
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
@tatsuyax25
tatsuyax25 / minRemoval.js
Created February 6, 2026 20:20
You are given an integer array nums and an integer k. An array is considered balanced if the value of its maximum element is at most k times the minimum element. You may remove any number of elements from nums​​​​​​​ without making it empty. Retur
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minRemoval = function(nums, k) {
// Step 1: Sort the array so window are contiguous and ordered
nums.sort((a, b) => a - b);
let n = nums.length;
@tatsuyax25
tatsuyax25 / constructTransformedArray.js
Created February 5, 2026 17:49
You are given an integer array nums that represents a circular array. Your task is to create a new array result of the same size, following these rules: For each index i (where 0 <= i < nums.length), perform the following independent actions: If num
/**
* @param {number[]} nums
* @return {number[]}
*/
var constructTransformedArray = function(nums) {
const n = nums.length;
const result = new Array(n);
for (let i = 0; i < n; i++) {
const steps = nums[i];
@tatsuyax25
tatsuyax25 / maxSumTrionic.js
Created February 4, 2026 17:41
You are given an integer array nums of length n. A trionic subarray is a contiguous subarray nums[l...r] (with 0 <= l < r < n) for which there exist indices l < p < q < r such that: nums[l...p] is strictly increasing, nums[p...q] is strictly decrea
/**
* @param {number[]} nums
* @return {number}
*
* This solution uses top‑down DP with memoization.
* We track 4 phases (k = 0..3) of a trionic subarray:
*
* 0 → before starting the first increasing phase
* 1 → strictly increasing
* 2 → strictly decreasing