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 / findClosest.js
Created September 4, 2025 18:44
You are given three integers x, y, and z, representing the positions of three people on a number line: x is the position of Person 1. y is the position of Person 2. z is the position of Person 3, who does not move. Both Person 1 and Person 2 move to
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @return {number}
*/
var findClosest = function(x, y, z) {
// Calculate absolute distance from Person 1 to Person 3
const distance1 = Math.abs(x - z);
@tatsuyax25
tatsuyax25 / numberOfPairs.js
Created September 3, 2025 18:34
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi]. We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-
/**
* Counts the number of valid point pairs (i, j) such that:
* - x[i] ≤ x[j]
* - y[i] ≥ y[j]
* - No other point between i and j has the same or higher y[j]
*
* @param {number[][]} points - Array of [x, y] coordinates
* @return {number} - Number of valid pairs
*/
var numberOfPairs = function(points) {
@tatsuyax25
tatsuyax25 / numberOfPairs.js
Created September 2, 2025 16:06
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi]. Count the number of pairs of points (A, B), where A is on the upper left side of B, and there are no other poin
/**
* @param {number[][]} points
* @return {number}
*/
var numberOfPairs = function(points) {
let count = 0;
// Step 1: Sort points by x ascending, then y descending
// This ensures that for any point[i], all point[j] with j < i have x <= x[i]
points.sort((a, b) => {
@tatsuyax25
tatsuyax25 / solveSudoku.js
Created August 31, 2025 16:12
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each colum
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solveSudoku = function(board) {
const SIZE = 9;
// Track used digits in rows, columns, and 3x3 boxes
const row = Array.from({ length: SIZE }, () => Array(SIZE).fill(false));
const col = Array.from({ length: SIZE }, () => Array(SIZE).fill(false));
@tatsuyax25
tatsuyax25 / isValidSudoku.js
Created August 30, 2025 17:23
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the
/**
* Validates a partially filled 9x9 Sudoku board.
* Only filled cells are checked for rule violations.
*
* @param {character[][]} board - 2D array representing the Sudoku board
* @return {boolean} - true if the board is valid, false otherwise
*/
var isValidSudoku = function(board) {
// Set to track seen digits in rows, columns, and sub-boxes
const seen = new Set();
@tatsuyax25
tatsuyax25 / flowerGame.js
Created August 29, 2025 17:05
Alice and Bob are playing a turn-based game on a field, with two lanes of flowers between them. There are x flowers in the first lane between Alice and Bob, and y flowers in the second lane between them. The game proceeds as follows: Alice takes th
/**
* @param {number} n
* @param {number} m
* @return {number}
*/
var flowerGame = function(n, m) {
// Count how many odd numbers are in [1, n]
const oddX = Math.floor(n / 2) + (n % 2); // e.g., 1, 3, 5...
const evenX = Math.floor(n / 2); // e.g., 2, 4, 6...
@tatsuyax25
tatsuyax25 / sortMatrix.js
Created August 28, 2025 16:28
You are given an n x n square matrix of integers grid. Return the matrix such that: The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order. The diagonals in the top-right triangle are sorted in n
/**
* @param {number[][]} grid
* @return {number[][]}
*/
var sortMatrix = function(grid) {
const n = grid.length;
// Helper to extract a matrix starting at (row, col)
function getMatrix(row, col) {
const matrix = [];
@tatsuyax25
tatsuyax25 / lenOfVDiagonal.js
Created August 27, 2025 17:17
You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2. A V-shaped diagonal segment is defined as: The segment starts with 1. The subsequent elements follow this infinite sequence: 2, 0, 2, 0, .... The segment
/**
* @param {number[][]} grid
* @return {number}
*/
var lenOfVDiagonal = function(grid) {
const n = grid.length;
const m = grid[0].length;
// Diagonal directions: ↘, ↙, ↖, ↗
const DIRS = [
@tatsuyax25
tatsuyax25 / areaOfMaxDiagonal.js
Last active August 26, 2025 17:06
You are given a 2D 0-indexed integer array dimensions. For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i. Return the area of the rectangle having the l
/**
* Returns the area of the rectangle with the longest diagonal.
* If multiple rectangles share the longest diagonal, returns the one with the largest area.
*
* @param {number[][]} dimensions - Array of [length, width] pairs for each rectangle.
* @return {number} - Area of the rectangle with the longest diagonal.
*/
var areaOfMaxDiagonal = function(dimensions) {
let maxDiagonalSq = 0; // Stores the longest diagonal squared (avoids using Math.sqrt)
let maxArea = 0; // Stores the area of the rectangle with the longest diagonal
@tatsuyax25
tatsuyax25 / findDiagonalOrder.js
Created August 25, 2025 19:09
Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.
/**
* @param {number[][]} mat
* @return {number[]}
*/
var findDiagonalOrder = function(mat) {
// Edge case: empty matrix
if (!mat || mat.length === 0 || mat[0].length === 0) return [];
const m = mat.length; // number of rows
const n = mat[0].length; // number of columns