π¨βπ»
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; |
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 check = function(nums) { | |
// An empty array is considered sorted and rotated | |
if (nums.length === 0) { | |
return true; | |
} |
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 isArraySpecial = function(nums) { | |
// Loop through the array up to the second last element | |
for (let i = 0; i < nums.length - 1; i++) { | |
// Check if the current element and the next element have the same parity | |
// Parity means being either both even or both odd | |
if ((nums[i] % 2) === (nums[i + 1] % 2)) { |
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[][]} grid | |
* @return {number} | |
*/ | |
var largestIsland = function(grid) { | |
const n = grid.length; | |
// Helper function to perform DFS and mark island cells with a unique island ID | |
function dfs(x, y, islandId) { | |
const stack = [[x, y]]; |
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 magnificentSets = function(n, edges) { | |
// Initialize Union-Find structure for connected components | |
let uf = new UnionFind(n + 1); | |
// Initialize adjacency list for the graph | |
let graph = Array(n + 1).fill(0).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[][]} edges | |
* @return {number[]} | |
*/ | |
class UnionFind { | |
// Constructor to initialize parent and rank arrays. | |
constructor(len) { | |
// Create parent array where each node is its own parent initially. | |
this.parent = Array.from({ length: len + 1 }, (_, i) => 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[][]} grid | |
* @return {number} | |
*/ | |
var findMaxFish = function(grid) { | |
// Initialize the maximum fish count as 0 | |
let maxFish = 0; | |
// Get the number of rows and columns in the grid | |
const rows = grid.length; |
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} numCourses | |
* @param {number[][]} prerequisites | |
* @param {number[][]} queries | |
* @return {boolean[]} | |
*/ | |
function buildGraph(prerequisites) { | |
// Initialize an adjacency list to store the graph | |
const graph = {}; | |
for (let [a, b] of prerequisites) { |
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[]} favorite | |
* @return {number} | |
*/ | |
var maximumInvitations = function(favorite) { | |
const n = favorite.length; | |
let longestCycle = 0; // Variable to track the longest cycle found | |
const visit = Array.from({ length: n }, () => false); // Boolean array to track visited nodes | |
const len2Cycles = []; // Array to track cycles of length 2 |