mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 16:01:41 +08:00
update
This commit is contained in:
@@ -1,35 +1,35 @@
|
||||
<p>On an alphabet board, we start at position <code>(0, 0)</code>, corresponding to character <code>board[0][0]</code>.</p>
|
||||
|
||||
<p>Here, <code>board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]</code>, as shown in the diagram below.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/07/28/azboard.png" style="width: 250px; height: 317px;" /></p>
|
||||
|
||||
<p>We may make the following moves:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'U'</code> moves our position up one row, if the position exists on the board;</li>
|
||||
<li><code>'D'</code> moves our position down one row, if the position exists on the board;</li>
|
||||
<li><code>'L'</code> moves our position left one column, if the position exists on the board;</li>
|
||||
<li><code>'R'</code> moves our position right one column, if the position exists on the board;</li>
|
||||
<li><code>'!'</code> adds the character <code>board[r][c]</code> at our current position <code>(r, c)</code> to the answer.</li>
|
||||
</ul>
|
||||
|
||||
<p>(Here, the only positions that exist on the board are positions with letters on them.)</p>
|
||||
|
||||
<p>Return a sequence of moves that makes our answer equal to <code>target</code> in the minimum number of moves. You may return any path that does so.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> target = "leet"
|
||||
<strong>Output:</strong> "DDR!UURRR!!DDD!"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> target = "code"
|
||||
<strong>Output:</strong> "RR!DDRR!UUL!R!"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target.length <= 100</code></li>
|
||||
<li><code>target</code> consists only of English lowercase letters.</li>
|
||||
<p>On an alphabet board, we start at position <code>(0, 0)</code>, corresponding to character <code>board[0][0]</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Here, <code>board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]</code>, as shown in the diagram below.</p>
|
||||
|
||||
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/07/28/azboard.png" style="width: 250px; height: 317px;" /></p>
|
||||
|
||||
|
||||
|
||||
<p>We may make the following moves:</p>
|
||||
|
||||
|
||||
|
||||
<ul>
|
||||
|
||||
<li><code>'U'</code> moves our position up one row, if the position exists on the board;</li>
|
||||
|
||||
<li><code>'D'</code> moves our position down one row, if the position exists on the board;</li>
|
||||
|
||||
<li><code>'L'</code> moves our position left one column, if the position exists on the board;</li>
|
||||
|
||||
<li><code>'R'</code> moves our position right one column, if the position exists on the board;</li>
|
||||
|
||||
<li><code>'!'</code> adds the character <code>board[r][c]</code> at our current position <code>(r, c)</code> to the answer.</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p>(Here, the only positions that exist on the board are positions with letters on them.)</p>
|
||||
|
||||
|
@@ -1,32 +1,32 @@
|
||||
<p>Given a binary array <code>nums</code> and an integer <code>goal</code>, return <em>the number of non-empty <strong>subarrays</strong> with a sum</em> <code>goal</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,0,1,0,1], goal = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The 4 subarrays are bolded and underlined below:
|
||||
[<u><strong>1,0,1</strong></u>,0,1]
|
||||
[<u><strong>1,0,1,0</strong></u>,1]
|
||||
[1,<u><strong>0,1,0,1</strong></u>]
|
||||
[1,0,<u><strong>1,0,1</strong></u>]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0,0,0], goal = 0
|
||||
<strong>Output:</strong> 15
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>0 <= goal <= nums.length</code></li>
|
||||
<p>Given a binary array <code>nums</code> and an integer <code>goal</code>, return <em>the number of non-empty <strong>subarrays</strong> with a sum</em> <code>goal</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> nums = [1,0,1,0,1], goal = 2
|
||||
|
||||
<strong>Output:</strong> 4
|
||||
|
||||
<strong>Explanation:</strong> The 4 subarrays are bolded and underlined below:
|
||||
|
||||
[<u><strong>1,0,1</strong></u>,0,1]
|
||||
|
||||
[<u><strong>1,0,1,0</strong></u>,1]
|
||||
|
||||
[1,<u><strong>0,1,0,1</strong></u>]
|
||||
|
||||
[1,0,<u><strong>1,0,1</strong></u>]
|
||||
|
||||
</pre>
|
||||
|
@@ -1,52 +1,52 @@
|
||||
<p>A conveyor belt has packages that must be shipped from one port to another within <code>days</code> days.</p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> package on the conveyor belt has a weight of <code>weights[i]</code>. Each day, we load the ship with packages on the conveyor belt (in the order given by <code>weights</code>). We may not load more weight than the maximum weight capacity of the ship.</p>
|
||||
|
||||
<p>Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within <code>days</code> days.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1,2,3,4,5,6,7,8,9,10], days = 5
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
|
||||
1st day: 1, 2, 3, 4, 5
|
||||
2nd day: 6, 7
|
||||
3rd day: 8
|
||||
4th day: 9
|
||||
5th day: 10
|
||||
|
||||
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [3,2,2,4,1,4], days = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
|
||||
1st day: 3, 2
|
||||
2nd day: 2, 4
|
||||
3rd day: 1, 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1,2,3,1,1], days = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
1st day: 1
|
||||
2nd day: 2
|
||||
3rd day: 3
|
||||
4th day: 1, 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= days <= weights.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= weights[i] <= 500</code></li>
|
||||
<p>A conveyor belt has packages that must be shipped from one port to another within <code>days</code> days.</p>
|
||||
|
||||
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> package on the conveyor belt has a weight of <code>weights[i]</code>. Each day, we load the ship with packages on the conveyor belt (in the order given by <code>weights</code>). We may not load more weight than the maximum weight capacity of the ship.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within <code>days</code> days.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> weights = [1,2,3,4,5,6,7,8,9,10], days = 5
|
||||
|
||||
<strong>Output:</strong> 15
|
||||
|
||||
<strong>Explanation:</strong> A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
|
||||
|
||||
1st day: 1, 2, 3, 4, 5
|
||||
|
||||
2nd day: 6, 7
|
||||
|
||||
3rd day: 8
|
||||
|
||||
4th day: 9
|
||||
|
||||
5th day: 10
|
||||
|
||||
|
||||
|
||||
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> weights = [3,2,2,4,1,4], days = 3
|
||||
|
@@ -1,41 +1,41 @@
|
||||
<p>We stack glasses in a pyramid, where the <strong>first</strong> row has <code>1</code> glass, the <strong>second</strong> row has <code>2</code> glasses, and so on until the 100<sup>th</sup> row. Each glass holds one cup of champagne.</p>
|
||||
|
||||
<p>Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)</p>
|
||||
|
||||
<p>For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.</p>
|
||||
|
||||
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png" style="height: 241px; width: 350px;" /></p>
|
||||
|
||||
<p>Now after pouring some non-negative integer cups of champagne, return how full the <code>j<sup>th</sup></code> glass in the <code>i<sup>th</sup></code> row is (both <code>i</code> and <code>j</code> are 0-indexed.)</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> poured = 1, query_row = 1, query_glass = 1
|
||||
<strong>Output:</strong> 0.00000
|
||||
<strong>Explanation:</strong> We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> poured = 2, query_row = 1, query_glass = 1
|
||||
<strong>Output:</strong> 0.50000
|
||||
<strong>Explanation:</strong> We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> poured = 100000009, query_row = 33, query_glass = 17
|
||||
<strong>Output:</strong> 1.00000
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= poured <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= query_glass <= query_row < 100</code></li>
|
||||
<p>We stack glasses in a pyramid, where the <strong>first</strong> row has <code>1</code> glass, the <strong>second</strong> row has <code>2</code> glasses, and so on until the 100<sup>th</sup> row. Each glass holds one cup of champagne.</p>
|
||||
|
||||
|
||||
|
||||
<p>Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)</p>
|
||||
|
||||
|
||||
|
||||
<p>For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.</p>
|
||||
|
||||
|
||||
|
||||
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png" style="height: 241px; width: 350px;" /></p>
|
||||
|
||||
|
||||
|
||||
<p>Now after pouring some non-negative integer cups of champagne, return how full the <code>j<sup>th</sup></code> glass in the <code>i<sup>th</sup></code> row is (both <code>i</code> and <code>j</code> are 0-indexed.)</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> poured = 1, query_row = 1, query_glass = 1
|
||||
|
||||
<strong>Output:</strong> 0.00000
|
||||
|
||||
<strong>Explanation:</strong> We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
45
leetcode/problem/count-collisions-on-a-road.html
Normal file
45
leetcode/problem/count-collisions-on-a-road.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>There are <code>n</code> cars on an infinitely long road. The cars are numbered from <code>0</code> to <code>n - 1</code> from left to right and each car is present at a <strong>unique</strong> point.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>directions</code> of length <code>n</code>. <code>directions[i]</code> can be either <code>'L'</code>, <code>'R'</code>, or <code>'S'</code> denoting whether the <code>i<sup>th</sup></code> car is moving towards the <strong>left</strong>, towards the <strong>right</strong>, or <strong>staying</strong> at its current point respectively. Each moving car has the <strong>same speed</strong>.</p>
|
||||
|
||||
<p>The number of collisions can be calculated as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>When two cars moving in <strong>opposite</strong> directions collide with each other, the number of collisions increases by <code>2</code>.</li>
|
||||
<li>When a moving car collides with a stationary car, the number of collisions increases by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number of collisions</strong> that will happen on the road</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> directions = "RLRSLL"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
The collisions that will happen on the road are:
|
||||
- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
|
||||
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
|
||||
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
|
||||
- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
|
||||
Thus, the total number of collisions that will happen on the road is 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> directions = "LLRR"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= directions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>directions[i]</code> is either <code>'L'</code>, <code>'R'</code>, or <code>'S'</code>.</li>
|
||||
</ul>
|
44
leetcode/problem/count-hills-and-valleys-in-an-array.html
Normal file
44
leetcode/problem/count-hills-and-valleys-in-an-array.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. An index <code>i</code> is part of a <strong>hill</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are smaller than <code>nums[i]</code>. Similarly, an index <code>i</code> is part of a <strong>valley</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are larger than <code>nums[i]</code>. Adjacent indices <code>i</code> and <code>j</code> are part of the <strong>same</strong> hill or valley if <code>nums[i] == nums[j]</code>.</p>
|
||||
|
||||
<p>Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on <strong>both</strong> the left and right of the index.</p>
|
||||
|
||||
<p>Return <i>the number of hills and valleys in </i><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,1,1,6,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
|
||||
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill.
|
||||
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
|
||||
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
|
||||
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
|
||||
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley.
|
||||
There are 3 hills and valleys so we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,6,5,5,4,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
|
||||
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
|
||||
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
|
||||
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
|
||||
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
|
||||
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
|
||||
There are 0 hills and valleys so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -1,41 +1,41 @@
|
||||
<p>You are an ant tasked with adding <code>n</code> new rooms numbered <code>0</code> to <code>n-1</code> to your colony. You are given the expansion plan as a <strong>0-indexed</strong> integer array of length <code>n</code>, <code>prevRoom</code>, where <code>prevRoom[i]</code> indicates that you must build room <code>prevRoom[i]</code> before building room <code>i</code>, and these two rooms must be connected <strong>directly</strong>. Room <code>0</code> is already built, so <code>prevRoom[0] = -1</code>. The expansion plan is given such that once all the rooms are built, every room will be reachable from room <code>0</code>.</p>
|
||||
|
||||
<p>You can only build <strong>one room</strong> at a time, and you can travel freely between rooms you have <strong>already built</strong> only if they are <strong>connected</strong>. You can choose to build <strong>any room</strong> as long as its <strong>previous room</strong> is already built.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of different orders</strong> you can build all the rooms in</em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/d1.JPG" style="width: 200px; height: 212px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> prevRoom = [-1,0,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is only one way to build the additional rooms: 0 → 1 → 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/d2.JPG" style="width: 200px; height: 239px;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prevRoom = [-1,0,0,1,2]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:
|
||||
</strong>The 6 ways are:
|
||||
0 → 1 → 3 → 2 → 4
|
||||
0 → 2 → 4 → 1 → 3
|
||||
0 → 1 → 2 → 3 → 4
|
||||
0 → 1 → 2 → 4 → 3
|
||||
0 → 2 → 1 → 3 → 4
|
||||
0 → 2 → 1 → 4 → 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == prevRoom.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>prevRoom[0] == -1</code></li>
|
||||
<li><code>0 <= prevRoom[i] < n</code> for all <code>1 <= i < n</code></li>
|
||||
<li>Every room is reachable from room <code>0</code> once all the rooms are built.</li>
|
||||
<p>You are an ant tasked with adding <code>n</code> new rooms numbered <code>0</code> to <code>n-1</code> to your colony. You are given the expansion plan as a <strong>0-indexed</strong> integer array of length <code>n</code>, <code>prevRoom</code>, where <code>prevRoom[i]</code> indicates that you must build room <code>prevRoom[i]</code> before building room <code>i</code>, and these two rooms must be connected <strong>directly</strong>. Room <code>0</code> is already built, so <code>prevRoom[0] = -1</code>. The expansion plan is given such that once all the rooms are built, every room will be reachable from room <code>0</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>You can only build <strong>one room</strong> at a time, and you can travel freely between rooms you have <strong>already built</strong> only if they are <strong>connected</strong>. You can choose to build <strong>any room</strong> as long as its <strong>previous room</strong> is already built.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return <em>the <strong>number of different orders</strong> you can build all the rooms in</em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/d1.JPG" style="width: 200px; height: 212px;" />
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> prevRoom = [-1,0,1]
|
||||
|
||||
<strong>Output:</strong> 1
|
||||
|
||||
<strong>Explanation:</strong> There is only one way to build the additional rooms: 0 → 1 → 2
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/d2.JPG" style="width: 200px; height: 239px;" /></strong>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> prevRoom = [-1,0,0,1,2]
|
||||
|
||||
<strong>Output:</strong> 6
|
@@ -1,18 +1,18 @@
|
||||
<p>Given a valid (IPv4) IP <code>address</code>, return a defanged version of that IP address.</p>
|
||||
|
||||
<p>A <em>defanged IP address</em> replaces every period <code>"."</code> with <code>"[.]"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> address = "1.1.1.1"
|
||||
<strong>Output:</strong> "1[.]1[.]1[.]1"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> address = "255.100.50.0"
|
||||
<strong>Output:</strong> "255[.]100[.]50[.]0"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The given <code>address</code> is a valid IPv4 address.</li>
|
||||
<p>Given a valid (IPv4) IP <code>address</code>, return a defanged version of that IP address.</p>
|
||||
|
||||
|
||||
|
||||
<p>A <em>defanged IP address</em> replaces every period <code>"."</code> with <code>"[.]"</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre><strong>Input:</strong> address = "1.1.1.1"
|
||||
|
||||
<strong>Output:</strong> "1[.]1[.]1[.]1"
|
||||
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
|
@@ -1,49 +1,49 @@
|
||||
<p>A bus has <code>n</code> stops numbered from <code>0</code> to <code>n - 1</code> that form a circle. We know the distance between all pairs of neighboring stops where <code>distance[i]</code> is the distance between the stops number <code>i</code> and <code>(i + 1) % n</code>.</p>
|
||||
|
||||
<p>The bus goes along both directions i.e. clockwise and counterclockwise.</p>
|
||||
|
||||
<p>Return the shortest distance between the given <code>start</code> and <code>destination</code> stops.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1.jpg" style="width: 388px; height: 240px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Distance between 0 and 1 is 1 or 9, minimum is 1.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1-1.jpg" style="width: 388px; height: 240px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Distance between 0 and 2 is 3 or 7, minimum is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1-2.jpg" style="width: 388px; height: 240px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Distance between 0 and 3 is 6 or 4, minimum is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10^4</code></li>
|
||||
<li><code>distance.length == n</code></li>
|
||||
<li><code>0 <= start, destination < n</code></li>
|
||||
<li><code>0 <= distance[i] <= 10^4</code></li>
|
||||
<p>A bus has <code>n</code> stops numbered from <code>0</code> to <code>n - 1</code> that form a circle. We know the distance between all pairs of neighboring stops where <code>distance[i]</code> is the distance between the stops number <code>i</code> and <code>(i + 1) % n</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>The bus goes along both directions i.e. clockwise and counterclockwise.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return the shortest distance between the given <code>start</code> and <code>destination</code> stops.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1.jpg" style="width: 388px; height: 240px;" /></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 1
|
||||
|
||||
<strong>Output:</strong> 1
|
||||
|
||||
<strong>Explanation:</strong> Distance between 0 and 1 is 1 or 9, minimum is 1.</pre>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1-1.jpg" style="width: 388px; height: 240px;" /></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 2
|
||||
|
||||
<strong>Output:</strong> 3
|
39
leetcode/problem/find-all-k-distant-indices-in-an-array.html
Normal file
39
leetcode/problem/find-all-k-distant-indices-in-an-array.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>key</code> and <code>k</code>. A <strong>k-distant index</strong> is an index <code>i</code> of <code>nums</code> for which there exists at least one index <code>j</code> such that <code>|i - j| <= k</code> and <code>nums[j] == key</code>.</p>
|
||||
|
||||
<p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,9,1,3,9,5], key = 9, k = 1
|
||||
<strong>Output:</strong> [1,2,3,4,5,6]
|
||||
<strong>Explanation:</strong> Here, <code>nums[2] == key</code> and <code>nums[5] == key.
|
||||
- For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j</code> where <code>|0 - j| <= k</code> and <code>nums[j] == key. Thus, 0 is not a k-distant index.
|
||||
- For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
|
||||
- For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
|
||||
- For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
|
||||
- For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
|
||||
- For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
|
||||
- For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
|
||||
</code>Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2,2,2], key = 2, k = 2
|
||||
<strong>Output:</strong> [0,1,2,3,4]
|
||||
<strong>Explanation:</strong> For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
|
||||
Hence, we return [0,1,2,3,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>key</code> is an integer from the array <code>nums</code>.</li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -10,7 +10,7 @@
|
||||
<strong>Output:</strong> [101,111,121,131,141,999]
|
||||
<strong>Explanation:</strong>
|
||||
The first few palindromes of length 3 are:
|
||||
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 201, ...
|
||||
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
|
||||
The 90<sup>th</sup> palindrome of length 3 is 999.
|
||||
</pre>
|
||||
|
||||
|
@@ -1,24 +1,24 @@
|
||||
<p>Given the <code>root</code> of a binary search tree, rearrange the tree in <strong>in-order</strong> so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg" style="width: 600px; height: 350px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
|
||||
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg" style="width: 300px; height: 114px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,1,7]
|
||||
<strong>Output:</strong> [1,null,5,null,7]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the given tree will be in the range <code>[1, 100]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
<p>Given the <code>root</code> of a binary search tree, rearrange the tree in <strong>in-order</strong> so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg" style="width: 600px; height: 350px;" />
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
|
||||
|
||||
<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg" style="width: 300px; height: 114px;" />
|
||||
|
@@ -1,25 +1,25 @@
|
||||
<p>Given a 2D <code>grid</code> of <code>0</code>s and <code>1</code>s, return the number of elements in the largest <strong>square</strong> subgrid that has all <code>1</code>s on its <strong>border</strong>, or <code>0</code> if such a subgrid doesn't exist in the <code>grid</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grid.length <= 100</code></li>
|
||||
<li><code>1 <= grid[0].length <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code></li>
|
||||
<p>Given a 2D <code>grid</code> of <code>0</code>s and <code>1</code>s, return the number of elements in the largest <strong>square</strong> subgrid that has all <code>1</code>s on its <strong>border</strong>, or <code>0</code> if such a subgrid doesn't exist in the <code>grid</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
|
||||
<strong>Output:</strong> 9
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
@@ -1,36 +1,36 @@
|
||||
<p>We define a harmonious array as an array where the difference between its maximum value and its minimum value is <b>exactly</b> <code>1</code>.</p>
|
||||
|
||||
<p>Given an integer array <code>nums</code>, return <em>the length of its longest harmonious subsequence among all its possible subsequences</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of array is a sequence that can be derived from the 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 = [1,3,2,2,5,2,3,7]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The longest harmonious subsequence is [3,2,2,2,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<p>We define a harmonious array as an array where the difference between its maximum value and its minimum value is <b>exactly</b> <code>1</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Given an integer array <code>nums</code>, return <em>the length of its longest harmonious subsequence among all its possible subsequences</em>.</p>
|
||||
|
||||
|
||||
|
||||
<p>A <strong>subsequence</strong> of array is a sequence that can be derived from the 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 = [1,3,2,2,5,2,3,7]
|
||||
|
||||
<strong>Output:</strong> 5
|
||||
|
||||
<strong>Explanation:</strong> The longest harmonious subsequence is [3,2,2,2,3].
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>. You are also given a <strong>0-indexed</strong> string <code>queryCharacters</code> of length <code>k</code> and a <strong>0-indexed</strong> array of integer <strong>indices</strong> <code>queryIndices</code> of length <code>k</code>, both of which are used to describe <code>k</code> queries.</p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> query updates the character in <code>s</code> at index <code>queryIndices[i]</code> to the character <code>queryCharacters[i]</code>.</p>
|
||||
|
||||
<p>Return <em>an array</em> <code>lengths</code> <em>of length </em><code>k</code><em> where</em> <code>lengths[i]</code> <em>is the <strong>length</strong> of the <strong>longest substring</strong> of </em><code>s</code><em> consisting of <strong>only one repeating</strong> character <strong>after</strong> the</em> <code>i<sup>th</sup></code> <em>query</em><em> is performed.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]
|
||||
<strong>Output:</strong> [3,3,4]
|
||||
<strong>Explanation:</strong>
|
||||
- 1<sup>st</sup> query updates s = "<u>b<strong>b</strong>b</u>acc". The longest substring consisting of one repeating character is "bbb" with length 3.
|
||||
- 2<sup>nd</sup> query updates s = "bbb<u><strong>c</strong>cc</u>".
|
||||
The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3.
|
||||
- 3<sup>rd</sup> query updates s = "<u>bbb<strong>b</strong></u>cc". The longest substring consisting of one repeating character is "bbbb" with length 4.
|
||||
Thus, we return [3,3,4].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abyzz", queryCharacters = "aa", queryIndices = [2,1]
|
||||
<strong>Output:</strong> [2,3]
|
||||
<strong>Explanation:</strong>
|
||||
- 1<sup>st</sup> query updates s = "ab<strong>a</strong><u>zz</u>". The longest substring consisting of one repeating character is "zz" with length 2.
|
||||
- 2<sup>nd</sup> query updates s = "<u>a<strong>a</strong>a</u>zz". The longest substring consisting of one repeating character is "aaa" with length 3.
|
||||
Thus, we return [2,3].
|
||||
</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.</li>
|
||||
<li><code>k == queryCharacters.length == queryIndices.length</code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>queryCharacters</code> consists of lowercase English letters.</li>
|
||||
<li><code>0 <= queryIndices[i] < s.length</code></li>
|
||||
</ul>
|
@@ -1,39 +1,39 @@
|
||||
<p>Given two integer arrays <code>arr1</code> and <code>arr2</code>, return the minimum number of operations (possibly zero) needed to make <code>arr1</code> strictly increasing.</p>
|
||||
|
||||
<p>In one operation, you can choose two indices <code>0 <= i < arr1.length</code> and <code>0 <= j < arr2.length</code> and do the assignment <code>arr1[i] = arr2[j]</code>.</p>
|
||||
|
||||
<p>If there is no way to make <code>arr1</code> strictly increasing, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Replace <code>5</code> with <code>2</code>, then <code>arr1 = [1, 2, 3, 6, 7]</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [4,3,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Replace <code>5</code> with <code>3</code> and then replace <code>3</code> with <code>4</code>. <code>arr1 = [1, 3, 4, 6, 7]</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> You can't make <code>arr1</code> strictly increasing.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 2000</code></li>
|
||||
<li><code>0 <= arr1[i], arr2[i] <= 10^9</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Given two integer arrays <code>arr1</code> and <code>arr2</code>, return the minimum number of operations (possibly zero) needed to make <code>arr1</code> strictly increasing.</p>
|
||||
|
||||
|
||||
|
||||
<p>In one operation, you can choose two indices <code>0 <= i < arr1.length</code> and <code>0 <= j < arr2.length</code> and do the assignment <code>arr1[i] = arr2[j]</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>If there is no way to make <code>arr1</code> strictly increasing, return <code>-1</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
|
||||
|
||||
<strong>Output:</strong> 1
|
||||
|
||||
<strong>Explanation:</strong> Replace <code>5</code> with <code>2</code>, then <code>arr1 = [1, 2, 3, 6, 7]</code>.
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [4,3,1]
|
||||
|
||||
<strong>Output:</strong> 2
|
@@ -1,39 +1,39 @@
|
||||
<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>
|
||||
|
||||
<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0],[0,1]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1],[1,0]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1],[1,1]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Can't change any 0 to 1, only one island with area = 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>
|
||||
|
||||
|
||||
|
||||
<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> grid = [[1,0],[0,1]]
|
||||
|
||||
<strong>Output:</strong> 3
|
||||
|
||||
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> grid = [[1,1],[1,0]]
|
||||
|
||||
<strong>Output:</strong> 4
|
@@ -1,47 +1,47 @@
|
||||
<p>A string is a <em>valid parentheses string</em> (denoted VPS) if and only if it consists of <code>"("</code> and <code>")"</code> characters only, and:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is the empty string, or</li>
|
||||
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are VPS's, or</li>
|
||||
<li>It can be written as <code>(A)</code>, where <code>A</code> is a VPS.</li>
|
||||
</ul>
|
||||
|
||||
<p>We can similarly define the <em>nesting depth</em> <code>depth(S)</code> of any VPS <code>S</code> as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>depth("") = 0</code></li>
|
||||
<li><code>depth(A + B) = max(depth(A), depth(B))</code>, where <code>A</code> and <code>B</code> are VPS's</li>
|
||||
<li><code>depth("(" + A + ")") = 1 + depth(A)</code>, where <code>A</code> is a VPS.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>""</code>, <code>"()()"</code>, and <code>"()(()())"</code> are VPS's (with nesting depths 0, 1, and 2), and <code>")("</code> and <code>"(()"</code> are not VPS's.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Given a VPS <font face="monospace">seq</font>, split it into two disjoint subsequences <code>A</code> and <code>B</code>, such that <code>A</code> and <code>B</code> are VPS's (and <code>A.length + B.length = seq.length</code>).</p>
|
||||
|
||||
<p>Now choose <strong>any</strong> such <code>A</code> and <code>B</code> such that <code>max(depth(A), depth(B))</code> is the minimum possible value.</p>
|
||||
|
||||
<p>Return an <code>answer</code> array (of length <code>seq.length</code>) that encodes such a choice of <code>A</code> and <code>B</code>: <code>answer[i] = 0</code> if <code>seq[i]</code> is part of <code>A</code>, else <code>answer[i] = 1</code>. Note that even though multiple answers may exist, you may return any of them.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> seq = "(()())"
|
||||
<strong>Output:</strong> [0,1,1,1,1,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> seq = "()(())()"
|
||||
<strong>Output:</strong> [0,0,0,1,1,0,1,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= seq.size <= 10000</code></li>
|
||||
</ul>
|
||||
<p>A string is a <em>valid parentheses string</em> (denoted VPS) if and only if it consists of <code>"("</code> and <code>")"</code> characters only, and:</p>
|
||||
|
||||
|
||||
|
||||
<ul>
|
||||
|
||||
<li>It is the empty string, or</li>
|
||||
|
||||
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are VPS's, or</li>
|
||||
|
||||
<li>It can be written as <code>(A)</code>, where <code>A</code> is a VPS.</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p>We can similarly define the <em>nesting depth</em> <code>depth(S)</code> of any VPS <code>S</code> as follows:</p>
|
||||
|
||||
|
||||
|
||||
<ul>
|
||||
|
||||
<li><code>depth("") = 0</code></li>
|
||||
|
||||
<li><code>depth(A + B) = max(depth(A), depth(B))</code>, where <code>A</code> and <code>B</code> are VPS's</li>
|
||||
|
||||
<li><code>depth("(" + A + ")") = 1 + depth(A)</code>, where <code>A</code> is a VPS.</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p>For example, <code>""</code>, <code>"()()"</code>, and <code>"()(()())"</code> are VPS's (with nesting depths 0, 1, and 2), and <code>")("</code> and <code>"(()"</code> are not VPS's.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
|
||||
|
||||
<p>Given a VPS <font face="monospace">seq</font>, split it into two disjoint subsequences <code>A</code> and <code>B</code>, such that <code>A</code> and <code>B</code> are VPS's (and <code>A.length + B.length = seq.length</code>).</p>
|
||||
|
||||
|
||||
|
||||
<p>Now choose <strong>any</strong> such <code>A</code> and <code>B</code> such that <code>max(depth(A), depth(B))</code> is the minimum possible value.</p>
|
||||
|
||||
|
||||
|
@@ -1,28 +1,28 @@
|
||||
<p>Given two arrays of integers with equal lengths, return the maximum value of:</p>
|
||||
|
||||
<p><code>|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|</code></p>
|
||||
|
||||
<p>where the maximum is taken over all <code>0 <= i, j < arr1.length</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
|
||||
<strong>Output:</strong> 13
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
|
||||
<strong>Output:</strong> 20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= arr1.length == arr2.length <= 40000</code></li>
|
||||
<li><code>-10^6 <= arr1[i], arr2[i] <= 10^6</code></li>
|
||||
</ul>
|
||||
<p>Given two arrays of integers with equal lengths, return the maximum value of:</p>
|
||||
|
||||
|
||||
|
||||
<p><code>|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|</code></p>
|
||||
|
||||
|
||||
|
||||
<p>where the maximum is taken over all <code>0 <= i, j < arr1.length</code>.</p>
|
||||
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
|
||||
<strong>Output:</strong> 13
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
|
||||
<strong>Output:</strong> 20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -0,0 +1,55 @@
|
||||
<p>Alice and Bob are opponents in an archery competition. The competition has set the following rules:</p>
|
||||
|
||||
<ol>
|
||||
<li>Alice first shoots <code>numArrows</code> arrows and then Bob shoots <code>numArrows</code> arrows.</li>
|
||||
<li>The points are then calculated as follows:
|
||||
<ol>
|
||||
<li>The target has integer scoring sections ranging from <code>0</code> to <code>11</code> <strong>inclusive</strong>.</li>
|
||||
<li>For <strong>each</strong> section of the target with score <code>k</code> (in between <code>0</code> to <code>11</code>), say Alice and Bob have shot <code>a<sub>k</sub></code> and <code>b<sub>k</sub></code> arrows on that section respectively. If <code>a<sub>k</sub> >= b<sub>k</sub></code>, then Alice takes <code>k</code> points. If <code>a<sub>k</sub> < b<sub>k</sub></code>, then Bob takes <code>k</code> points.</li>
|
||||
<li>However, if <code>a<sub>k</sub> == b<sub>k</sub> == 0</code>, then <strong>nobody</strong> takes <code>k</code> points.</li>
|
||||
</ol>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p>For example, if Alice and Bob both shot <code>2</code> arrows on the section with score <code>11</code>, then Alice takes <code>11</code> points. On the other hand, if Alice shot <code>0</code> arrows on the section with score <code>11</code> and Bob shot <code>2</code> arrows on that same section, then Bob takes <code>11</code> points.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given the integer <code>numArrows</code> and an integer array <code>aliceArrows</code> of size <code>12</code>, which represents the number of arrows Alice shot on each scoring section from <code>0</code> to <code>11</code>. Now, Bob wants to <strong>maximize</strong> the total number of points he can obtain.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>bobArrows</code><em> which represents the number of arrows Bob shot on <strong>each</strong> scoring section from </em><code>0</code><em> to </em><code>11</code>. The sum of the values in <code>bobArrows</code> should equal <code>numArrows</code>.</p>
|
||||
|
||||
<p>If there are multiple ways for Bob to earn the maximum total points, return <strong>any</strong> one of them.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/24/ex1.jpg" style="width: 600px; height: 120px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0]
|
||||
<strong>Output:</strong> [0,0,0,0,1,1,0,0,1,2,3,1]
|
||||
<strong>Explanation:</strong> The table above shows how the competition is scored.
|
||||
Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47.
|
||||
It can be shown that Bob cannot obtain a score higher than 47 points.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/24/ex2new.jpg" style="width: 600px; height: 117px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2]
|
||||
<strong>Output:</strong> [0,0,0,0,0,0,0,0,1,1,1,0]
|
||||
<strong>Explanation:</strong> The table above shows how the competition is scored.
|
||||
Bob earns a total point of 8 + 9 + 10 = 27.
|
||||
It can be shown that Bob cannot obtain a score higher than 27 points.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= numArrows <= 10<sup>5</sup></code></li>
|
||||
<li><code>aliceArrows.length == bobArrows.length == 12</code></li>
|
||||
<li><code>0 <= aliceArrows[i], bobArrows[i] <= numArrows</code></li>
|
||||
<li><code>sum(aliceArrows[i]) == numArrows</code></li>
|
||||
</ul>
|
44
leetcode/problem/minimum-operations-to-halve-array-sum.html
Normal file
44
leetcode/problem/minimum-operations-to-halve-array-sum.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>You are given an array <code>nums</code> of positive integers. In one operation, you can choose <strong>any</strong> number from <code>nums</code> and reduce it to <strong>exactly</strong> half the number. (Note that you may choose this reduced number in future operations.)</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of operations to reduce the sum of </em><code>nums</code><em> by <strong>at least</strong> half.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,19,8,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.
|
||||
The following is one of the ways to reduce the sum by at least half:
|
||||
Pick the number 19 and reduce it to 9.5.
|
||||
Pick the number 9.5 and reduce it to 4.75.
|
||||
Pick the number 8 and reduce it to 4.
|
||||
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.
|
||||
The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 >= 33/2 = 16.5.
|
||||
Overall, 3 operations were used so we return 3.
|
||||
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,8,20]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The initial sum of nums is equal to 3 + 8 + 20 = 31.
|
||||
The following is one of the ways to reduce the sum by at least half:
|
||||
Pick the number 20 and reduce it to 10.
|
||||
Pick the number 10 and reduce it to 5.
|
||||
Pick the number 3 and reduce it to 1.5.
|
||||
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.
|
||||
The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 >= 31/2 = 16.5.
|
||||
Overall, 3 operations were used so we return 3.
|
||||
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given an integer <code>n</code> denoting the number of nodes of a <strong>weighted directed</strong> graph. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> denotes that there exists a <strong>directed</strong> edge from <code>from<sub>i</sub></code> to <code>to<sub>i</sub></code> with weight <code>weight<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Lastly, you are given three <strong>distinct</strong> integers <code>src1</code>, <code>src2</code>, and <code>dest</code> denoting three distinct nodes of the graph.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum weight</strong> of a subgraph of the graph such that it is <strong>possible</strong> to reach</em> <code>dest</code> <em>from both</em> <code>src1</code> <em>and</em> <code>src2</code> <em>via a set of edges of this subgraph</em>. In case such a subgraph does not exist, return <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subgraph</strong> is a graph whose vertices and edges are subsets of the original graph. The <strong>weight</strong> of a subgraph is the sum of weights of its constituent edges.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example1drawio.png" style="width: 263px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
The above figure represents the input graph.
|
||||
The blue edges represent one of the subgraphs that yield the optimal answer.
|
||||
Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/17/example2-1drawio.png" style="width: 350px; height: 51px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong>
|
||||
The above figure represents the input graph.
|
||||
It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub>, src1, src2, dest <= n - 1</code></li>
|
||||
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
|
||||
<li><code>src1</code>, <code>src2</code>, and <code>dest</code> are pairwise distinct.</li>
|
||||
<li><code>1 <= weight[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed binary</strong> string <code>floor</code>, which represents the colors of tiles on a floor:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>floor[i] = '0'</code> denotes that the <code>i<sup>th</sup></code> tile of the floor is colored <strong>black</strong>.</li>
|
||||
<li>On the other hand, <code>floor[i] = '1'</code> denotes that the <code>i<sup>th</sup></code> tile of the floor is colored <strong>white</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given <code>numCarpets</code> and <code>carpetLen</code>. You have <code>numCarpets</code> <strong>black</strong> carpets, each of length <code>carpetLen</code> tiles. Cover the tiles with the given carpets such that the number of <strong>white</strong> tiles still visible is <strong>minimum</strong>. Carpets may overlap one another.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of white tiles still visible.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/10/ex1-1.png" style="width: 400px; height: 73px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> floor = "10110101", numCarpets = 2, carpetLen = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.
|
||||
No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/10/ex2.png" style="width: 353px; height: 123px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> floor = "11111", numCarpets = 2, carpetLen = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
|
||||
Note that the carpets are able to overlap one another.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= carpetLen <= floor.length <= 1000</code></li>
|
||||
<li><code>floor[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
<li><code>1 <= numCarpets <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.<strong> </strong>You are also given an integer <code>key</code>, which is present in <code>nums</code>.</p>
|
||||
|
||||
<p>For every unique integer <code>target</code> in <code>nums</code>, <strong>count</strong> the number of times <code>target</code> immediately follows an occurrence of <code>key</code> in <code>nums</code>. In other words, count the number of indices <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= nums.length - 2</code>,</li>
|
||||
<li><code>nums[i] == key</code> and,</li>
|
||||
<li><code>nums[i + 1] == target</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the </em><code>target</code><em> with the <strong>maximum</strong> count</em>. The test cases will be generated such that the <code>target</code> with maximum count is unique.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100,200,1,100], key = 1
|
||||
<strong>Output:</strong> 100
|
||||
<strong>Explanation:</strong> For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
|
||||
No other integers follow an occurrence of key, so we return 100.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2,2,3], key = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
|
||||
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
|
||||
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li>The test cases will be generated such that the answer is unique.</li>
|
||||
</ul>
|
@@ -1,31 +1,31 @@
|
||||
<p>The Tribonacci sequence T<sub>n</sub> is defined as follows: </p>
|
||||
|
||||
<p>T<sub>0</sub> = 0, T<sub>1</sub> = 1, T<sub>2</sub> = 1, and T<sub>n+3</sub> = T<sub>n</sub> + T<sub>n+1</sub> + T<sub>n+2</sub> for n >= 0.</p>
|
||||
|
||||
<p>Given <code>n</code>, return the value of T<sub>n</sub>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
T_3 = 0 + 1 + 1 = 2
|
||||
T_4 = 1 + 1 + 2 = 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 25
|
||||
<strong>Output:</strong> 1389537
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 37</code></li>
|
||||
<li>The answer is guaranteed to fit within a 32-bit integer, ie. <code>answer <= 2^31 - 1</code>.</li>
|
||||
<p>The Tribonacci sequence T<sub>n</sub> is defined as follows: </p>
|
||||
|
||||
|
||||
|
||||
<p>T<sub>0</sub> = 0, T<sub>1</sub> = 1, T<sub>2</sub> = 1, and T<sub>n+3</sub> = T<sub>n</sub> + T<sub>n+1</sub> + T<sub>n+2</sub> for n >= 0.</p>
|
||||
|
||||
|
||||
|
||||
<p>Given <code>n</code>, return the value of T<sub>n</sub>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> n = 4
|
||||
|
||||
<strong>Output:</strong> 4
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
|
||||
T_3 = 0 + 1 + 1 = 2
|
||||
|
||||
T_4 = 1 + 1 + 2 = 4
|
||||
|
||||
</pre>
|
@@ -1,25 +1,25 @@
|
||||
<p>You are given a square <code>board</code> of characters. You can move on the board starting at the bottom right square marked with the character <code>'S'</code>.</p>
|
||||
|
||||
<p>You need to reach the top left square marked with the character <code>'E'</code>. The rest of the squares are labeled either with a numeric character <code>1, 2, ..., 9</code> or with an obstacle <code>'X'</code>. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.</p>
|
||||
|
||||
<p>Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, <strong>taken modulo <code>10^9 + 7</code></strong>.</p>
|
||||
|
||||
<p>In case there is no path, return <code>[0, 0]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> board = ["E23","2X2","12S"]
|
||||
<strong>Output:</strong> [7,1]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> board = ["E12","1X1","21S"]
|
||||
<strong>Output:</strong> [4,2]
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> board = ["E11","XXX","11S"]
|
||||
<strong>Output:</strong> [0,0]
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= board.length == board[i].length <= 100</code></li>
|
||||
<p>You are given a square <code>board</code> of characters. You can move on the board starting at the bottom right square marked with the character <code>'S'</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>You need to reach the top left square marked with the character <code>'E'</code>. The rest of the squares are labeled either with a numeric character <code>1, 2, ..., 9</code> or with an obstacle <code>'X'</code>. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, <strong>taken modulo <code>10^9 + 7</code></strong>.</p>
|
||||
|
||||
|
||||
|
||||
<p>In case there is no path, return <code>[0, 0]</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre><strong>Input:</strong> board = ["E23","2X2","12S"]
|
||||
|
||||
<strong>Output:</strong> [7,1]
|
||||
|
||||
</pre><p><strong>Example 2:</strong></p>
|
@@ -1,49 +1,49 @@
|
||||
<p>You are given an undirected weighted graph of <code>n</code> nodes (0-indexed), represented by an edge list where <code>edges[i] = [a, b]</code> is an undirected edge connecting the nodes <code>a</code> and <code>b</code> with a probability of success of traversing that edge <code>succProb[i]</code>.</p>
|
||||
|
||||
<p>Given two nodes <code>start</code> and <code>end</code>, find the path with the maximum probability of success to go from <code>start</code> to <code>end</code> and return its success probability.</p>
|
||||
|
||||
<p>If there is no path from <code>start</code> to <code>end</code>, <strong>return 0</strong>. Your answer will be accepted if it differs from the correct answer by at most <strong>1e-5</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex1.png" style="width: 187px; height: 186px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
|
||||
<strong>Output:</strong> 0.25000
|
||||
<strong>Explanation:</strong> There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex2.png" style="width: 189px; height: 186px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
|
||||
<strong>Output:</strong> 0.30000
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex3.png" style="width: 215px; height: 191px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
|
||||
<strong>Output:</strong> 0.00000
|
||||
<strong>Explanation:</strong> There is no path between 0 and 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10^4</code></li>
|
||||
<li><code>0 <= start, end < n</code></li>
|
||||
<li><code>start != end</code></li>
|
||||
<li><code>0 <= a, b < n</code></li>
|
||||
<li><code>a != b</code></li>
|
||||
<li><code>0 <= succProb.length == edges.length <= 2*10^4</code></li>
|
||||
<li><code>0 <= succProb[i] <= 1</code></li>
|
||||
<li>There is at most one edge between every two nodes.</li>
|
||||
<p>You are given an undirected weighted graph of <code>n</code> nodes (0-indexed), represented by an edge list where <code>edges[i] = [a, b]</code> is an undirected edge connecting the nodes <code>a</code> and <code>b</code> with a probability of success of traversing that edge <code>succProb[i]</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Given two nodes <code>start</code> and <code>end</code>, find the path with the maximum probability of success to go from <code>start</code> to <code>end</code> and return its success probability.</p>
|
||||
|
||||
|
||||
|
||||
<p>If there is no path from <code>start</code> to <code>end</code>, <strong>return 0</strong>. Your answer will be accepted if it differs from the correct answer by at most <strong>1e-5</strong>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex1.png" style="width: 187px; height: 186px;" /></strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
|
||||
|
||||
<strong>Output:</strong> 0.25000
|
||||
|
||||
<strong>Explanation:</strong> There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.
|
||||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/20/1558_ex2.png" style="width: 189px; height: 186px;" /></strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
|
||||
|
||||
<strong>Output:</strong> 0.30000
|
||||
|
||||
</pre>
|
@@ -1,36 +1,36 @@
|
||||
<p>Given the <code>head</code> of a linked list, we repeatedly delete consecutive sequences of nodes that sum to <code>0</code> until there are no such sequences.</p>
|
||||
|
||||
<p>After doing so, return the head of the final linked list. You may return any such answer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p>(Note that in the examples below, all sequences are serializations of <code>ListNode</code> objects.)</p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,-3,3,1]
|
||||
<strong>Output:</strong> [3,1]
|
||||
<strong>Note:</strong> The answer [1,2,1] would also be accepted.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,-3,4]
|
||||
<strong>Output:</strong> [1,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,-3,-2]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The given linked list will contain between <code>1</code> and <code>1000</code> nodes.</li>
|
||||
<li>Each node in the linked list has <code>-1000 <= node.val <= 1000</code>.</li>
|
||||
</ul>
|
||||
<p>Given the <code>head</code> of a linked list, we repeatedly delete consecutive sequences of nodes that sum to <code>0</code> until there are no such sequences.</p>
|
||||
|
||||
|
||||
|
||||
<p>After doing so, return the head of the final linked list. You may return any such answer.</p>
|
||||
|
||||
|
||||
<p> </p>
|
||||
<p>(Note that in the examples below, all sequences are serializations of <code>ListNode</code> objects.)</p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,-3,3,1]
|
||||
<strong>Output:</strong> [3,1]
|
||||
<strong>Note:</strong> The answer [1,2,1] would also be accepted.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,-3,4]
|
||||
<strong>Output:</strong> [1,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,-3,-2]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
|
@@ -1,28 +1,28 @@
|
||||
<p>Given a text file <code>file.txt</code>, print just the 10th line of the file.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<p>Assume that <code>file.txt</code> has the following content:</p>
|
||||
|
||||
<pre>
|
||||
Line 1
|
||||
Line 2
|
||||
Line 3
|
||||
Line 4
|
||||
Line 5
|
||||
Line 6
|
||||
Line 7
|
||||
Line 8
|
||||
Line 9
|
||||
Line 10
|
||||
</pre>
|
||||
|
||||
<p>Your script should output the tenth line, which is:</p>
|
||||
|
||||
<pre>
|
||||
Line 10
|
||||
</pre>
|
||||
|
||||
<div class="spoilers"><b>Note:</b><br />
|
||||
1. If the file contains less than 10 lines, what should you output?<br />
|
||||
2. There's at least three different solutions. Try to explore all possibilities.</div>
|
||||
<p>Given a text file <code>file.txt</code>, print just the 10th line of the file.</p>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p>Assume that <code>file.txt</code> has the following content:</p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
Line 1
|
||||
|
||||
Line 2
|
||||
|
||||
Line 3
|
||||
|
||||
Line 4
|
||||
|
||||
Line 5
|
||||
|
||||
Line 6
|
||||
|
||||
Line 7
|
||||
|
||||
|
@@ -1,34 +1,34 @@
|
||||
<p>Write a bash script to calculate the frequency of each word in a text file <code>words.txt</code>.</p>
|
||||
|
||||
<p>For simplicity sake, you may assume:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>words.txt</code> contains only lowercase characters and space <code>' '</code> characters.</li>
|
||||
<li>Each word must consist of lowercase characters only.</li>
|
||||
<li>Words are separated by one or more whitespace characters.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<p>Assume that <code>words.txt</code> has the following content:</p>
|
||||
|
||||
<pre>
|
||||
the day is sunny the the
|
||||
the sunny is is
|
||||
</pre>
|
||||
|
||||
<p>Your script should output the following, sorted by descending frequency:</p>
|
||||
|
||||
<pre>
|
||||
the 4
|
||||
is 3
|
||||
sunny 2
|
||||
day 1
|
||||
</pre>
|
||||
|
||||
<p><b>Note:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.</li>
|
||||
<li>Could you write it in one-line using <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html">Unix pipes</a>?</li>
|
||||
</ul>
|
||||
<p>Write a bash script to calculate the frequency of each word in a text file <code>words.txt</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>For simplicity sake, you may assume:</p>
|
||||
|
||||
|
||||
|
||||
<ul>
|
||||
|
||||
<li><code>words.txt</code> contains only lowercase characters and space <code>' '</code> characters.</li>
|
||||
|
||||
<li>Each word must consist of lowercase characters only.</li>
|
||||
|
||||
<li>Words are separated by one or more whitespace characters.</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
|
||||
|
||||
<p>Assume that <code>words.txt</code> has the following content:</p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
the day is sunny the the
|
||||
|
||||
the sunny is is
|
||||
|
||||
|
Reference in New Issue
Block a user