Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@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
@tatsuyax25
tatsuyax25 / deleteDuplicateFolder.js
Created July 20, 2025 18:06
Due to a bug, there are many duplicate folders in a file system. You are given a 2D array paths, where paths[i] is an array representing an absolute path to the ith folder in the file system. For example, ["one", "two", "three"] represents the path
/**
* @param {string[][]} paths
* @return {string[][]}
*/
// Trie node definition to build the folder structure
function TrieNode() {
this.next = new Map(); // maps folder name to child TrieNode
this.marked = false; // flag for duplicate detection
}
@tatsuyax25
tatsuyax25 / minimumDifference.js
Created July 18, 2025 17:00
You are given a 0-indexed integer array nums consisting of 3 * n elements. You are allowed to remove any subsequence of elements of size exactly n from nums. The remaining 2 * n elements will be divided into two equal parts: The first n elements be
/**
* @param {number[]} nums - Array containing 3n integers
* @return {number} - Minimum difference between two parts after removing n elements
*/
var minimumDifference = function(nums) {
const n = nums.length / 3;
// Max heap for prefix sums (we want the smallest n elements for left side)
let maxHeap = [];
let leftSum = 0;
@tatsuyax25
tatsuyax25 / maximumLength.js
Created July 17, 2025 20:30
You are given an integer array nums and a positive integer k. A subsequence sub of nums with length x is called valid if it satisfies: (sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k. Return the length of the l
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maximumLength = function(nums, k) {
const n = nums.length;
// dp[i][mod] = max length of valid subsequence ending at index i with mod class `mod`
const dp = Array.from({ length: n }, () => new Map());
let maxLength = 0;
@tatsuyax25
tatsuyax25 / maximumLength.js
Created July 16, 2025 17:39
You are given an integer array nums. A subsequence sub of nums with length x is called valid if it satisfies: (sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2. Return the length of the longest valid subsequence
/**
* @param {number[]} nums
* @return {number}
*/
var maximumLength = function(nums) {
const n = nums.length;
// Counters for how many times each "type" of parity combo appears
let oddodd = 0; // odd element followed by odd sum? (Not well defined here)
let eveneven = 0; // even element followed by even sum?
@tatsuyax25
tatsuyax25 / isValid.js
Created July 15, 2025 16:36
A word is considered valid if: It contains a minimum of 3 characters. It contains only digits (0-9), and English letters (uppercase and lowercase). It includes at least one vowel. It includes at least one consonant. You are given a string word. Ret
/**
* @param {string} word
* @return {boolean}
*/
var isValid = function(word) {
// βœ… Check if the word has at least 3 characters
if (word.length < 3) return false;
// βœ… Check if the word contains only English letters and digits
// This regular expression ensures all characters are alphanumeric
@tatsuyax25
tatsuyax25 / getDecimalValue.js
Created July 14, 2025 17:15
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. Return the decimal value of the number in the linked list. The mo
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number}
@tatsuyax25
tatsuyax25 / matchPlayersAndTrainers.js
Created July 13, 2025 15:49
You are given a 0-indexed integer array players, where players[i] represents the ability of the ith player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training capacity of the jth trainer. The ith player
/**
* @param {number[]} players
* @param {number[]} trainers
* @return {number}
*/
var matchPlayersAndTrainers = function(players, trainers) {
// Step 1: Sort both arrays
players.sort((a, b) => a - b);
trainers.sort((a, b) => a - b);
@tatsuyax25
tatsuyax25 / earliestAndLatest.js
Created July 12, 2025 17:19
There is a tournament where n players are participating. The players are standing in a single row and are numbered from 1 to n based on their initial standing position (player 1 is the first player in the row, player 2 is the second player in the row
/**
* Calculates the earliest and latest round two top players can meet in a knockout tournament.
* @param {number} n - Total number of players.
* @param {number} firstPlayer - The position of one top player.
* @param {number} secondPlayer - The position of the other top player.
* @return {number[]} - [earliestRound, latestRound]
*/
var earliestAndLatest = function(n, firstPlayer, secondPlayer) {
// Normalize player positions: ensure lower < upper
let lower = Math.min(firstPlayer, secondPlayer);
@tatsuyax25
tatsuyax25 / mostBooked.js
Last active July 11, 2025 16:15
You are given an integer n. There are n rooms numbered from 0 to n - 1. You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the value
/**
* @param {number} n
* @param {number[][]} meetings
* @return {number}
*/
var mostBooked = function(n, meetings) {
// Initialize arrays to track the number of bookings and the end times of the last meetings for each room
const count = new Array(n).fill(0);
const roomEndTimes = new Array(n).fill(0);