Count Negative Numbers in a Sorted Matrix Solution In C++/Java/Python/JS Problem Description Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid. Examples Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] Output: 8 Explanation: There
Search a 2D Matrix Solution In C++/Java/Python/JS Problem Description You are given an m x n integer matrix matrix with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer target, return true if target is in
Search a 2D Matrix II Solution In C++/Java/Python/JS Problem Description Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.
Maximum Number of Robots Within Budget Solution In C++/Java/Python/JS Maximum Number of Robots Within Budget Problem Description: You have n robots. You are given two 0-indexed integer arrays, chargeTimes and runningCosts, both of length n. The i-th robot costs chargeTimes[i] units to charge and costs runningCosts[i] units to run. You are also given an integer budget. The
Maximum Running Time of N Computers Solution In C++/Java/Python/JS Maximum Running Time of N Computers Problem Description: You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries. Initially,
Split Array Largest Sum Solution In C++/Java/Python/JS Split array largest sum Problem Description: Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. Return the minimized largest sum of the split. A subarray is a contiguous part of the array. Examples: Input:
Minimum Number of Days to Make m Bouquets Solution In C++/Java/Python/JS Problem Description: You are given an integer array bloomDay, an integer m and an integer k. You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the i-th flower will bloom in the bloomDay[
Find the Smallest Divisor Given a Threshold Solution In C++/Java/Python/JS Problem Description: Given an array of integers nums and an integer threshold, we will choose a positive integer divisor, divide all the array by it, and sum the division's result. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each
Koko Eating Bananas Solution In C++/Java/Python/JS Problem Description: Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours. Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats
Capacity To Ship Packages Within D Days Solution In C++/Java/Python/JS Problem Description: A conveyor belt has packages that must be shipped from one port to another within D days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights)
Find in Mountain Array Solution In C++/Java/Python/JS Problem Description: (This problem is an interactive problem.) You may recall that an array arr is a mountain array if and only if: * arr.length >= 3 * There exists some i with 0 < i < arr.length - 1 such that: * arr[0] < arr[1] < ... < arr[
Find Minimum in Rotated Sorted Array II Solution In C++/Java/Python/JS Problem Description: Suppose an array of length n, sorted in ascending order, is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: * [4,5,6,7,0,1,4] if it was rotated 4 times. * [0,1,4,4,
Find the Duplicate Number Solution In C++/Java/Python/JS Find the Duplicate Number Problem Description: Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and using
Minimum Common Value Solution In C++/Java/Python/JS Minimum Common Value Problem Description: Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer among nums1 and nums2, return -1. An integer is said to be common to nums1 and nums2 if both arrays
Single Element in a Sorted Array Solution In C++/Java/Python/JS Problem Description: You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Return the single element that appears only once. Your solution must run in O(log n) time and O(1) space. Examples: Input: nums
Kth Missing Positive Number Solution In C++/Java/Python/JS Problem Description: Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. Return the k-th positive integer that is missing from this array. Examples: Input: arr = [2, 3, 4, 7, 11], k = 5 Output: 9 Explanation: The missing positive integers are [1, 5,
Most Beautiful Item for Each Query Solution In C++/Java/Python/JS Problem Description: You are given a 2D integer array items where items[i] = [priceᵢ, beautyᵢ] denotes the price and beauty of an item respectively. You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is
Online Election Solution In C++/Java/Python/JS Problem Description: You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i]. For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will
Find Target Indices After Sorting Array Solution In C++/Java/Python/JS Problem Description: You are given a 0-indexed integer array nums and a target element target. A target index is an index i such that nums[i] == target. Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an
Search Insert Position Solution In C++/Java/Python/JS Problem Description: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Examples: Input: nums
Find the Distance Value Between Two Arrays Solution In C++/Java/Python/JS Problem Description: Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays. The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where | arr1[i]-arr2[j] | <= d. Examples:
First Bad Version Solution in C++/Java/Python/JS Problem Description: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you
Find Smallest Letter Greater Than Target Solution in C++/Java/Python/JS Problem Description: You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters. Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the
Binary Search Arranging Coins Solution In C++/Java/Python/JS Problem Description: You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. Given the integer n, return the number of complete rows of
Binary Search Longest Subsequence with Limited Sum Solution in C++/Java/Python/JS Problem Description: You are given an integer array nums of length n, and an integer array queries of length m. Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements