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 / getNoZeroIntegers.js
Created September 8, 2025 19:53
No-Zero integer is a positive integer that does not contain any 0 in its decimal representation. Given an integer n, return a list of two integers [a, b] where: a and b are No-Zero integers. a + b = n The test cases are generated so that there is a
/**
* Helper function to check if a number is a No-Zero integer
* A No-Zero integer has no '0' digit in its decimal representation
*/
function isNoZero(num) {
return !num.toString().includes('0');
};
/**
* Main function to find two No-Zero integers that sum to n
@tatsuyax25
tatsuyax25 / sumZero.js
Created September 7, 2025 16:58
Given an integer n, return any array containing n unique integers such that they add up to 0.
/**
* @param {number} n
* @return {number[]}
*/
var sumZero = function(n) {
const result = [];
// Add symmetric pairs: [-1, 1], [-2, 2], ...
for (let i = 1; i <= Math.floor(n / 2); i++) {
result.push(-i, i)
@tatsuyax25
tatsuyax25 / minOperations.js
Created September 6, 2025 14:51
You are given a 2D array queries, where queries[i] is of the form [l, r]. Each queries[i] defines an array of integers nums consisting of elements ranging from l to r, both inclusive. In one operation, you can: Select two integers a and b from the
/**
* Calculates the minimum number of operations required to reduce
* all elements in each query range [l, r] to zero.
*
* In one operation, select two numbers and replace them with floor(a / 4) and floor(b / 4).
*
* This solution groups numbers by their "division depth" β€” how many times
* they must be divided by 4 before reaching 0 β€” and counts how many numbers
* fall into each depth level.
*
@tatsuyax25
tatsuyax25 / makeTheIntegerZero.js
Created September 5, 2025 18:49
You are given two integers num1 and num2. In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1. Return the integer denoting the minimum number of operations needed to make num1 equal to 0. If it is impos
/**
* Determines the minimum number of operations needed to reduce num1 to 0.
* Each operation subtracts (2^i + num2), where i ∈ [0, 60].
* Returns -1 if it's impossible.
*
* @param {number} num1 - Starting value
* @param {number} num2 - Fixed offset added to each power of 2
* @return {number} - Minimum number of operations or -1
*/
var makeTheIntegerZero = function(num1, num2) {
@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...