The Data Pair$\textit{people}[h, k]$, $h_i$ represents the i-th person of height $h_i$, $k_i$ represents the other people in front who have a height greater than or equal to $h_i$.

Pattern:

When face such kind of problem with Data Pair, sorting first will simplify the problem.

Usually, we will sort the first element in ascending order, while the second element in descending order, or sort the first element in descending order with the second element in ascending order.

Greedy

We sort the $\textit{people}$ pairs first by $height$ in descending order, and $k$ in ascending order.

According to the descending order of $height$, for each person, the number of people before him is the number of people whose height is greater than or equal to him.

According to the ascending order of $k$, because the $k$ is increase later, which can reduce the number of insert operations.

While we are moving, we will need to use bricks or ladders several times. Suppose we have to move to the next building, which means we must use one ladder or $\Delta h$ bricks, so there is a question, whether to use ladders or bricks?

If the gap of buildings is large, we may use the ladder, otherwise we use the bricks.

We can consider ladder as a one-time unlimited number of bricks, That is, if we have l ladders, we will use the ladder on the $l$ times where $\Delta h$ is the largest, and use the bricks in the rest.

Therefore, we got the answer. We maintain no more than $l$ largest $\Delta h$ using priority queues, and these are where the ladders are used. For the remaining $\Delta h$, we need to use bricks, so we need to accumulate them, if the $sum$ exceeds the number of bricks $b$, then we have move to the furthest building.

int min = dp[len - 1][0]; for (int i = 1; i < len; i++) { min = Math.min(min, dp[len - 1][i]); }

return min; }

In fact, $dp[i][j]$ is only relevant to $dp[i-1][j]$, but $dp[i-2][j]$ and previous states are irrelevant, so we don’t have to store these states. We can only use extra $O(2n)$ space: two one-dimensional arrays of length $n$ for the transfer, and map $i$ to one of the one-dimensional arrays according to the parity, then $i-1$ is mapped to the other one-dimensional array.

To simulate the CPU operations, there comes $3$ questions:

Which task should int the enqueued tasks list?

How to maintain the enqueued tasks?

Which task of the enqueued tasks should the CPU choose?

Let’s answer the $3$ questions:

We assign the tasks to the CPU by $\textit{enqueueTime}$, so we sort the array first by $\textit{enqueueTime}$. However, we will lose the $\textit{index}$ of the task.

We can parse the task by creating a new class $\texttt{Job}$, whose members are $\textit{id}$, $\textit{enqueueTime}$, $\textit{processTime}$.

We put all the tasks assigned to the CPU into a Priority Queue and poll out the task whose $\textit{processTime}$ is the least each time.

We can maintain a $\textit{curTime}$ variable, which represents the current time with initial value is $0$.

If the CPU has no tasks to execute, we set the $\textit{curTime}$ to $\textit{enqueueTime}$ of the next task in the array that has not yet been assigned to the CPU.

After this, we put all $\textit{enqueueTime} \le \textit{curTime}$ tasks into the Priority Queue.

Here shows 5 Approaches to slove this problem: BF use long, BF use int, Binary Search use Long, Binary Search use Int, Recursion.

Intuition

To divide two integers without using multiplication, division, and mod operator, so we can subtract the $\textit{divisor}$ from the $\textit{dividend}$ util the $\textit{result} \ge 0$.

Brute Force use Long

We can make the $\textit{dividend}$ and $\textit{divisor}$ positive, and cast to long, then process.