Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / pacificAtlantic.js
Created October 5, 2025 14:48
There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges. The island is partitioned int
/**
* Given a matrix of heights, returns coordinates of cells that can flow to both the Pacific and Atlantic oceans.
* Pacific touches the left and top edges; Atlantic touches the right and bottom edges.
* @param {number[][]} heights - 2D grid of elevation values
* @return {number[][]} - List of coordinates [i, j] that can reach both oceans
*/
var pacificAtlantic = function(heights) {
let row = heights.length;
let col = heights[0].length;
let arr = []; // Stores final result: cells that can reach both oceans
@tatsuyax25
tatsuyax25 / maxArea.js
Created October 4, 2025 17:01
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container con
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
// Initialize two pointers: one at the start, one at the end of the array
let left = 0;
let right = height.length - 1;
// Variable to keep track of the maximum area found so far
@tatsuyax25
tatsuyax25 / maxBottlesDrunk.js
Created October 2, 2025 19:44
You are given two integers numBottles and numExchange. numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations: Drink any number of full water bottles turning
/**
* @param {number} numBottles
* @param {number} numExchange
* @return {number}
*/
var maxBottlesDrunk = function(numBottles, numExchange) {
// Track the total number of bottles drunk
let totalDrunk = 0;
// Track how many empty bottles we have after drinking
@tatsuyax25
tatsuyax25 / triangularSum.js
Created September 30, 2025 17:23
You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive). The triangular sum of nums is the value of the only element present in nums after the following process terminates: Let nums comprise of n elements.
/**
* @param {number[]} nums
* @return {number}
*/
var triangularSum = function(nums) {
// Continue the process until only one element remains
while (nums.length > 1) {
let newNums = [];
// Generate the next row by summing adjacent elements modulo 10
for (let i = 0; i < nums.length - 1; i++) {
@tatsuyax25
tatsuyax25 / minScoreTriangulation.js
Created September 29, 2025 18:44
You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex in clockwise order. Polygon triangulation is a process where you divide a polygon into a s
/**
* @param {number[]} values
* @return {number}
*/
var minScoreTriangulation = function(values) {
const n = values.length;
// Create a 2D DP table initialized with 0s
// dp[i][j] will store the minimum triangulation score for the subpolygon from vertex i to vertex j.
const dp = Array.from ({ length: n }, () => Array(n).fill(0));
@tatsuyax25
tatsuyax25 / largestPerimeter.js
Created September 28, 2025 17:27
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.
/**
* Finds the largest perimeter of a triangle that can be formed from the given array of side lengths.
* A triangle is valid if the sum of any two sides is greater than the third side.
*
* @param {number[]} nums - Array of positive integers representing side lengths.
* @return {number} - The largest perimeter of a valid triangle, or 0 if none can be formed.
*/
var largestPerimeter = function(nums) {
// Sort the array in descending order to try the largest sides first
nums.sort((a, b) => b - a);
@tatsuyax25
tatsuyax25 / largestTriangleArea.js
Last active September 27, 2025 15:29
Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of the largest triangle that can be formed by any three different points. Answers within 10-5 of the actual answer will be accepted.
/**
* @param {number[][]} points
* @return {number}
*/
var largestTriangleArea = function(points) {
// Helper function to calculate the area of a triangle given three points
function triangleArea(p1, p2, p3) {
// Shoelace formula:
// Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
let [x1, y1] = p1;
@tatsuyax25
tatsuyax25 / triangleNumber.js
Last active September 26, 2025 16:38
Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
/**
* @param {number[]} nums
* @return {number}
*/
var triangleNumber = function(nums) {
// Step 1: Sort the array to simplify triangle inequality checks
nums.sort((a, b) => a - b);
let count = 0;
let n = nums.length;
@tatsuyax25
tatsuyax25 / minimumTotal.js
Created September 25, 2025 17:26
Given a triangle array, return the minimum path sum from top to bottom. For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the
/**
* @param {number[][]} triangle
* @return {number}
*/
var minimumTotal = function(triangle) {
// Start from the second-to-last row and move upward
for (let row = triangle.length - 2; row >= 0; row--) {
for (let col = 0; col < triangle[row].length; col++) {
// For each element, choose the minimum of the two adjacent numbers in the row below
// and add it to the current element
@tatsuyax25
tatsuyax25 / fractionToDecimal.js
Created September 24, 2025 18:30
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return any of them.
/**
* @param {number} numerator
* @param {number} denominator
* @return {string}
*/
var fractionToDecimal = function(numerator, denominator) {
if (numerator === 0) return "0";
let result = "";