LeetCode - 150 - Binary Search

The problem Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Examples Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1 Constraints 1 <= nums.length <= 10^4 -10^4 < nums[i], target < 10^4 All the integers in nums are unique. nums is sorted in ascending order. Explanation Before we jump into the solution, let’s figure out what the requirements for a binary search algorithm are and how it is going to work. The main requirement for binary search is that the input must be sorted. ...

June 27, 2025 · 3 min · Dmytro Chumakov

LeetCode - 150 - Largest Rectangle in Histogram

The problem Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. Examples Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. ...

June 24, 2025 · 5 min · Dmytro Chumakov

LeetCode - 150 - Car Fleet

The problem There are n cars at given miles away from the starting mile 0, traveling to reach the mile target. You are given two integer array position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour. A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car. ...

June 18, 2025 · 5 min · Dmytro Chumakov

LeetCode - 150 - Daily Temperatures

The problem Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead. Examples Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Input: temperatures = [30,40,50,60] Output: [1,1,1,0] Input: temperatures = [30,60,90] Output: [1,1,0] Constraints 1 <= temperatures.length <= 10^5 30 <= temperatures[i] <= 100 Explanation Before we jump to the solution, let’s figure out the way we can solve this problem. In the first example, we can see that we can calculate how many days in the input array it takes us to find a temperature that is greater than 73. ...

June 12, 2025 · 3 min · Dmytro Chumakov

LeetCode - 150 - Generate Parentheses

The problem Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Examples Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Input: n = 1 Output: ["()"] Constraints 1 <= n <= 8 Explanation Before we jump to the solution, let’s figure out what well-formed parentheses mean. In this problem, this means when you’re writing the code using nested parentheses, you want them to be nested in a valid way, like (()()). ...

June 9, 2025 · 2 min · Dmytro Chumakov