π¨βπ»
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
NewerOlder