LeetCode 435. Non-overlapping Intervals
Question
Given an array of intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Given an array of intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points
where points[i] = [xstart, xend]
denotes a balloon whose horizontal diameter stretches between xstart
and xend
. You do not know the exact y-coordinates of the balloons.
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart
and xend
is burst by an arrow shot at x
if xstart <= x <= xend
. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
Given the array points
, return the minimum number of arrows that must be shot to burst all balloons.
Given an array of meeting time intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
Given an array of meeting time intervals
where intervals[i] = [starti, endi]
, determine if a person could attend all meetings.
You are given an integer array prices
where prices[i]
is the price of a given stock on the ith
day, and an integer k
.
Find the maximum profit you can achieve. You may complete at most k
transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
1 | Input: k = 2, prices = [2,4,1] |
Example 2:
1 | Input: k = 2, prices = [3,2,6,5,0,3] |
Constraints:
0 <= k <= 100
0 <= prices.length <= 1000
0 <= prices[i] <= 1000
Source: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
To find an efficient solution for this type of problem, we need to use DP.
However, let us consider an extreme case fisrt. We are allow to make at most k
transactions. If k
is large enough to enable us to grab every profit, we do not have to do DP but just traverse the array once. The threshold of this k
is n/2
. Because a stock can rise at most n/2
times (two rises on two adjacent days can be merged into one). This is the idea of quickSolve()
.
For DP, the essence of DP is to solve small sub-problems first, and derive the results from these small sub-problem to larger problems. This max profit problem has two dimensions: 1. the number of days; 2. the max number of transactions allowed. So we creaste a 2D DP cache. And because deriving results from fewer days to more days is easier than changing max number of transactions, we make k+1
(more convenient than k
) the first dimension and n
the second dimension. Always use a variable that is easier to derive as the second dimension in 2D DP.
dp[i][j]
represents the max profit of prices[:j]
(inclusive) with at most i
transactions. Thus, there are only two options for dp[i][j]
: sell the stock on j
th day or not. If sell, we need to iterate all possible cases to find the max profit. If not, dp[i][j]
should be equal to dp[i][j-1]
.
So, we can get the following formulas for these two situations:
dp[i][j] = dp[i][j-1]
.t
is the date the last transaction was brought. dp[i][j] = for t: 0->j-1, max(dp[i-1][t-1]+prices[j]-prices[t])
.This solution still has a O((n^2)*k)
time complexity. The bottleneck is the second formula. However, we can further optimize it. It is equivalent to for t: 0->j-1, max(dp[i-1][t-1]-prices[t])+prices[j]
. Thus, we can find the max of the second formula while iterating 0 to j-1 once.
Finally, the time complexity is optimized to O(nk)
.
1 | // return the max profit without limiting the number of transactions |
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
Given an n x n
matrix
where each of the rows and columns is sorted in ascending order, return the kth
smallest element in the matrix.
Note that it is the kth
smallest element in the sorted order, not the kth
distinct element.
You must find a solution with a memory complexity better than O(n^2)
.
Given an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
$a\equiv b\space(\text{mod}\space n)$ means a mod n = b mod n
. The parentheses mean that $(\text{mod}\space n)$ applies to the entire equation, not just to the right-hand side.
Reflexivity, Symmetry and Transitivity:
If $a_1\equiv b_1\space(\text{mod}\space n)$ and $a_2\equiv b_2\space(\text{mod}\space n)$, or if $a\equiv b\space(\text{mod}\space n)$, then:
Compatibility with translation, scaling, addition, subtraction, multiplication, exponentiation and polynomial evaluation.
2^16
: two to the power of sixteen. 3^4
: three to the power of four. x^2
: x squared. x^3
: x cubed.