π¨βπ»
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
/** | |
* Calculates the maximum Manhattan distance from the origin | |
* that can be achieved while moving in a grid, with the option | |
* to change up to 'k' directions. | |
* | |
* @param {string} s - String of movement directions: 'N', 'S', 'E', 'W'. | |
* @param {number} k - Number of allowed direction changes. | |
* @return {number} - Maximum distance from the origin at any point. | |
*/ | |
var maxDistance = function(s, 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[]} nums | |
* @param {number} k | |
* @return {number} | |
*/ | |
var partitionArray = function(nums, k) { | |
if (nums.length === 0) return 0; | |
// Step 1: Sort the array so that we can group close numbers together | |
nums.sort((a, b) => a - b); |
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[][]} | |
*/ | |
// Function to divide an array into subarrays of size 3 | |
// such that the difference between the smallest and largest element in each subarray is less than or equal to k | |
var divideArray = function(nums, k) { | |
const arraySize = 3; // Size of each subarray | |
const result = []; // Array to store the result |
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} n | |
* @param {number} m | |
* @param {number} k | |
* @return {number} | |
*/ | |
var countGoodArrays = function(n, m, k) { | |
const MOD = 1e9 + 7; // Large prime number for modulo operations to prevent overflow | |
// Function to compute (a^b) % MOD using binary exponentiation (efficient power calculation) |
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 maximumDifference = function(nums) { | |
// Edge case: If the array has fewer than 2 elements, there's no valid pair. | |
if (nums.length < 2) return -1; | |
let minSoFar = nums[0]; // Track the smallest element seen so far | |
let maxDiff = -1; // Initialize max difference to -1 (default if no valid pair is found) |
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} num | |
* @return {number} | |
*/ | |
var maxDiff = function(num) { | |
let numStr = num.toString(); // Convert number to string for easy mainpulation | |
// **Step 1: Find the maximum number by replacing the first non-9 digit with 9** | |
let maxNumStr = numStr; |
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} num | |
* @return {number} | |
*/ | |
var minMaxDifference = function(num) { | |
let numStr = num.toString(); | |
// Finding the max number | |
let maxDigit = numStr.split('').find(d => d !== '9'); // First non-9 digit | |
let maxNum = parseInt(numStr.replaceAll(maxDigit, '9'), 10); |
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
/** | |
* Function to minimize the maximum difference between pairs in a sorted array. | |
* @param {number[]} nums - Array of numbers to be paired. | |
* @param {number} p - Target number of pairs. | |
* @return {number} - Minimum possible maximum difference between pairs. | |
*/ | |
var minimizeMax = function(nums, p) { | |
// Sort the numbers in ascending order | |
nums.sort((a, b) => a - b); |
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 maxAdjacentDistance = function(nums) { | |
if (nums.length < 2) return 0; // If there's only one element, no adjacent pairs exist. | |
let maxDiff = 0; // Initialize a variable to store the maximum absolute difference | |
for (let i = 0; i < nums.length; 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 maxDifference = function(s, k) { | |
const n = s.length; | |
// Convert each character to a digit (0-4 for faster lookup) | |
const digits = new Uint8Array(n); |