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 / 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;
@tatsuyax25
tatsuyax25 / check.js
Created February 2, 2025 18:15
Given an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false. There may be duplicates in the original array. Note: An array A rotated by
/**
* @param {number[]} nums
* @return {boolean}
*/
var check = function(nums) {
// An empty array is considered sorted and rotated
if (nums.length === 0) {
return true;
}
@tatsuyax25
tatsuyax25 / isArraySpecial.js
Last active February 1, 2025 16:52
An array is considered special if every pair of its adjacent elements contains two numbers with different parity. You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
/**
* @param {number[]} nums
* @return {boolean}
*/
var isArraySpecial = function(nums) {
// Loop through the array up to the second last element
for (let i = 0; i < nums.length - 1; i++) {
// Check if the current element and the next element have the same parity
// Parity means being either both even or both odd
if ((nums[i] % 2) === (nums[i + 1] % 2)) {
@tatsuyax25
tatsuyax25 / largestIsland.js
Created January 31, 2025 19:51
You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1. Return the size of the largest island in grid after applying this operation. An island is a 4-directionally connected group of 1s.
/**
* @param {number[][]} grid
* @return {number}
*/
var largestIsland = function(grid) {
const n = grid.length;
// Helper function to perform DFS and mark island cells with a unique island ID
function dfs(x, y, islandId) {
const stack = [[x, y]];
@tatsuyax25
tatsuyax25 / magnificentSets.js
Created January 30, 2025 18:45
You are given a positive integer n representing the number of nodes in an undirected graph. The nodes are labeled from 1 to n. You are also given a 2D integer array edges, where edges[i] = [ai, bi] indicates that there is a bidirectional edge betwee
/**
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/
var magnificentSets = function(n, edges) {
// Initialize Union-Find structure for connected components
let uf = new UnionFind(n + 1);
// Initialize adjacency list for the graph
let graph = Array(n + 1).fill(0).map(() => []);
@tatsuyax25
tatsuyax25 / findRedundantConnection.js
Created January 29, 2025 17:25
In this problem, a tree is an undirected graph that is connected and has no cycles. You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from
/**
* @param {number[][]} edges
* @return {number[]}
*/
class UnionFind {
// Constructor to initialize parent and rank arrays.
constructor(len) {
// Create parent array where each node is its own parent initially.
this.parent = Array.from({ length: len + 1 }, (_, i) => i);
@tatsuyax25
tatsuyax25 / findMaxFish.js
Created January 28, 2025 19:02
You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents: A land cell if grid[r][c] = 0, or A water cell containing grid[r][c] fish, if grid[r][c] > 0. A fisher can start at any water cell (r, c) and can do the following opera
/**
* @param {number[][]} grid
* @return {number}
*/
var findMaxFish = function(grid) {
// Initialize the maximum fish count as 0
let maxFish = 0;
// Get the number of rows and columns in the grid
const rows = grid.length;
@tatsuyax25
tatsuyax25 / checkIfPrerequisite.js
Created January 27, 2025 17:23
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course ai first if you want to take course bi. For exampl
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @param {number[][]} queries
* @return {boolean[]}
*/
function buildGraph(prerequisites) {
// Initialize an adjacency list to store the graph
const graph = {};
for (let [a, b] of prerequisites) {
@tatsuyax25
tatsuyax25 / maximumInvitations.js
Last active January 26, 2025 17:50
A company is organizing a meeting and has a list of n employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees. The employees are numbered from 0 to n - 1. Each employee has a favori
/**
* @param {number[]} favorite
* @return {number}
*/
var maximumInvitations = function(favorite) {
const n = favorite.length;
let longestCycle = 0; // Variable to track the longest cycle found
const visit = Array.from({ length: n }, () => false); // Boolean array to track visited nodes
const len2Cycles = []; // Array to track cycles of length 2