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 / countPartitions.js
Created December 5, 2025 17:35
You are given an integer array nums of length n. A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two non-empty subarrays such that: Left subarray contains indices [0, i]. Right subarray contains indices [i + 1, n
/**
* @param {number[]} nums
* @return {number}
*/
var countPartitions = function(nums) {
// Step 1: Compute the total sum of the array.
// We'll use this to quickly calculate the right subarray sum at each partition.
let totalSum = 0;
for (let num of nums) {
totalSum += num;
@tatsuyax25
tatsuyax25 / countCollisions.js
Created December 4, 2025 17:18
There are n cars on an infinitely long road. The cars are numbered from 0 to n - 1 from left to right and each car is present at a unique point. You are given a 0-indexed string directions of length n. directions[i] can be either 'L', 'R', or 'S' de
/**
* @param {string} directions
* @return {number}
*/
var countCollisions = function(directions) {
// Step 1: Convert string to array for easier handling
let arr = directions.split('');
// Step 2: Remove cars that will never collide
// Trim leading 'L' cars (they move left off the road)
@tatsuyax25
tatsuyax25 / countTrapezoids.js
Created December 3, 2025 18:47
You are given a 2D integer array points where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane. Return the number of unique trapezoids that can be formed by choosing any four distinct points from points. A tra
/**
* Count the number of unique trapezoids that can be formed
* from a set of points on the Cartesian plane.
*
* @param {number[][]} points - Array of [x, y] coordinates
* @return {number} - Number of trapezoids
*/
var countTrapezoids = function(points) {
const t = new Map(); // Tracks lines grouped by slope (normalized)
const v = new Map(); // Tracks lines grouped by raw vector direction
@tatsuyax25
tatsuyax25 / countTrapezoids.js
Created December 2, 2025 17:38
You are given a 2D integer array points, where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane. A horizontal trapezoid is a convex quadrilateral with at least one pair of horizontal sides (i.e. parallel to the
/**
* @param {number[][]} points
* @return {number}
*/
var countTrapezoids = function(points) {
const MOD = 1e9 + 7;
// Step 1: Group points by y-coordinate
let yGroups = new Map();
for (let [x, y] of points) {
@tatsuyax25
tatsuyax25 / maxRunTime.js
Created December 1, 2025 18:46
You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries. Init
/**
* Calculates the maximum running time for `n` computers
* given a set of batteries with different capacities.
*
* @param {number} n - Number of computers
* @param {number[]} batteries - Array of battery capacities
* @return {number} - Maximum possible running time
*/
var maxRunTime = function(n, batteries) {
// Step 1: Compute the total available power across all batteries
@tatsuyax25
tatsuyax25 / minOperations.js
Created November 29, 2025 17:05
You are given an integer array nums and an integer k. You can perform the following operation any number of times: Select an index i and replace nums[i] with nums[i] - 1. Return the minimum number of operations required to make the sum of the array
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minOperations = function(nums, k) {
// Step 1: Calculate the sum of the array
let sum = 0;
for (let num of nums) {
sum += num;
@tatsuyax25
tatsuyax25 / maxSubarraySum.js
Created November 27, 2025 17:24
You are given an array of integers nums and an integer k. Return the maximum sum of a subarray of nums, such that the size of the subarray is divisible by k.
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxSubarraySum = function(nums, k) {
let n = nums.length;
// Step 1: Build prefix sums
let prefix = new Array(n + 1).fill(0);
@tatsuyax25
tatsuyax25 / numberOfPaths.js
Created November 26, 2025 21:27
You are given a 0-indexed m x n integer matrix grid and an integer k. You are currently at position (0, 0) and you want to reach position (m - 1, n - 1) moving only down or right. Return the number of paths where the sum of the elements on the path
/**
* @param {number[][]} grid
* @param {number} k
* @return {number}
*/
var numberOfPaths = function(grid, k) {
const MOD = 1e9 + 7; // modulus for large answers
const m = grid.length; // number of rows
const n = grid[0].length; // number of columns
@tatsuyax25
tatsuyax25 / smallestRepunitDivByK.js
Last active November 25, 2025 21:22
Given a positive integer k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1. Return the length of n. If there is no such n, return -1. Note: n may not fit in a 64-bit sig
/**
* @param {number} k
* @return {number}
*/
var smallestRepunitDivByK = function(k) {
// Step 1: Handle impossible cases
// Any number made only of '1's is odd and not divisible by 2 or 5.
// So if k has a factor of 2 or 5, return -1 immediately.
if (k % 2 === 0 || k % 5 === 0) {
return -1;
@tatsuyax25
tatsuyax25 / prefixesDivBy5.js
Created November 24, 2025 21:58
You are given a binary array nums (0-indexed). We define xi as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit). For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
/**
* @param {number[]} nums
* @return {boolean[]}
*/
var prefixesDivBy5 = function(nums) {
// Result array to store true/false for each prefix
let answer = [];
// We'll keep track of the current number modulo 5
// This avoids dealing with huge binary numbers directly