mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bidirectional </strong>road between cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with a distance equal to <code>distance<sub>i</sub></code>. The cities graph is not necessarily connected.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a path between two cities is defined as the <strong>minimum </strong>distance of a road in this path.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>possible score of a path between cities </em><code>1</code><em> and </em><code>n</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A path is a sequence of roads between two cities.</li>
|
||||
<li>It is allowed for a path to contain the same road <strong>multiple</strong> times, and you can visit cities <code>1</code> and <code>n</code> multiple times along the path.</li>
|
||||
<li>The test cases are generated such that there is <strong>at least</strong> one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph11.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
|
||||
It can be shown that no other path has less score.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph22.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>1 <= distance<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
<li>There is at least one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given the <code>head</code> of a linked list.</p>
|
||||
|
||||
<p>Remove every node which has a node with a <strong>strictly greater</strong> value anywhere to the right side of it.</p>
|
||||
|
||||
<p>Return <em>the </em><code>head</code><em> of the modified linked list.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [5,2,13,3,8]
|
||||
<strong>Output:</strong> [13,8]
|
||||
<strong>Explanation:</strong> The nodes that should be removed are 5, 2 and 3.
|
||||
- Node 13 is to the right of node 5.
|
||||
- Node 13 is to the right of node 2.
|
||||
- Node 8 is to the right of node 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,1,1,1]
|
||||
<strong>Output:</strong> [1,1,1,1]
|
||||
<strong>Explanation:</strong> Every node has value 1, so no nodes are removed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>A <strong>valid cut</strong> in a circle can be:</p>
|
||||
|
||||
<ul>
|
||||
<li>A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or</li>
|
||||
<li>A cut that is represented by a straight line that touches one point on the edge of the circle and its center.</li>
|
||||
</ul>
|
||||
|
||||
<p>Some valid and invalid cuts are shown in the figures below.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/alldrawio.png" style="width: 450px; height: 174px;" />
|
||||
<p>Given the integer <code>n</code>, return <em>the <strong>minimum</strong> number of cuts needed to divide a circle into </em><code>n</code><em> equal slices</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/11drawio.png" style="width: 200px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/22drawio.png" style="width: 200px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
At least 3 cuts are needed to divide the circle into 3 equal slices.
|
||||
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
|
||||
Also note that the first cut will not divide the circle into distinct parts.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given a positive integer array <code>skill</code> of <strong>even</strong> length <code>n</code> where <code>skill[i]</code> denotes the skill of the <code>i<sup>th</sup></code> player. Divide the players into <code>n / 2</code> teams of size <code>2</code> such that the total skill of each team is <strong>equal</strong>.</p>
|
||||
|
||||
<p>The <strong>chemistry</strong> of a team is equal to the <strong>product</strong> of the skills of the players on that team.</p>
|
||||
|
||||
<p>Return <em>the sum of the <strong>chemistry</strong> of all the teams, or return </em><code>-1</code><em> if there is no way to divide the players into teams such that the total skill of each team is equal.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> skill = [3,2,5,1,3,4]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong>
|
||||
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
|
||||
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> skill = [3,4]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong>
|
||||
The two players form a team with a total skill of 7.
|
||||
The chemistry of the team is 3 * 4 = 12.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> skill = [1,1,2,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong>
|
||||
There is no way to divide the players into teams such that the total skill of each team is equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= skill.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>skill.length</code> is even.</li>
|
||||
<li><code>1 <= skill[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>'N'</code> and <code>'Y'</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>if the <code>i<sup>th</sup></code> character is <code>'Y'</code>, it means that customers come at the <code>i<sup>th</sup></code> hour</li>
|
||||
<li>whereas <code>'N'</code> indicates that no customers come at the <code>i<sup>th</sup></code> hour.</li>
|
||||
</ul>
|
||||
|
||||
<p>If the shop closes at the <code>j<sup>th</sup></code> hour (<code>0 <= j <= n</code>), the <strong>penalty</strong> is calculated as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>For every hour when the shop is open and no customers come, the penalty increases by <code>1</code>.</li>
|
||||
<li>For every hour when the shop is closed and customers come, the penalty increases by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>earliest</strong> hour at which the shop must be closed to incur a <strong>minimum</strong> penalty.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that if a shop closes at the <code>j<sup>th</sup></code> hour, it means the shop is closed at the hour <code>j</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> customers = "YYNY"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- Closing the shop at the 0<sup>th</sup> hour incurs in 1+1+0+1 = 3 penalty.
|
||||
- Closing the shop at the 1<sup>st</sup> hour incurs in 0+1+0+1 = 2 penalty.
|
||||
- Closing the shop at the 2<sup>nd</sup> hour incurs in 0+0+0+1 = 1 penalty.
|
||||
- Closing the shop at the 3<sup>rd</sup> hour incurs in 0+0+1+1 = 2 penalty.
|
||||
- Closing the shop at the 4<sup>th</sup> hour incurs in 0+0+1+0 = 1 penalty.
|
||||
Closing the shop at 2<sup>nd</sup> or 4<sup>th</sup> hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> customers = "NNNNN"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> It is best to close the shop at the 0<sup>th</sup> hour as no customers arrive.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> customers = "YYYY"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> It is best to close the shop at the 4<sup>th</sup> hour as customers arrive at each hour.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= customers.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>customers</code> consists only of characters <code>'Y'</code> and <code>'N'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>A <strong>sentence</strong> is a list of words that are separated by a<strong> single</strong> space with no leading or trailing spaces.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"Hello World"</code>, <code>"HELLO"</code>, <code>"hello world hello world"</code> are all sentences.</li>
|
||||
</ul>
|
||||
|
||||
<p>Words consist of <strong>only</strong> uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.</p>
|
||||
|
||||
<p>A sentence is <strong>circular </strong>if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The last character of a word is equal to the first character of the next word.</li>
|
||||
<li>The last character of the last word is equal to the first character of the first word.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>"leetcode exercises sound delightful"</code>, <code>"eetcode"</code>, <code>"leetcode eats soul" </code>are all circular sentences. However, <code>"Leetcode is cool"</code>, <code>"happy Leetcode"</code>, <code>"Leetcode"</code> and <code>"I like Leetcode"</code> are <strong>not</strong> circular sentences.</p>
|
||||
|
||||
<p>Given a string <code>sentence</code>, return <code>true</code><em> if it is circular</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "leetcode exercises sound delightful"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The words in sentence are ["leetcode", "exercises", "sound", "delightful"].
|
||||
- leetcod<u>e</u>'s last character is equal to <u>e</u>xercises's first character.
|
||||
- exercise<u>s</u>'s last character is equal to <u>s</u>ound's first character.
|
||||
- soun<u>d</u>'s last character is equal to <u>d</u>elightful's first character.
|
||||
- delightfu<u>l</u>'s last character is equal to <u>l</u>eetcode's first character.
|
||||
The sentence is circular.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "eetcode"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The words in sentence are ["eetcode"].
|
||||
- eetcod<u>e</u>'s last character is equal to <u>e</u>etcode's first character.
|
||||
The sentence is circular.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "Leetcode is cool"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The words in sentence are ["Leetcode", "is", "cool"].
|
||||
- Leetcod<u>e</u>'s last character is <strong>not</strong> equal to <u>i</u>s's first character.
|
||||
The sentence is <strong>not</strong> circular.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 500</code></li>
|
||||
<li><code>sentence</code> consist of only lowercase and uppercase English letters and spaces.</li>
|
||||
<li>The words in <code>sentence</code> are separated by a single space.</li>
|
||||
<li>There are no leading or trailing spaces.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a positive integer <code>n</code> representing the number of nodes in an <strong>undirected</strong> graph. The nodes are labeled from <code>1</code> to <code>n</code>.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = [a<sub>i, </sub>b<sub>i</sub>]</code> indicates that there is a <strong>bidirectional</strong> edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. <strong>Notice</strong> that the given graph may be disconnected.</p>
|
||||
|
||||
<p>Divide the nodes of the graph into <code>m</code> groups (<strong>1-indexed</strong>) such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each node in the graph belongs to exactly one group.</li>
|
||||
<li>For every pair of nodes in the graph that are connected by an edge <code>[a<sub>i, </sub>b<sub>i</sub>]</code>, if <code>a<sub>i</sub></code> belongs to the group with index <code>x</code>, and <code>b<sub>i</sub></code> belongs to the group with index <code>y</code>, then <code>|y - x| = 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum number of groups (i.e., maximum </em><code>m</code><em>) into which you can divide the nodes</em>. Return <code>-1</code> <em>if it is impossible to group the nodes with the given conditions</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/13/example1.png" style="width: 352px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> As shown in the image we:
|
||||
- Add node 5 to the first group.
|
||||
- Add node 1 to the second group.
|
||||
- Add nodes 2 and 4 to the third group.
|
||||
- Add nodes 3 and 6 to the fourth group.
|
||||
We can see that every edge is satisfied.
|
||||
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[1,2],[2,3],[3,1]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
|
||||
It can be shown that no grouping is possible.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>1 <= edges.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There is at most one edge between any pair of vertices.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a positive integer <code>n</code>, find the <strong>pivot integer</strong> <code>x</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The sum of all elements between <code>1</code> and <code>x</code> inclusively equals the sum of all elements between <code>x</code> and <code>n</code> inclusively.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the pivot integer </em><code>x</code>. If no such integer exists, return <code>-1</code>. It is guaranteed that there will be at most one pivot index for the given input.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 8
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> 1 is the pivot integer since: 1 = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proved that no such integer exist.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given an array <code>nums</code> of size <code>n</code> consisting of <strong>distinct </strong>integers from <code>1</code> to <code>n</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of non-empty subarrays in </em><code>nums</code><em> that have a <strong>median</strong> equal to </em><code>k</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>The median of an array is the <strong>middle </strong>element after sorting the array in <strong>ascending </strong>order. If the array is of even length, the median is the <strong>left </strong>middle element.
|
||||
|
||||
<ul>
|
||||
<li>For example, the median of <code>[2,3,1,4]</code> is <code>2</code>, and the median of <code>[8,4,3,5,1]</code> is <code>4</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>A subarray is a contiguous part of an array.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,4,5], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,1], k = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> [3] is the only subarray that has a median equal to 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], k <= n</code></li>
|
||||
<li>The integers in <code>nums</code> are distinct.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given a string of digits <code>s</code>, return <em>the number of <strong>palindromic subsequences</strong> of</em> <code>s</code><em> having length </em><code>5</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A string is <strong>palindromic</strong> if it reads the same forward and backward.</li>
|
||||
<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "103301"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
There are 6 possible subsequences of length 5: "10330","10331","10301","10301","13301","03301".
|
||||
Two of them (both equal to "10301") are palindromic.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0000000"
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> All 21 subsequences are "00000", which is palindromic.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "9999900000"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The only two palindromic subsequences are "99999" and "00000".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of digits.</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p>
|
||||
|
||||
<p>A <strong>0-indexed</strong> <code>m x n</code> difference matrix <code>diff</code> is created with the following procedure:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let the number of ones in the <code>i<sup>th</sup></code> row be <code>onesRow<sub>i</sub></code>.</li>
|
||||
<li>Let the number of ones in the <code>j<sup>th</sup></code> column be <code>onesCol<sub>j</sub></code>.</li>
|
||||
<li>Let the number of zeros in the <code>i<sup>th</sup></code> row be <code>zerosRow<sub>i</sub></code>.</li>
|
||||
<li>Let the number of zeros in the <code>j<sup>th</sup></code> column be <code>zerosCol<sub>j</sub></code>.</li>
|
||||
<li><code>diff[i][j] = onesRow<sub>i</sub> + onesCol<sub>j</sub> - zerosRow<sub>i</sub> - zerosCol<sub>j</sub></code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the difference matrix </em><code>diff</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/11/06/image-20221106171729-5.png" style="width: 400px; height: 208px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,1],[1,0,1],[0,0,1]]
|
||||
<strong>Output:</strong> [[0,0,4],[0,0,4],[-2,-2,2]]
|
||||
<strong>Explanation:</strong>
|
||||
- diff[0][0] = <code>onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
|
||||
- diff[0][1] = <code>onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
|
||||
- diff[0][2] = <code>onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
|
||||
- diff[1][0] = <code>onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0
|
||||
- diff[1][1] = <code>onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0
|
||||
- diff[1][2] = <code>onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4
|
||||
- diff[2][0] = <code>onesRow<sub>2</sub> + onesCol<sub>0</sub> - zerosRow<sub>2</sub> - zerosCol<sub>0</sub></code> = 1 + 1 - 2 - 2 = -2
|
||||
- diff[2][1] = <code>onesRow<sub>2</sub> + onesCol<sub>1</sub> - zerosRow<sub>2</sub> - zerosCol<sub>1</sub></code> = 1 + 1 - 2 - 2 = -2
|
||||
- diff[2][2] = <code>onesRow<sub>2</sub> + onesCol<sub>2</sub> - zerosRow<sub>2</sub> - zerosCol<sub>2</sub></code> = 1 + 3 - 2 - 0 = 2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/11/06/image-20221106171747-6.png" style="width: 358px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,1,1]]
|
||||
<strong>Output:</strong> [[5,5,5],[5,5,5]]
|
||||
<strong>Explanation:</strong>
|
||||
- diff[0][0] = onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
|
||||
- diff[0][1] = onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
|
||||
- diff[0][2] = onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
|
||||
- diff[1][0] = onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5
|
||||
- diff[1][1] = onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5
|
||||
- diff[1][2] = onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p>
|
||||
|
||||
<p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "coaching", t = "coding"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Append the characters "ding" to the end of s so that s = "coachingding".
|
||||
Now, t is a subsequence of s ("<u><strong>co</strong></u>aching<u><strong>ding</strong></u>").
|
||||
It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcde", t = "a"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> t is already a subsequence of s ("<u><strong>a</strong></u>bcde").
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "z", t = "abcde"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Append the characters "abcde" to the end of s so that s = "zabcde".
|
||||
Now, t is a subsequence of s ("z<u><strong>abcde</strong></u>").
|
||||
It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> and <code>t</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user