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 / 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
@tatsuyax25
tatsuyax25 / isTrionic.js
Created February 3, 2026 20:27
You are given an integer array nums of length n. An array is trionic if there exist indices 0 < p < q < n − 1 such that: nums[0...p] is strictly increasing, nums[p...q] is strictly decreasing, nums[q...n − 1] is strictly increasing. Return true if
/**
* @param {number[]} nums
* @return {boolean}
*/
var isTrionic = function(nums) {
const n = nums.length;
// Need at least 4 elements to form inc → dec → inc
if (n < 4) return false;
@tatsuyax25
tatsuyax25 / minimumCost.js
Created February 2, 2026 21:16
You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist. The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3. You need to divide num
/**
* @param {number[]} nums
* @param {number} k
* @param {number} dist
* @return {number}
*/
var minimumCost = function(nums, k, dist) {
const n = nums.length;
const INF = Number.MAX_SAFE_INTEGER;
@tatsuyax25
tatsuyax25 / minimumCost.js
Last active February 1, 2026 18:06
You are given an array of integers nums of length n. The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3. You need to divide nums into 3 disjoint contiguous subarrays. Return
/**
* @param {number[]} nums
* @return {number}
*/
var minimumCost = function(nums) {
const n = nums.length;
// We must split into 3 subarrays, so n must be at least 3.
// If n == 3, there's no choice: each element starts a subarray.
if (n === 3) {