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 / findLHS.js
Created June 30, 2025 16:32
We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1. Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.
/**
* @param {number[]} nums
* @return {number}
*/
var findLHS = function(nums) {
const freqMap = new Map(); // To store frequency of each number
let maxLength = 0;
// Step 1: Count the frequency of each number
for (const num of nums) {
@tatsuyax25
tatsuyax25 / numSubseq.js
Created June 29, 2025 16:16
You are given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it mo
/**
* Counts the number of non-empty subsequences such that
* the minimum and maximum elements sum to <= target.
*
* @param {number[]} nums - Input array of integers
* @param {number} target - Max allowed sum of min and max in a subsequence
* @return {number} - Count of valid subsequences modulo 10^9 + 7
*/
var numSubseq = function(nums, target) {
// Sort array to allow two-pointer traversal
@tatsuyax25
tatsuyax25 / maxSubsequence.js
Created June 28, 2025 16:08
You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum. Return any such subsequence as an integer array of length k. A subsequence is an array that can be derived from anoth
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSubsequence = function(nums, k) {
// Step 1: Map each element to a pair [value, originalIndex]
const withIndices = nums.map((num, index) => [num, index]);
// Step 2: Sort by value in descending order to find the top-k values
@tatsuyax25
tatsuyax25 / longestSubsequenceRepeatedK.js
Created June 27, 2025 16:56
You are given a string s of length n, and an integer k. You are tasked to find the longest subsequence repeated k times in string s. A subsequence is a string that can be derived from another string by deleting some or no characters without changing
/**
* @param {string} s
* @param {number} k
* @return {string}
*/
function isKRepeatedSubsequence(s, candidate, k) {
let i = 0, count = 0;
for (let ch of s) {
if (ch === candidate[i]) {
i++;
@tatsuyax25
tatsuyax25 / longestSubsequence.js
Created June 26, 2025 20:32
You are given a binary string s and a positive integer k. Return the length of the longest subsequence of s that makes up a binary number less than or equal to k. Note: The subsequence can contain leading zeroes. The empty string is considered to
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var longestSubsequence = function(s, k) {
// Special case: string is just "0"
if (s === "0") return 1;
// If the entire binary string is already less than or equal to k
@tatsuyax25
tatsuyax25 / kthSmallestProduct.js
Created June 25, 2025 17:16
Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return the kth (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k
* @return {number}
*/
var kthSmallestProduct = function(nums1, nums2, k) {
// The range for the possible product values.
// the values in nums can be up to 10^5, so the product can be up to 10^10.
let left = -1e10 - 7; // A sufficiently small number
@tatsuyax25
tatsuyax25 / findKDistantIndices.js
Last active June 24, 2025 16:42
You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key. Return a list of all k-distant indices sorted in
/**
* @param {number[]} nums
* @param {number} key
* @param {number} k
* @return {number[]}
*/
var findKDistantIndices = function(nums, key, k) {
// Step 1: Find all indices where the value equals the 'key'.
// We'll store these in an array to easily iterate over them later.
const keyIndices = [];
@tatsuyax25
tatsuyax25 / kMirror.js
Created June 23, 2025 18:22
A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k. For example, 9 is a 2-mirror number. The representation of 9 in base-10 and base-2 are 9 and 1001 respective
/**
* Returns the sum of the first `n` numbers that are palindromic
* in both base-10 and base-`k` (k-mirror numbers).
*
* @param {number} k - The target base (base-k)
* @param {number} n - Number of k-mirror numbers to find
* @return {number} - Sum of the first `n` k-mirror numbers
*/
var kMirror = function(k, n) {
let res = []; // Array to store valid k-mirror numbers
@tatsuyax25
tatsuyax25 / divideString.js
Created June 22, 2025 16:11
A string s can be partitioned into groups of size k using the following procedure: The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each element can be a p
/**
* @param {string} s
* @param {number} k
* @param {character} fill
* @return {string[]}
*/
var divideString = function(s, k, fill) {
const result = [];
// Loop through the string in increments of k
@tatsuyax25
tatsuyax25 / minimumDeletions.js
Created June 21, 2025 16:52
You are given a string word and an integer k. We consider word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i and j in the string. Here, freq(x) denotes the frequency of the character x in word, and |y| denotes the absolu
/**
* Returns the minimum number of deletions required to make the string k-special.
* A string is k-special if the difference between the frequencies of any two characters
* is at most k.
*
* @param {string} word - The input string.
* @param {number} k - The allowed max difference between frequencies.
* @return {number} - The minimum deletions needed.
*/
var minimumDeletions = function(word, k) {