π¨βπ»
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} k | |
* @return {number} | |
*/ | |
var countGoodIntegers = function(n, k) { | |
// Precalculate factorial values for combinatorics | |
const memoF = Array(n); // Memoization array for factorial values | |
memoF[0] = 1; // Base case: 0! = 1 | |
for (let i = 1; i < n; 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} low | |
* @param {number} high | |
* @return {number} | |
*/ | |
// Function to check if a number has an even number of digits | |
function hasEvenDigits(num) { | |
const digitCount = num.toString().length; | |
return digitCount % 2 === 0; // True if the digit count is even | |
} |
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 number of "powerful integers" between the range `start` and `finish`. | |
* A "powerful integer" is determined by the given `limit` and string representation `s`. | |
* | |
* @param {number} start - Start of the range (inclusive). | |
* @param {number} finish - End of the range (inclusive). | |
* @param {number} limit - Maximum digit allowed in the calculation. | |
* @param {string} s - String representation of a threshold value. | |
* @return {number} - The number of powerful integers in the range. | |
*/ |
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
/** | |
* This function calculates the minimum operations required to satisfy certain conditions. | |
* @param {number[]} nums - Array of numbers to process. | |
* @param {number} k - Threshold value to compare against. | |
* @return {number} - Returns the size of the unique set of numbers greater than k, or -1 if any number is less than k. | |
*/ | |
var minOperations = function (nums, k) { | |
// Check if any number in the array is less than k. If so, return -1. | |
for (let num of nums) { | |
if (num < k) return -1; |
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 minimum number of operations required to make all elements in the array distinct | |
* by removing groups of 3 elements from the start of the array. | |
* | |
* @param {number[]} nums - The input array of integers. | |
* @return {number} - The minimum number of operations required. | |
*/ | |
var minimumOperations = function(nums) { | |
// Create an array to track the elements that have already appeared | |
let appeared = new Array(101); // This seems to assume the numbers are in the range [0, 100]. |
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
/** | |
* Determines if the given array of numbers can be partitioned into two subsets with equal sum. | |
* @param {number[]} nums - Array of integers | |
* @return {boolean} - True if the array can be partitioned, otherwise false | |
*/ | |
var canPartition = function(nums) { | |
const total = nums.reduce((a, b) => a + b, 0); | |
// If the total is odd, partitioning into two equal subsets is not possible | |
if (total % 2 !== 0) return false; |
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 largest subset of numbers from `nums` such that every pair (i, j) | |
* in the subset satisfies: | |
* nums[i] % nums[j] === 0 or nums[j] % nums[i] === 0. | |
* | |
* @param {number[]} nums - Array of distinct positive integers. | |
* @return {number[]} - The largest divisible subset. | |
*/ | |
var largestDivisibleSubset = function(nums) { | |
// Edge case: If the input is empty, return an empty subset. |
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
/** | |
* 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 |
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 maximumTripletValue = function(nums) { | |
const n = nums.length; | |
if (n < 3) return 0; // Edge case: no valid triplet possible | |
// Arrays to store the maximum values | |
const leftMax = new Array(n).fill(Number.MIN_SAFE_INTEGER); |
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 maximumTripletValue = function(nums) { | |
// Initialize the maximum value to 0 (default if all values are negative) | |
let maxValue = 0; | |
// Outer loop: Fix the first index i | |
for (let i = 0; i < nums.length - 2; i++) { |