π¨βπ»
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++) { |
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 calculate the maximum points that can be earned | |
* by answering questions strategically. | |
* @param {number[][]} questions - Array of questions, where each question is represented as [points, brainpower]. | |
* @return {number} - The maximum points that can be achieved. | |
*/ | |
var mostPoints = function(questions) { | |
const n = questions.length; | |
// Memoization array to store results of subproblems |
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 difference between the maximum and minimum scores | |
* of distributing marbles into k bags based on given rules. | |
* | |
* @param {number[]} weights - Array representing the weights of marbles. | |
* @param {number} k - Number of bags to divide the marbles into. | |
* @return {number} - Difference between the maximum and minimum scores. | |
*/ | |
var putMarbles = function(weights, k) { | |
// If there's only one bag, no need to calculate; the difference is 0. |
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 get the index of a character in the alphabet (0 for 'a', 1 for 'b', etc.) | |
const getIndexCharacter = (c) => { | |
return c.charCodeAt(0) - 'a'.charCodeAt(0); | |
} | |
/** | |
* Function to partition the string into as many parts as possible | |
* such that each character appears in at most one part. | |
* | |
* @param {string} s - Input string |