π¨βπ»
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); |
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 {number} | |
*/ | |
var maxDifference = function(s) { | |
// Step 1: Count frequencies of each character | |
const freqMap = {}; // Object to store frequency of each character | |
for (const char of s) { | |
freqMap[char] = (freqMap[char] || 0) + 1; // Increment frequency count |
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 clearStars = function(s) { | |
// Array to store indices of characters ('a' to 'z') | |
const indices = Array.from({ length: 26 }, () => []); | |
// Convert the string into an array for easier modifications | |
const res = s.split(''); |
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 robotWithString = function(s) { | |
let ans = ""; // Stores the final result string | |
let stack = []; // Acts as a temporary storage for characters | |
let freq = new Array(26).fill(0); // Keeps track of character frequencies | |
// Populate frequency array for all characters in the string |
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 | |
* @param {string} baseStr | |
* @return {string} | |
*/ | |
class UnionFind { | |
constructor() { | |
this.parent = Array.from({ length: 26 }, (_, i) => i); // 'a' to 'z' mapped to 0-25 | |
} |
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 lexicographically largest substring based on the number of friends. | |
* If `numFriends` is 1, the whole word is returned. | |
* Otherwise, the function finds the best substring by iterating through possible slices. | |
* | |
* @param {string} word - The input word. | |
* @param {number} numFriends - The number of friends involved in the selection process. | |
* @return {string} - The lexicographically largest substring. | |
*/ | |
var answerString = function (word, numFriends) { |
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[]} status - Array indicating whether each box is open (1) or closed (0). | |
* @param {number[]} candies - Number of candies in each box. | |
* @param {number[][]} keys - List of keys contained in each box. | |
* @param {number[][]} containedBoxes - List of boxes contained in each box. | |
* @param {number[]} initialBoxes - Labels of the boxes you start with. | |
* @return {number} - Maximum number of candies you can collect. | |
*/ | |
var maxCandies = function(status, candies, keys, containedBoxes, initialBoxes) { | |
const queue = [...initialBoxes]; // Queue to process available boxes |
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[]} ratings | |
* @return {number} | |
*/ | |
var candy = function(ratings) { | |
const length = ratings.length; | |
// Initialize candies array, giving each child at least one candy. | |
const candies = new Array(length).fill(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
/** | |
* Calculates the number of ways to distribute `n` candies among 3 children | |
* ensuring no child gets more than `limit` candies. | |
* | |
* @param {number} n - Total number of candies. | |
* @param {number} limit - Maximum candies each child can receive. | |
* @return {number} - The number of valid distributions. | |
*/ | |
var distributeCandies = function(n, limit) { | |
let count = 0; // Stores the total number of valid distributions. |
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 closest meeting node between two starting nodes in a directed graph. | |
* | |
* @param {number[]} edges - Array representing directed edges of the graph. | |
* @param {number} node1 - First starting node. | |
* @param {number} node2 - Second starting node. | |
* @return {number} - The closest meeting node or -1 if no valid meeting node exists. | |
*/ | |
// Function to initialize an adjacency list representation of the graph |