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 / checkPowersOfThree.js
Created March 4, 2025 17:07
Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false. An integer y is a power of three if there exists an integer x such that y == 3x.
/**
* @param {number} n
* @return {boolean}
*/
var checkPowersOfThree = function(n) {
// Loop until n becomes zero
while (n > 0) {
// If the remainder when n is divided by 3 is 2, return. false
// This means n cannot be represented as the sum of distinct powers of three
if (n % 3 === 2) {
@tatsuyax25
tatsuyax25 / pivotArray.js
Created March 4, 2025 00:01
You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied: Every element less than pivot appears before every element greater than pivot. Every element equal to pivot appears i
/**
* @param {number[]} nums
* @param {number} pivot
* @return {number[]}
*/
var pivotArray = function(nums, pivot) {
// Arrays to hold elements less than, equal to, and greater than pivot
let lessThanPivot = [];
let equalToPivot = [];
let greaterThanPivot = [];
@tatsuyax25
tatsuyax25 / mergeArrays.js
Last active April 14, 2025 16:20
You are given two 2D integer arrays nums1 and nums2. nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. Each array con
/**
* @param {number[][]} nums1
* @param {number[][]} nums2
* @return {number[][]}
*/
var mergeArrays = function(nums1, nums2) {
// Create a set to collect all unique ids
let allIds = new Set();
nums1.forEach(num => allIds.add(num[0]));
nums2.forEach(num => allIds.add(num[0]));
@tatsuyax25
tatsuyax25 / mergeArrays.js
Created March 2, 2025 17:48
You are given two 2D integer arrays nums1 and nums2. nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. Each array con
/**
* @param {number[][]} nums1
* @param {number[][]} nums2
* @return {number[][]}
*/
var mergeArrays = function(nums1, nums2) {
// Create a set to collect all unique ids
let allIds = new Set();
nums1.forEach(num => allIds.add(num[0]));
nums2.forEach(num => allIds.add(num[0]));
@tatsuyax25
tatsuyax25 / applyOperations.js
Last active March 1, 2025 17:17
You are given a 0-indexed array nums of size n consisting of non-negative integers. You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums: If nums[i] == num
/**
* @param {number[]} nums
* @return {number[]}
*/
var applyOperations = function(nums) {
const n = nums.length;
// Perform the operations
for (let i = 0; i < n - 1; i++) {
if (nums[i] === nums[i + 1]) {
@tatsuyax25
tatsuyax25 / shortestCommonSupersequence.js
Created February 28, 2025 18:47
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of characters from t (p
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// Function to find the longest common subsequence (LCS) of two strings
function findLCS(str1, str2) {
const m = str1.length;
const n = str2.length;
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
@tatsuyax25
tatsuyax25 / lenLongestFibSubseq.js
Created February 27, 2025 19:00
A sequence x1, x2, ..., xn is Fibonacci-like if: n >= 3 xi + xi+1 == xi+2 for all i + 2 <= n Given a strictly increasing array arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence of arr. If one do
/**
* @param {number[]} arr
* @return {number}
*/
var lenLongestFibSubseq = function(arr) {
const n = arr.length;
if (n < 3) return 0;
const indexMap = new Map();
for (let i = 0; i < n; i++) {
@tatsuyax25
tatsuyax25 / maxAbsoluteSum.js
Created February 26, 2025 17:31
You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr). Return the maximum absolute sum of any (possibly empty) subarray of nums. Note that abs(x) is
/**
* @param {number[]} nums
* @return {number}
*/
var maxAbsoluteSum = function(nums) {
// Initialize variables to store the max sum, current max, min sum, and current min
let maxSum = 0;
let currentMax = 0;
let minSum = 0;
let currentMin = 0;
@tatsuyax25
tatsuyax25 / numOfSubarrays.js
Created February 25, 2025 17:17
Given an array of integers arr, return the number of subarrays with an odd sum. Since the answer can be very large, return it modulo 109 + 7.
/**
* @param {number[]} arr
* @return {number}
*/
var numOfSubarrays = function(arr) {
const MOD = 10**9 + 7;
let n = arr.length;
let count = 0;
let prefixSum = 0;
let oddCount = 0;
@tatsuyax25
tatsuyax25 / mostProfitablePath.js
Created February 24, 2025 17:52
There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. At every node i
/**
* @param {number[][]} edges
* @param {number} bob
* @param {number[]} amount
* @return {number}
*/
var mostProfitablePath = function(edges, bob, amount) {
const n = amount.length;
const graph = new Map();