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[][]} fruits | |
* @param {number} startPos | |
* @param {number} k | |
* @return {number} | |
*/ | |
var maxTotalFruits = function(fruits, startPos, k) { | |
let max = 0; | |
let left = 0; | |
let sum = 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[]} basket1 | |
* @param {number[]} basket2 | |
* @return {number} | |
*/ | |
var minCost = function(basket1, basket2) { | |
const freq1 = new Map(); | |
const freq2 = new Map(); | |
const combinedFreq = new Map(); | |
const swaps = []; |
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} numRows | |
* @return {number[][]} | |
*/ | |
var generate = function(numRows) { | |
if (numRows <= 0) return []; | |
const result = []; | |
for (let i = 0; i < numRows; 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[]} arr | |
* @return {number} | |
*/ | |
var subarrayBitwiseORs = function(arr) { | |
// A set to store all unique OR results | |
const resultSet = new Set(); | |
// This set will track ORs of subarrays ending at the current index | |
let currentSet = new Set(); |
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 smallestSubarrays = function(nums) { | |
const n = nums.length; | |
const answer = new Array(n).fill(1); // Result array | |
const latestSeenBit = new Array(32).fill(-1); // Tracks last index each bit is seen | |
for (let i = n - 1; i >= 0; 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 countHillValley = function(nums) { | |
// Step 1: Remove consecutive duplicates for easier neighbor comparisons | |
const filtered = [nums[0]]; | |
for (let i = 1; i < nums.length; i++) { | |
if (nums[i] !== filtered[filtered.length - 1]) { | |
filtered.push(nums[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
/** | |
* Calculates the maximum number of valid subarrays from 1 to n, | |
* excluding conflicting pairs (each conflict prevents both elements | |
* from being in the same subarray). | |
* | |
* @param {number} n - The size of the array (from 1 to n). | |
* @param {number[][]} conflictingPairs - Array of conflict pairs [u, v]. | |
* @return {number} - Maximum number of valid subarrays. | |
*/ | |
var maxSubarrays = function(n, conflictingPairs) { |
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 maximum sum of unique positive numbers in the array. | |
* If no positive numbers are present, returns the largest (least negative) number. | |
* Note: This does not enforce subarray contiguity. | |
* | |
* @param {number[]} nums - Array of integers | |
* @return {number} - Maximum sum of unique positives or max negative value | |
*/ | |
var maxSum = function(nums) { | |
// Boolean flags to track numbers from 1 to 100 |
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[][]} edges | |
* @return {number} | |
*/ | |
var minimumScore = function(nums, edges) { | |
const n = nums.length; | |
const tree = Array.from({ length: n }, () => []); | |
// \U0001f527 Build adjacency list for the tree |
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 maximumUniqueSubarray = function(nums) { | |
let seen = new Set(); // To track unique elements in the current window | |
let left = 0; // Left boundary of the sliding window | |
let maxScore = 0; // To Keep track of the maximum score found | |
let currentSum = 0; // Sum of the current window |