mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-08 00:41:42 +08:00
update
This commit is contained in:
56
leetcode/problem/build-a-matrix-with-conditions.html
Normal file
56
leetcode/problem/build-a-matrix-with-conditions.html
Normal file
@@ -0,0 +1,56 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
|
||||
|
||||
<ul>
|
||||
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
|
||||
<li>a 2D integer array <code>colConditions</code> of size <code>m</code> where <code>colConditions[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The two arrays contain integers from <code>1</code> to <code>k</code>.</p>
|
||||
|
||||
<p>You have to build a <code>k x k</code> matrix that contains each of the numbers from <code>1</code> to <code>k</code> <strong>exactly once</strong>. The remaining cells should have the value <code>0</code>.</p>
|
||||
|
||||
<p>The matrix should also satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number <code>above<sub>i</sub></code> should appear in a <strong>row</strong> that is strictly <strong>above</strong> the row at which the number <code>below<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>n - 1</code>.</li>
|
||||
<li>The number <code>left<sub>i</sub></code> should appear in a <strong>column</strong> that is strictly <strong>left</strong> of the column at which the number <code>right<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>m - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em><strong>any</strong> matrix that satisfies the conditions</em>. If no answer exists, return an empty matrix.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/06/gridosdrawio.png" style="width: 211px; height: 211px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
|
||||
<strong>Output:</strong> [[3,0,0],[0,0,1],[0,2,0]]
|
||||
<strong>Explanation:</strong> The diagram above shows a valid example of a matrix that satisfies all the conditions.
|
||||
The row conditions are the following:
|
||||
- Number 1 is in row <u>1</u>, and number 2 is in row <u>2</u>, so 1 is above 2 in the matrix.
|
||||
- Number 3 is in row <u>0</u>, and number 2 is in row <u>2</u>, so 3 is above 2 in the matrix.
|
||||
The column conditions are the following:
|
||||
- Number 2 is in column <u>1</u>, and number 1 is in column <u>2</u>, so 2 is left of 1 in the matrix.
|
||||
- Number 3 is in column <u>0</u>, and number 2 is in column <u>1</u>, so 3 is left of 2 in the matrix.
|
||||
Note that there may be multiple correct answers.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.
|
||||
No matrix can satisfy all the conditions, so we return the empty matrix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= k <= 400</code></li>
|
||||
<li><code>1 <= rowConditions.length, colConditions.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>rowConditions[i].length == colConditions[i].length == 2</code></li>
|
||||
<li><code>1 <= above<sub>i</sub>, below<sub>i</sub>, left<sub>i</sub>, right<sub>i</sub> <= k</code></li>
|
||||
<li><code>above<sub>i</sub> != below<sub>i</sub></code></li>
|
||||
<li><code>left<sub>i</sub> != right<sub>i</sub></code></li>
|
||||
</ul>
|
34
leetcode/problem/longest-subsequence-with-limited-sum.html
Normal file
34
leetcode/problem/longest-subsequence-with-limited-sum.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>maximum</strong> size of a <strong>subsequence</strong> that you can take from </em><code>nums</code><em> such that the <strong>sum</strong> of its elements is less than or equal to </em><code>queries[i]</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,2,1], queries = [3,10,21]
|
||||
<strong>Output:</strong> [2,3,4]
|
||||
<strong>Explanation:</strong> We answer the queries as follows:
|
||||
- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
|
||||
- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
|
||||
- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,4,5], queries = [1]
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>garbage</code> where <code>garbage[i]</code> represents the assortment of garbage at the <code>i<sup>th</sup></code> house. <code>garbage[i]</code> consists only of the characters <code>'M'</code>, <code>'P'</code> and <code>'G'</code> representing one unit of metal, paper and glass garbage respectively. Picking up <strong>one</strong> unit of any type of garbage takes <code>1</code> minute.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> integer array <code>travel</code> where <code>travel[i]</code> is the number of minutes needed to go from house <code>i</code> to house <code>i + 1</code>.</p>
|
||||
|
||||
<p>There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house <code>0</code> and must visit each house <strong>in order</strong>; however, they do <strong>not</strong> need to visit every house.</p>
|
||||
|
||||
<p>Only <strong>one</strong> garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks <strong>cannot</strong> do anything.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of minutes needed to pick up all the garbage.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> garbage = ["G","P","GP","GG"], travel = [2,4,3]
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong>
|
||||
The paper garbage truck:
|
||||
1. Travels from house 0 to house 1
|
||||
2. Collects the paper garbage at house 1
|
||||
3. Travels from house 1 to house 2
|
||||
4. Collects the paper garbage at house 2
|
||||
Altogether, it takes 8 minutes to pick up all the paper garbage.
|
||||
The glass garbage truck:
|
||||
1. Collects the glass garbage at house 0
|
||||
2. Travels from house 0 to house 1
|
||||
3. Travels from house 1 to house 2
|
||||
4. Collects the glass garbage at house 2
|
||||
5. Travels from house 2 to house 3
|
||||
6. Collects the glass garbage at house 3
|
||||
Altogether, it takes 13 minutes to pick up all the glass garbage.
|
||||
Since there is no metal garbage, we do not need to consider the metal garbage truck.
|
||||
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> garbage = ["MMM","PGM","GP"], travel = [3,10]
|
||||
<strong>Output:</strong> 37
|
||||
<strong>Explanation:</strong>
|
||||
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
|
||||
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
|
||||
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
|
||||
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= garbage.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>garbage[i]</code> consists of only the letters <code>'M'</code>, <code>'P'</code>, and <code>'G'</code>.</li>
|
||||
<li><code>1 <= garbage[i].length <= 10</code></li>
|
||||
<li><code>travel.length == garbage.length - 1</code></li>
|
||||
<li><code>1 <= travel[i] <= 100</code></li>
|
||||
</ul>
|
46
leetcode/problem/removing-stars-from-a-string.html
Normal file
46
leetcode/problem/removing-stars-from-a-string.html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p>
|
||||
|
||||
<p>In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a star in <code>s</code>.</li>
|
||||
<li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the string after <strong>all</strong> stars have been removed</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The input will be generated such that the operation is always possible.</li>
|
||||
<li>It can be shown that the resulting string will always be unique.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leet**cod*e"
|
||||
<strong>Output:</strong> "lecoe"
|
||||
<strong>Explanation:</strong> Performing the removals from left to right:
|
||||
- The closest character to the 1<sup>st</sup> star is 't' in "lee<strong><u>t</u></strong>**cod*e". s becomes "lee*cod*e".
|
||||
- The closest character to the 2<sup>nd</sup> star is 'e' in "le<strong><u>e</u></strong>*cod*e". s becomes "lecod*e".
|
||||
- The closest character to the 3<sup>rd</sup> star is 'd' in "leco<strong><u>d</u></strong>*e". s becomes "lecoe".
|
||||
There are no more stars, so we return "lecoe".</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "erase*****"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> The entire string is removed, so we return an empty string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and stars <code>*</code>.</li>
|
||||
<li>The operation above can be performed on <code>s</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user