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 / minOperations.js
Last active February 13, 2025 22:11
You are given a 0-indexed integer array nums, and an integer k. You are allowed to perform some operations on nums, where in a single operation, you can: Select the two smallest integers x and y from nums. Remove x and y from nums. Insert (min(x, y
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minOperations = function(nums, k) {
let queue = [], ans = 0, i = 0;
// Sort nums in descending order
nums.sort((a, b) => b - a);
@tatsuyax25
tatsuyax25 / maximumSum.js
Created February 12, 2025 17:35
You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j]. Return the maximum value of nums[i] + nums[j] that y
/**
* @param {number[]} nums
* @return {number}
*/
// Helper function to calculate the sum of digits of a given number
function sumOfDigits(num) {
let total = 0;
while (num > 0) {
total += num % 10;
num = Math.floor(num / 10);
@tatsuyax25
tatsuyax25 / removeOccurrences.js
Created February 11, 2025 17:40
Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed: Find the leftmost occurrence of the substring part and remove it from s. Return s after removing all occurrences of part. A
/**
* @param {string} s
* @param {string} part
* @return {string}
*/
var removeOccurrences = function(s, part) {
// Loop until there are no more occurrences of 'part' in 's'
while (s.includes(part)) {
// Replace the first occurrence of 'part' with an empty string
s = s.replace(part, "");
@tatsuyax25
tatsuyax25 / clearDigits.js
Created February 10, 2025 17:41
You are given a string s. Your task is to remove all digits by doing this operation repeatedly: Delete the first digit and the closest non-digit character to its left. Return the resulting string after removing all digits.
/**
* @param {string} s
* @return {string}
*/
var clearDigits = function(s) {
while (true) {
let digitIndex = -1;
// Find the first digit in the string
for (let i = 0; i < s.length; i++) {
@tatsuyax25
tatsuyax25 / countBadPairs.js
Created February 9, 2025 20:29
You are given a 0-indexed integer array nums. A pair of indices (i, j) is a bad pair if i < j and j - i != nums[j] - nums[i]. Return the total number of bad pairs in nums.
/**
* @param {number[]} nums
* @return {number}
*/
var countBadPairs = function(nums) {
const len = nums.length;
// Total number of pairs possible in the array
const totalPairs = len * (len - 1) / 2;
const pairsMap = new Map();
let good = 0;
@tatsuyax25
tatsuyax25 / NumberContainers.js
Created February 8, 2025 20:19
Design a number container system that can do the following: Insert or Replace a number at the given index in the system. Return the smallest index for the given number in the system. Implement the NumberContainers class: NumberContainers() Initiali
// Constructor for NumberContainers class
var NumberContainers = function() {
// Map to store the relationship between index and number
this.indexMap = new Map();
// Map to store the relationship between number and MinHeap of indices
this.numberMap = new Map();
};
/**
* Change the number at a specific index
@tatsuyax25
tatsuyax25 / queryResults.js
Created February 7, 2025 17:33
You are given an integer limit and a 2D array queries of size n x 2. There are limit + 1 balls with distinct labels in the range [0, limit]. Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x w
/**
* @param {number} limit
* @param {number[][]} queries
* @return {number[]}
*/
var queryResults = function(limit, queries) {
// Map to store the color of each ball
const ballColors = new Map();
// Map to store the frequency of each color
const colorFreqs = new Map();
@tatsuyax25
tatsuyax25 / areAlmostEqual.js
Created February 5, 2025 18:52
You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return true if it is possible to make both strings equa
/**
* @param {string} s1
* @param {string} s2
* @return {boolean}
*/
var areAlmostEqual = function(s1, s2) {
// Initialize an array to store the positions where s1 and s2 differ
let differences = [];
// Iterate through the strings to find differing positions
@tatsuyax25
tatsuyax25 / maxAscendingSum.js
Created February 4, 2025 18:29
Given an array of positive integers nums, return the maximum possible sum of an ascending subarray in nums. A subarray is defined as a contiguous sequence of numbers in an array. A subarray [numsl, numsl+1, ..., numsr-1, numsr] is ascending if for
/**
* @param {number[]} nums
* @return {number}
*/
var maxAscendingSum = function(nums) {
// Initialize maxSum with the first element of the array
let maxSum = nums[0];
// Initialize currentSum with the first element of the array
let currentSum = nums[0];
@tatsuyax25
tatsuyax25 / longestMonotonicSubarray.js
Created February 3, 2025 17:37
You are given an array of integers nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.
/**
* @param {number[]} nums
* @return {number}
*/
var longestMonotonicSubarray = function(nums) {
if (nums.length === 0) return 0;
let maxLength = 1;
let currentLength = 1;
let increasing = null;