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 / maxTotalFruits.js
Created August 3, 2025 17:14
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, a
/**
* @param {number[][]} fruits
* @param {number} startPos
* @param {number} k
* @return {number}
*/
var maxTotalFruits = function(fruits, startPos, k) {
let max = 0;
let left = 0;
let sum = 0;
@tatsuyax25
tatsuyax25 / minCost.js
Created August 2, 2025 18:00
You have two fruit baskets containing n fruits each. You are given two 0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal. To do so, you can use the following operation as m
/**
* @param {number[]} basket1
* @param {number[]} basket2
* @return {number}
*/
var minCost = function(basket1, basket2) {
const freq1 = new Map();
const freq2 = new Map();
const combinedFreq = new Map();
const swaps = [];
@tatsuyax25
tatsuyax25 / generate.js
Created August 1, 2025 17:56
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
if (numRows <= 0) return [];
const result = [];
for (let i = 0; i < numRows; i++) {
@tatsuyax25
tatsuyax25 / subarrayBitwiseORs.js
Created July 31, 2025 15:45
Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr. The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
/**
* @param {number[]} arr
* @return {number}
*/
var subarrayBitwiseORs = function(arr) {
// A set to store all unique OR results
const resultSet = new Set();
// This set will track ORs of subarrays ending at the current index
let currentSet = new Set();
@tatsuyax25
tatsuyax25 / smallestSubarrays.js
Last active July 29, 2025 19:23
You are given a 0-indexed array nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possib
/**
* @param {number[]} nums
* @return {number[]}
*/
var smallestSubarrays = function(nums) {
const n = nums.length;
const answer = new Array(n).fill(1); // Result array
const latestSeenBit = new Array(32).fill(-1); // Tracks last index each bit is seen
for (let i = n - 1; i >= 0; i--) {
@tatsuyax25
tatsuyax25 / countHillValley.js
Created July 27, 2025 17:21
You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger
/**
* @param {number[]} nums
* @return {number}
*/
var countHillValley = function(nums) {
// Step 1: Remove consecutive duplicates for easier neighbor comparisons
const filtered = [nums[0]];
for (let i = 1; i < nums.length; i++) {
if (nums[i] !== filtered[filtered.length - 1]) {
filtered.push(nums[i]);
@tatsuyax25
tatsuyax25 / maxSubarrays.js
Created July 26, 2025 16:30
You are given an integer n which represents an array nums containing the numbers from 1 to n in order. Additionally, you are given a 2D array conflictingPairs, where conflictingPairs[i] = [a, b] indicates that a and b form a conflicting pair. Remove
/**
* Calculates the maximum number of valid subarrays from 1 to n,
* excluding conflicting pairs (each conflict prevents both elements
* from being in the same subarray).
*
* @param {number} n - The size of the array (from 1 to n).
* @param {number[][]} conflictingPairs - Array of conflict pairs [u, v].
* @return {number} - Maximum number of valid subarrays.
*/
var maxSubarrays = function(n, conflictingPairs) {
@tatsuyax25
tatsuyax25 / maxSum.js
Last active July 25, 2025 16:09
You are given an integer array nums. You are allowed to delete any number of elements from nums without making it empty. After performing the deletions, select a subarray of nums such that: All elements in the subarray are unique. The sum of the el
/**
* Finds the maximum sum of unique positive numbers in the array.
* If no positive numbers are present, returns the largest (least negative) number.
* Note: This does not enforce subarray contiguity.
*
* @param {number[]} nums - Array of integers
* @return {number} - Maximum sum of unique positives or max negative value
*/
var maxSum = function(nums) {
// Boolean flags to track numbers from 1 to 100
@tatsuyax25
tatsuyax25 / minimumScore.js
Created July 24, 2025 19:00
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 0-indexed integer array nums of length n where nums[i] represents the value of the ith node. You are also given a 2D integer array edges of l
/**
* @param {number[]} nums
* @param {number[][]} edges
* @return {number}
*/
var minimumScore = function(nums, edges) {
const n = nums.length;
const tree = Array.from({ length: n }, () => []);
// \U0001f527 Build adjacency list for the tree
@tatsuyax25
tatsuyax25 / maximumUniqueSubarray.js
Created July 22, 2025 16:37
You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. Return the maximum score you can get by erasing exactly one suba
/**
* @param {number[]} nums
* @return {number}
*/
var maximumUniqueSubarray = function(nums) {
let seen = new Set(); // To track unique elements in the current window
let left = 0; // Left boundary of the sliding window
let maxScore = 0; // To Keep track of the maximum score found
let currentSum = 0; // Sum of the current window