π¨βπ»
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 minOperations = function(nums, k) { | |
let queue = [], ans = 0, i = 0; | |
// Sort nums in descending order | |
nums.sort((a, b) => b - a); |
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} | |
*/ | |
// Helper function to calculate the sum of digits of a given number | |
function sumOfDigits(num) { | |
let total = 0; | |
while (num > 0) { | |
total += num % 10; | |
num = Math.floor(num / 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
/** | |
* @param {string} s | |
* @param {string} part | |
* @return {string} | |
*/ | |
var removeOccurrences = function(s, part) { | |
// Loop until there are no more occurrences of 'part' in 's' | |
while (s.includes(part)) { | |
// Replace the first occurrence of 'part' with an empty string | |
s = s.replace(part, ""); |
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 {string} | |
*/ | |
var clearDigits = function(s) { | |
while (true) { | |
let digitIndex = -1; | |
// Find the first digit in the string | |
for (let i = 0; i < s.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 | |
* @return {number} | |
*/ | |
var countBadPairs = function(nums) { | |
const len = nums.length; | |
// Total number of pairs possible in the array | |
const totalPairs = len * (len - 1) / 2; | |
const pairsMap = new Map(); | |
let good = 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
// Constructor for NumberContainers class | |
var NumberContainers = function() { | |
// Map to store the relationship between index and number | |
this.indexMap = new Map(); | |
// Map to store the relationship between number and MinHeap of indices | |
this.numberMap = new Map(); | |
}; | |
/** | |
* Change the number at a specific index |
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} limit | |
* @param {number[][]} queries | |
* @return {number[]} | |
*/ | |
var queryResults = function(limit, queries) { | |
// Map to store the color of each ball | |
const ballColors = new Map(); | |
// Map to store the frequency of each color | |
const colorFreqs = new Map(); |
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} s1 | |
* @param {string} s2 | |
* @return {boolean} | |
*/ | |
var areAlmostEqual = function(s1, s2) { | |
// Initialize an array to store the positions where s1 and s2 differ | |
let differences = []; | |
// Iterate through the strings to find differing positions |
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 maxAscendingSum = function(nums) { | |
// Initialize maxSum with the first element of the array | |
let maxSum = nums[0]; | |
// Initialize currentSum with the first element of the array | |
let currentSum = nums[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
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var longestMonotonicSubarray = function(nums) { | |
if (nums.length === 0) return 0; | |
let maxLength = 1; | |
let currentLength = 1; | |
let increasing = null; |