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 / maxDotProduct.js
Created January 8, 2026 20:24
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none)
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var maxDotProduct = function(nums1, nums2) {
const m = nums1.length;
const n = nums2.length;
// Create a DP table where:
@tatsuyax25
tatsuyax25 / maxProduct.js
Last active January 7, 2026 21:38
Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return the maximum product of the sums of the two subtrees. Since the answer may be too lar
/**
* 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 / maxLevelSum.js
Created January 6, 2026 21:34
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
/**
* 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 / sumFourDivisors.js
Created January 4, 2026 19:40
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.
/**
* @param {number[]} nums
* @return {number}
*/
var sumFourDivisors = function(nums) {
// Helper: return the sum of divisors of n if it has exactly 4 divisors.
// Otherwise return 0.
function sumIfFourDivisors(n) {
let count = 0; // how many divisors we've found
let sum = 0; // sum of those divisors
@tatsuyax25
tatsuyax25 / numOfWays.js
Created January 3, 2026 18:20
You have a grid of size n x 3 and you want to paint each cell of the grid with exactly one of the three colors: Red, Yellow, or Green while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizont
/**
* @param {number} n
* @return {number}
*/
var numOfWays = function(n) {
const MOD = 1_000_000_007;
// a = number of ABC patterns for the current row
// b = number of ABA patterns for the current row
let a = 6; // ABC has 6 permutations
@tatsuyax25
tatsuyax25 / repeatedNTimes.js
Created January 2, 2026 19:23
You are given an integer array nums with the following properties: nums.length == 2 * n. nums contains n + 1 unique elements. Exactly one element of nums is repeated n times. Return the element that is repeated n times.
/**
* @param {number[]} nums
* @return {number}
*/
var repeatedNTimes = function(nums) {
// We'll use a Set to track which numbers we've seen.
const seen = new Set();
// Walk through each number in the array
for (let num of nums) {
@tatsuyax25
tatsuyax25 / plusOne.js
Created January 1, 2026 18:25
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
// Start from the last digit and move left
for (let i = digits.length - 1; i >= 0; i--) {
// If the current digit is less than 9, we can safely increment it
// and return immediately because no carry is needed.
@tatsuyax25
tatsuyax25 / latestDayToCross.js
Created December 31, 2025 18:37
There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively. Initially on day 0, the entire matrix is land. However, each
/**
* @param {number} row
* @param {number} col
* @param {number[][]} cells
* @return {number}
*/
var latestDayToCross = function(row, col, cells) {
// Helper: check if it's possible to cross on a given day
function canCross(day) {
// Build a grid where 1 = water, 0 = land
@tatsuyax25
tatsuyax25 / pyramidTransition.js
Created December 29, 2025 21:07
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains one less block than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there
/**
* @param {string} bottom
* @param {string[]} allowed
* @return {boolean}
*/
var pyramidTransition = function(bottom, allowed) {
// STEP 1: Build a mapping from (left,right) -> list of possible tops
// Example: "ABC" means A+B -> C
const map = new Map();
for (let pattern of allowed) {
@tatsuyax25
tatsuyax25 / countNegatives.js
Last active December 28, 2025 18:03
Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.
/**
* @param {number[][]} grid - A 2D matrix sorted in non-increasing order
* @return {number} - Total count of negative numbers in the matrix
*/
var countNegatives = function (grid) {
let count = 0;
// Loop through each row
for (let row = 0; row < grid.length; row++) {