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 / maximumWeight.js
Created September 12, 2026 17:46
You are given a 2D integer array intervals, where intervals[i] = [li, ri, weighti]. Interval i starts at position li and ends at ri, and has a weight of weighti. You can choose up to 4 non-overlapping intervals. The score of the chosen intervals is d
/**
* @param {number[][]} intervals
* @return {number[]}
*/
var maximumWeight = function(intervals) {
const n = intervals.length;
// augment with original index
let arr = intervals.map((it, i) => ({ l: it[0], r: it[1], w: it[2], idx: i }));
@tatsuyax25
tatsuyax25 / totalNumbers.js
Created September 11, 2026 23:18
You are given an array of digits called digits. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits. Note: Each copy of a digit can only be used once per number, and there may not be leadin
/**
* @param {number[]} digits
* @return {number}
*/
var totalNumbers = function(digits) {
// Count how many times each digit appears in the input
const freq = Array(10).fill(0);
for (let d of digits) freq[d]++;
const result = new Set(); // store distinct valid numbers
@tatsuyax25
tatsuyax25 / averageOfSubtree.js
Created September 10, 2026 16:06
Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: The average of n elements is the sum of the n elements divided by n and rounded down to the nearest
/**
* 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 / countCommas.js
Created September 9, 2026 15:11
You are given an integer n. Return the total number of commas used when writing all integers from [1, n] (inclusive) in standard number formatting. In standard formatting: A comma is inserted after every three digits from the right. Numbers with f
/**
* @param {number} n
* @return {number}
*/
var countCommas = function(n) {
// Convert n to BigInt so all math stays consistent
n = BigInt(n);
// Use BigInt for total since we accumulate BigInt values
let total = 0n;
@tatsuyax25
tatsuyax25 / countCommas.js
Last active September 8, 2026 17:56
You are given an integer n. Return the total number of commas used when writing all integers from [1, n] (inclusive) in standard number formatting. In standard formatting: A comma is inserted after every three digits from the right. Numbers with f
/**
* @param {number} n
* @return {number}
*/
var countCommas = function(n) {
// Numbers from 1 to 999 never contain commas in standard formatting.
// Starting at 1000, every number has exactly ONE comma (e.g., "1,000", "4,582").
// So we simply count how many numbers from 1000 up to n exist.
// If n < 1000, the result should be 0 - hence Math.max(0, n - 999).
@tatsuyax25
tatsuyax25 / distinctSubseqII.js
Created September 7, 2026 16:38
Given a string s, return the number of distinct non-empty subsequences of s. Since the answer may be very large, return it modulo 109 + 7. A subsequence of a string is a new string that is formed from the original string by deleting some (can be non
/**
* @param {string} s
* @return {number}
*/
var distinctSubseqII = function(s) {
const MOD = 1_000_000_007;
const last = Array(26).fill(0);
let dp = 1; // counts empty subsequence initially
@tatsuyax25
tatsuyax25 / numDistinct.js
Created September 6, 2026 17:49
Given two strings s and t, return the number of distinct subsequences of s which equals t. The test cases are generated so that the answer fits on a 32-bit signed integer.
/**
* @param {string} s
* @param {string} t
* @return {number}
*/
var numDistinct = function(s, t) {
const m = s.length, n = t.length;
// dp[j] = number of ways to form t[0..j-1] using processed part of s
const dp = Array(n + 1).fill(0);
@tatsuyax25
tatsuyax25 / firstStableIndex.js
Created September 5, 2026 16:34
You are given an integer array nums of length n and an integer k. For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]). In other words: max(nums[0..i]) is the largest value among the elements from index 0 to inde
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var firstStableIndex = function(nums, k) {
const n = nums.length;
// Build suffix min
const suffMin = Array(n);
@tatsuyax25
tatsuyax25 / firstStableIndex.js
Created September 4, 2026 17:11
You are given an integer array nums of length n and an integer k. For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]). In other words: max(nums[0..i]) is the largest value among the elements from index 0 to inde
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var firstStableIndex = function(nums, k) {
const n = nums.length;
// Build prefix max
const prefixMax = Array(n);
@tatsuyax25
tatsuyax25 / uniformArray.js
Created September 3, 2026 16:33
You are given an array nums1 of n distinct integers. You want to construct another array nums2 of length n such that the elements in nums2 are either all odd or all even. For each index i, you must choose exactly one of the following (in any order)
/**
* @param {number[]} nums1
* @return {boolean}
*/
var uniformArray = function(nums1) {
nums1.sort((a, b) => a - b);
let smallestOdd = null;
let smallestEven = null;