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 / minOperations.js
Last active March 5, 2026 20:53
You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alterna
/**
* @param {string} s
* @return {number}
*/
var minOperations = function(s) {
// cost0 = number of flips needed if we force pattern "010101..."
// cost1 = number of flips needed if we force pattern "101010..."
let cost0 = 0;
let cost1 = 0;
@tatsuyax25
tatsuyax25 / numSpecial.js
Created March 4, 2026 17:59
Given an m x n binary matrix mat, return the number of special positions in mat. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).
/**
* @param {number[][]} mat
* @return {number}
*/
var numSpecial = function(mat) {
const m = mat.length;
const n = mat[0].length;
// Count how many 1s appear in each row
const rowCount = new Array(m).fill(0);
@tatsuyax25
tatsuyax25 / minSwaps.js
Created March 2, 2026 18:00
Given an n x n binary grid, in one step you can choose two adjacent rows of the grid and swap them. A grid is said to be valid if all the cells above the main diagonal are zeros. Return the minimum number of steps needed to make the grid valid, or
/**
* @param {number[][]} grid
* @return {number}
*/
var minSwaps = function(grid) {
const n = grid.length;
// Step 1: For each row, compute the index of the rightmost 1.
// If a row has no 1s, store -1.
const rightmost = new Array(n).fill(0);
@tatsuyax25
tatsuyax25 / minPartitions.js
Created March 1, 2026 17:27
A decimal number is called deci-binary if each of its digits is either 0 or 1 without any leading zeros. For example, 101 and 1100 are deci-binary, while 112 and 3001 are not. Given a string n that represents a positive decimal integer, return the m
/**
* @param {string} n
* @return {number}
*/
var minPartitions = function(n) {
// We want the minimum number of deci-binary numbers needed.
// A deci-binary number can only contribute 0 or 1 to each digit.
// Therefore, the number of deci-binary numbers required is equal
// to the maximum digit in the string.
@tatsuyax25
tatsuyax25 / concatenatedBinary.js
Created February 28, 2026 18:19
Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 109 + 7.
/**
* @param {number} n
* @return {number}
*/
var concatenatedBinary = function(n) {
const MOD = 1_000_000_007;
let ans = 0;
let bitLength = 0;
let pow2 = 1; // this will always store 2^bitLength % MOD
@tatsuyax25
tatsuyax25 / minOperations.js
Created February 27, 2026 19:30
You are given a binary string s, and an integer k. In one operation, you must choose exactly k different indices and flip each '0' to '1' and each '1' to '0'. Return the minimum number of operations required to make all characters in the string equ
/**
* @param {string} s
* @param {number} k
* @return {number}
*
* This solution is based on analyzing how many zeros can be flipped per operation.
* Each operation flips exactly k bits, so the parity of the zero count changes
* depending on k. The editorial shows that the minimum number of operations can be
* computed using two candidate formulas depending on parity constraints.
*/
@tatsuyax25
tatsuyax25 / sortByBits.js
Created February 25, 2026 18:23
You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order. Retur
/**
* @param {number[]} arr
* @return {number[]}
*/
var sortByBits = function(arr) {
// Helper: count how many 1-bits are in the binary representation
function bitCount(n) {
// Convert to binary string and count '1' characters
return n.toString(2).split('1').length - 1;
}
@tatsuyax25
tatsuyax25 / sumRootToLeaf.js
Created February 24, 2026 20:28
You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
@tatsuyax25
tatsuyax25 / hasAllCodes.js
Created February 23, 2026 17:46
Given a binary string s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false.
/**
* @param {string} s
* @param {number} k
* @return {boolean}
*/
var hasAllCodes = function(s, k) {
// Total number of binary codes of length k
const needed = 1 << k; // same as Math.pow(2, k)
// Quick fail: if s is too short to contain all codes
@tatsuyax25
tatsuyax25 / binaryGap.js
Created February 22, 2026 18:04
Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0. Two 1's are adjacent if there are only 0's separating them (possibly no 0's)
/**
* @param {number} n
* @return {number}
*/
var binaryGap = function(n) {
// Convert the number to its binary string representation.
// Example: 22 -> "10110"
const binary = n.toString(2);
// This will store the index of the *previous* '1' we saw.