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 / punishmentNumber.js
Created February 15, 2025 17:33
Given a positive integer n, return the punishment number of n. The punishment number of n is defined as the sum of the squares of all integers i such that: 1 <= i <= n The decimal representation of i * i can be partitioned into contiguous substring
/**
* @param {number} n
* @return {number}
*/
// Function to check if the string representation of a number can be partitioned
// into contiguous substrings that sum to a target value
function canPartition(numStr, target) {
// Helper function to perform backtracking
function backtrack(index, currentSum) {
// If we've reached the end of the string, check if the current sum equals the target
@tatsuyax25
tatsuyax25 / ProductOfNumbers.js
Last active February 14, 2025 17:44
Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream. Implement the ProductOfNumbers class: ProductOfNumbers() Initializes the object with an empty stream. void add(int num) Appends th
var ProductOfNumbers = function() {
// Initialize an array to store the prefix products
this.prefixProduct = [1]; // Start with 1 to handle the empty product case
};
/**
* @param {number} num
* @return {void}
*/
@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