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 / 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();
@tatsuyax25
tatsuyax25 / constructFromPrePost.js
Created February 23, 2025 23:06
Given two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the postorder traversal of the same tree, reconstruct and return the binary tree. If there exist multiple
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} preorder
@tatsuyax25
tatsuyax25 / recoverFromPreorder.js
Last active February 22, 2025 17:42
We run a preorder depth-first search (DFS) on the root of a binary tree. At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node. If the depth of a node is D, the depth of its im
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {string} traversal
@tatsuyax25
tatsuyax25 / FindElements.js
Last active February 21, 2025 17:06
Given a binary tree with the following rules: root.val == 0 For any treeNode: If treeNode.val has a value x and treeNode.left != null, then treeNode.left.val == 2 * x + 1 If treeNode.val has a value x and treeNode.right != null, then treeNode.right.
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
@tatsuyax25
tatsuyax25 / findDifferentBinaryString .js
Created February 20, 2025 17:01
Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.
/**
* @param {string[]} nums
* @return {string}
*/
var findDifferentBinaryString = function(nums) {
const n = nums.length;
const numsSet = new Set(nums);
// Generate all possible binary strings of length n
for (let i = 0; i < 2 ** n; i++) {
@tatsuyax25
tatsuyax25 / getHappyString.js
Created February 19, 2025 17:27
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and
/**
* @param {number} n
* @param {number} k
* @return {string}
*/
var getHappyString = function(n, k) {
// Helper function to perform backtracking and generate all happy string
function backtrack(current, result) {
// If the current string has reached the desired length, add it to the result list
if (current.length === n) {
@tatsuyax25
tatsuyax25 / smallestNumber.js
Last active February 18, 2025 17:09
You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing. A 0-indexed string num of length n + 1 is created using the following conditions: num consists of the digits '1' to
/**
* @param {string} pattern
* @return {string}
*/
var smallestNumber = function(pattern) {
const n = pattern.length;
const result = [];
const stack = [];
for (let i = 0; i <= n; i++) {
@tatsuyax25
tatsuyax25 / numTilePossibilities.js
Created February 17, 2025 17:56
You have n tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
/**
* @param {string} tiles
* @return {number}
*/
var numTilePossibilities = function(tiles) {
// Helper function for backtracking
function backtrack(path, remaining) {
// If the current path is not empty, add it to the set of unique sequences
if (path.length > 0) {
uniqueSequences.add(path.join(''));
@tatsuyax25
tatsuyax25 / constructDistancedSequence.js
Created February 16, 2025 20:03
Given an integer n, find a sequence that satisfies all of the following: The integer 1 occurs once in the sequence. Each integer between 2 and n occurs twice in the sequence. For every integer i between 2 and n, the distance between the two occurren
/**
* @param {number} n
* @return {number[]}
*/
var constructDistancedSequence = function(n) {
// Initialize the sequence array with 0s and an array to track placed numbers
const seq = new Array(n * 2 - 1).fill(0), isPlaced = new Array(n + 1).fill(false);
// Mark index 0 as placed (not used in actual sequence)
isPlaced[0] = true;