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 / 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;
@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}
*/