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 / countTriples.js
Created December 8, 2025 17:16
A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2. Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.
/**
* @param {number} n
* @return {number}
*/
var countTriples = function(n) {
// Initialize a counter to keep track of valid triples
let count = 0;
// Loop through all possible values of 'a'
for (let a = 1; a <= n; a++) {
@tatsuyax25
tatsuyax25 / countOdds.js
Created December 7, 2025 17:27
Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
/**
* @param {number} low
* @param {number} high
* @return {number}
*/
var countOdds = function(low, high) {
// Step 1: Think about what we want:
// We need to count how many odd numbers exist between low and high (inclusive).
// Step 2: Use math instead of looping:
@tatsuyax25
tatsuyax25 / countPartitions.js
Last active December 7, 2025 17:29
You are given an integer array nums and an integer k. Your task is to partition nums into one or more non-empty contiguous segments such that in each segment, the difference between its maximum and minimum elements is at most k. Return the total num
/**
* Count the number of ways to partition nums into contiguous segments
* such that in each segment, max - min <= k.
*
* @param {number[]} nums - Input array of integers
* @param {number} k - Maximum allowed difference between max and min in a segment
* @return {number} - Number of valid partitions modulo 1e9+7
*/
var countPartitions = function (nums, k) {
const MOD = 1e9 + 7;
@tatsuyax25
tatsuyax25 / countPartitions.js
Created December 6, 2025 19:22
You are given an integer array nums and an integer k. Your task is to partition nums into one or more non-empty contiguous segments such that in each segment, the difference between its maximum and minimum elements is at most k. Return the total num
/**
* Count the number of ways to partition nums into contiguous segments
* such that in each segment, max - min <= k.
*
* @param {number[]} nums - Input array of integers
* @param {number} k - Maximum allowed difference between max and min in a segment
* @return {number} - Number of valid partitions modulo 1e9+7
*/
var countPartitions = function (nums, k) {
const MOD = 1e9 + 7;
@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;