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 / 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
@tatsuyax25
tatsuyax25 / maxSumDivThree.js
Created November 23, 2025 17:31
Given an integer array nums, return the maximum possible sum of elements of the array such that it is divisible by three.
/**
* @param {number[]} nums
* @return {number}
*/
var maxSumDivThree = function(nums) {
// Step 1: Calculate the total sum of the array
let totalSum = nums.reduce((acc, num) => acc + num, 0);
// Step 2: If already divisible by 3, return it
if (totalSum % 3 === 0) return totalSum;
@tatsuyax25
tatsuyax25 / minimumOperations.js
Last active November 22, 2025 17:09
You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums. Return the minimum number of operations to make all elements of nums divisible by 3.
/**
* @param {number[]} nums
* @return {number}
*/
var minimumOperations = function(nums) {
// Initialize a counter to keep track of total operations
let operations = 0;
// Loop through each number in the array
for (let num of nums) {
@tatsuyax25
tatsuyax25 / intersectionSizeTwo.js
Created November 20, 2025 21:06
You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively. A containing set is an array nums where each interval from intervals has at least two integers in nums. For
/**
* Finds the minimum number of integers needed so that
* each interval contains at least two chosen numbers.
*
* @param {number[][]} intervals - Array of [start, end] intervals
* @return {number} - Minimum number of integers chosen
*/
var intersectionSizeTwo = function(intervals) {
// Step 1: Sort intervals
// - Primary: by end ascending (smaller right boundary first)
@tatsuyax25
tatsuyax25 / findFinalValue.js
Created November 19, 2025 17:27
You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums. You then do the following steps: If original is found in nums, multiply it by two (i.e., set original =
/**
* @param {number[]} nums
* @param {number} original
* @return {number}
*/
var findFinalValue = function(nums, original) {
// Step 1: Convert nums into a Set for faster lookups.
// Why? Searching in an array is O(n), but in a Set it's O(1).
let numSet = new Set(nums);
@tatsuyax25
tatsuyax25 / isOneBitCharacter.js
Created November 18, 2025 21:57
We have two special characters: The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Given a binary array bits that ends with 0, return true if the last character must be a one-bit char
/**
* @param {number[]} bits
* @return {boolean}
*/
var isOneBitCharacter = function(bits) {
// Start from the first bit
let i = 0;
// Traverse until the second-to-last bit
// (because the last bit is always 0, we want to see if it's consumed or not)
@tatsuyax25
tatsuyax25 / kLengthApart.js
Created November 17, 2025 16:42
Given an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var kLengthApart = function(nums, k) {
// Keep track of the index of the last '1' we saw
let lastOneIndex = -1;
// Loop through the array