π¨βπ»
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 check if two horizontal or two vertical cuts can divide the grid | |
* into three sections, with each section containing at least one rectangle. | |
* @param {number} n - The dimensions of the n x n grid (not directly used here). | |
* @param {number[][]} rectangles - Array of rectangles where each rectangle is defined as [startx, starty, endx, endy]. | |
* @return {boolean} - Returns true if valid cuts can be made; otherwise, false. | |
*/ | |
var checkValidCuts = function(n, rectangles) { | |
// Arrays to store x and y intervals for each rectangle | |
let x = []; |
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 number of days an employee is free from meetings. | |
* | |
* @param {number} days - Total number of days the employee is available for work. | |
* @param {number[][]} meetings - Array of meeting intervals where each meeting is [start, end]. | |
* @returns {number} - The count of days when the employee is free from meetings. | |
*/ | |
var countDays = function(days, meetings) { | |
// Sort meetings by their start day to process them in chronological order | |
meetings.sort((a, b) => a[0] - b[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} 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; |