π¨βπ»
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 - Number of intersections (nodes) | |
* @param {number[][]} roads - 2D array representing roads where [a, b, time] means | |
* a road exists between a and b with travel time "time" | |
* @return {number} - Number of ways to reach intersection (n-1) in the shortest time | |
*/ | |
var countPaths = function(n, roads) { | |
const MOD = 1_000_000_007; // Modular arithmetic base | |
// PART ONE: Build the graph and calculate shortest distances |
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[][]} edges | |
* @return {number} | |
*/ | |
var countCompleteComponents = function(n, edges) { | |
// Initialize parent array (each vertex is its own parent initially) | |
let parent = Array.from({ length: n }, (_, i) => i); | |
// Rank array to optimize Union-Find operations | |
let rank = Array(n).fill(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 {string[]} recipes | |
* @param {string[][]} ingredients | |
* @param {string[]} supplies | |
* @return {string[]} | |
*/ | |
var findAllRecipes = function(recipes, ingredients, supplies) { | |
// Step 1: Initialization | |
const n = recipes.length; // Get the number of recipes | |
const graph = new Map(); // Represents dependencies: ingredients point to recipes they can help create |
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 cost to connect queried nodes in a weighted graph. | |
* | |
* @param {number} n - The number of nodes in the graph. | |
* @param {number[][]} edges - The edges of the graph, where each edge is represented as [u, v, w]. | |
* @param {number[][]} query - An array of queries, where each query is represented as [s, t]. | |
* @return {number[]} - An array where each element corresponds to the minimum cost of connecting the queried nodes. | |
*/ | |
var minimumCost = function(n, edges, query) { | |
// Initialize Union-Find (Disjoint Set Union) structures |
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 minOperations = function(nums) { | |
let count = 0; // Initialize the counter for the number of flips | |
// Helper function to flip a single element at index i | |
function flip(i) { | |
nums[i] = nums[i] === 0 ? 1 : 0; // Toggle the element (0 -> 1, 1 -> 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
/** | |
* Finds the length of the longest "nice" subarray. | |
* A "nice" subarray is one where the bitwise AND of all elements is zero. | |
* | |
* @param {number[]} nums - Array of integers. | |
* @return {number} - Length of the longest nice subarray. | |
*/ | |
var longestNiceSubarray = function(nums) { | |
let windowSizeMax = 1; // Tracks the maximum length of the nice subarray. | |
let left = 0; // Left pointer of the sliding window. |
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 {boolean} | |
*/ | |
var divideArray = function(nums) { | |
// Create a map to store the frequency of each number | |
let frequencyMap = new Map(); | |
let length = nums.length; | |
// Populate the frequency 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 {number[]} ranks | |
* @param {number} cars | |
* @return {number} | |
*/ | |
var repairCars = function (ranks, cars) { | |
// Initialize the binary search range: | |
// 'left' represents the minimum possible time (1 minute). | |
// 'right' is the maximum possible time (if the slowest mechanic repairs all cars). | |
let left = 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
/** | |
* @param {number[]} nums | |
* @param {number} k | |
* @return {number} | |
*/ | |
var minCapability = function(nums, k) { | |
// Define the binary search range: minimum and maximum house values. | |
let left = Math.min(...nums), right = Math.max(...nums); | |
// Function to check if a given capability can rob at least `k` house. |
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[]} candies | |
* @param {number} k | |
* @return {number} | |
*/ | |
var maximumCandies = function(candies, k) { | |
// Helper function to check if it's possible to distribute 'target' candies to 'k' children | |
function canDistribute(candies, k, target) { | |
let count = 0; // Count the number of children that can receive 'target' candies | |
for (let pile of candies) { |