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
| /** | |
| * Finds the next "beautiful" number greater than the given input. | |
| * A beautiful number is defined as one where each digit appears exactly as many times as its value. | |
| * For example, 22 is beautiful because digit 2 appears twice. | |
| * | |
| * @param {number} n - The input number. | |
| * @return {number} - The next beautiful number greater than n. | |
| */ | |
| var nextBeautifulNumber = function(n) { | |
| // Precomputed list of beautiful numbers in ascending order. |
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 | |
| * @return {boolean} | |
| */ | |
| var hasSameDigits = function(s) { | |
| // Convert the input string into an array of digits (as numbers) | |
| let digits = s.split('').map(Number); | |
| // Repeat the transformation until only two digits remain | |
| while (digits.length > 2) { |
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 | |
| * @param {number} numOperations | |
| * @return {number} | |
| */ | |
| var maxFrequency = function (nums, k, numOperations) { | |
| // Sort the array to enable binary search and range grouping | |
| 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 - The input array of integers | |
| * @param {number} k - The maximum value that can be added/subtracted in one operation | |
| * @param {number} numOperations - The total number of operations allowed | |
| * @return {number} - The maximum frequency of any element after operations | |
| */ | |
| var maxFrequency = function(nums, k, numOperations) { | |
| const n = nums.length; | |
| // Sort the array to enable binary search and range grouping |
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[]} operations | |
| * @return {number} | |
| */ | |
| var finalValueAfterOperations = function(operations) { | |
| // Step 1: Initialize the variable X to 0 | |
| let X = 0; | |
| // Step 2: Loop through each operation in the array | |
| for (let i = 0; i < operations.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} a | |
| * @param {number} b | |
| * @return {string} | |
| */ | |
| var findLexSmallestString = function(s, a, b) { | |
| // Set to keep track of visited strings to avoid revisiting the same state | |
| const visited = new Set(); |
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 number of partitions after performing operations | |
| * that allow up to `k` distinct characters per partition. | |
| * | |
| * @param {string} s - Input string consisting of lowercase letters. | |
| * @param {number} k - Maximum number of distinct characters allowed per partition. | |
| * @return {number} - Maximum number of partitions achievable. | |
| */ | |
| var maxPartitionsAfterOperations = function(s, k) { | |
| const n = s.length; |
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} value | |
| * @return {number} | |
| */ | |
| var findSmallestInteger = function(nums, value) { | |
| // Step 1: Create a frequency map to count how many times each remainder appears | |
| const freq = new Map(); | |
| for (let 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
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var maxIncreasingSubarrays = function (nums) { | |
| let maxK = 0; // Stores the maximum valid k found so far | |
| let curLen = 1; // Length of the current strictly increasing run | |
| let prevLen = 0; // Length of the previous strictly increasing run | |
| for (let i = 1; 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 {number[]} nums | |
| * @param {number} k | |
| * @return {boolean} | |
| */ | |
| var hasIncreasingSubarrays = function(nums, k) { | |
| // Helper function to check if a subarray is strictly increasing | |
| function isStrictlyIncreasing(start, end) { | |
| for (let i = start; i < end; i++) { | |
| if (nums[i] >= nums[i + 1]) { |