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 / findMin.js
Created May 15, 2026 21:26
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that ro
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
@tatsuyax25
tatsuyax25 / isGood.js
Created May 14, 2026 16:46
You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrence
/**
* @param {number[]} nums
* @return {boolean}
*/
var isGood = function(nums) {
const n = Math.max(...nums);
// Condition 1: length must be n + 1
if (nums.length !== n + 1) return false;
@tatsuyax25
tatsuyax25 / minMoves.js
Created May 13, 2026 19:09
You are given an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive. The array nums is complementary if for all indices i (0-indexed), nums
/**
* @param {number[]} nums
* @param {number} limit
* @return {number}
*/
var minMoves = function(nums, limit) {
const n = nums.length;
const diff = new Array(2 * limit + 2).fill(0);
for (let i = 0; i < n / 2; i++) {
@tatsuyax25
tatsuyax25 / minimumEffort.js
Created May 12, 2026 16:21
You are given an array tasks where tasks[i] = [actuali, minimumi]: actuali is the actual amount of energy you spend to finish the ith task. minimumi is the minimum amount of energy you require to begin the ith task. For example, if the task is [10,
/**
* @param {number[][]} tasks
* @return {number}
*/
var minimumEffort = function(tasks) {
// Sort by (minimum - actual) descending
tasks.sort((a, b) => (b[1] - b[0]) - (a[1] - a[0]));
let totalActual = 0; // sum of actual costs of tasks done so far
let answer = 0; // minimal initial energy required
@tatsuyax25
tatsuyax25 / separateDigits.js
Created May 11, 2026 15:42
Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums. To separate the digits of an integer is to get all the digits it has in
/**
* @param {number[]} nums
* @return {number[]}
*/
var separateDigits = function(nums) {
const res = [];
for (let n of nums) {
// If the number is a single digit, just append it
if (n < 10) {
@tatsuyax25
tatsuyax25 / maximumJumps.js
Created May 10, 2026 16:44
You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: 0 <= i < j < n -target <= nums[j] - nums[i] <= target Return the max
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var maximumJumps = function(nums, target) {
const n = nums.length;
// dp[i] = maximum number of jumps needed to reach index i
// Initialize all as unreachable (-Infinity)
@tatsuyax25
tatsuyax25 / rotateGrid.js
Created May 9, 2026 17:43
ou are given an m x n integer matrix grid​​​, where m and n are both even integers, and an integer k. The matrix is composed of several layers, which is shown in the below image, where each color is its own layer: A cyclic rotation of the matrix
/**
* @param {number[][]} grid
* @param {number} k
* @return {number[][]}
*/
var rotateGrid = function(grid, k) {
const m = grid.length, n = grid[0].length;
const layers = Math.min(m, n) / 2;
for (let layer = 0; layer < layers; layer++) {
@tatsuyax25
tatsuyax25 / minJumps.js
Created May 8, 2026 17:02
You are given an integer array nums of length n. You start at index 0, and your goal is to reach index n - 1. From any index i, you may perform one of the following operations: Adjacent Step: Jump to index i + 1 or i - 1, if the index is within bo
/**
* @param {number[]} nums
* @return {number}
*/
var minJumps = function(nums) {
const n = nums.length;
if (n === 1) return 0;
// 1. Build SPF (smallest prime factor) up to max(nums)
const maxVal = Math.max(...nums);
@tatsuyax25
tatsuyax25 / maxValue.js
Created May 7, 2026 19:57
You are given an integer array nums. From any index i, you can jump to another index j under the following rules: Jump to index j where j > i is allowed only if nums[j] < nums[i]. Jump to index j where j < i is allowed only if nums[j] > nums[i]. Fo
/**
* @param {number[]} nums
* @return {number[]}
*/
var maxValue = function(nums) {
const n = nums.length;
const pref = Array(n);
const suff = Array(n);
// prefix max
@tatsuyax25
tatsuyax25 / rotateRight.js
Created May 5, 2026 19:36
Given the head of a linked list, rotate the list to the right by k places.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k