Latest Updates
LeetCode/problems/141-linked-list-cycle/141-linked-list-cycle.js
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */
/** * @param {ListNode} head * @return {boolean} */
var hasCycle = function(head){
let slow = head; let fast = head;
while(fast && fast.next){ slow = slow.next; fast = fast.next.next; if(slow === fast){return true;} }
return false;}
LeetCode/problems/2226-maximum-candies-allocated-to-k-children/2226-maximum-candies-allocated-to-k-children.js
/** * @param {number[]} candies * @param {number} k * @return {number} */var maximumCandies = function(candies, k) { let blah = function(I){ return k <= candies.reduce( (sum,el)=>sum + Math.floor(el/I), 0); }
let L = 0; let R = Math.max(...candies); let M = Math.floor( (L+R)/2 ); while(L<=R){ M = Math.floor( (L+R)/2 ); if(blah(M) ){ L = M+1; }else{ R = M-1; } }
return R;};
LeetCode/problems/3356-zero-array-transformation-ii/3356-zero-array-transformation-ii.js
/** * @param {number[]} nums * @param {number[][]} queries * @return {number} */var minZeroArray = function(nums, queries) { let sweep = new Array(nums.length+1).fill(0);
let prefixSum = 0; let Q = 0; for(let I = 0; I<nums.length; I++){ while(prefixSum + sweep[I] < nums[I]){ if( Q >= queries.length){ return -1; }
let [left, right, val] = queries[Q++];
if(right >= I){ sweep[ Math.max(left,I)] += val; sweep[right+1] += -val; } } prefixSum += sweep[I]; } return Q;};
Templates/binarySearch/binarySearch.js
function binarySearch(arr, target) { let left = 0, right = arr.length - 1; while (left <= right) { let mid = Math.floor((left + right) / 2); if (arr[mid] === target) return mid; // Found else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; // Not found}