mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 15:01:40 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/count-pairs-of-similar-strings.json
Normal file
180
leetcode/originData/count-pairs-of-similar-strings.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/cycle-length-queries-in-a-tree.json
Normal file
175
leetcode/originData/cycle-length-queries-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
190
leetcode/originData/non-decreasing-subsequences.json
Normal file
190
leetcode/originData/non-decreasing-subsequences.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,43 @@
|
||||
<p>There is an <strong>undirected</strong> graph consisting of <code>n</code> nodes numbered from <code>1</code> to <code>n</code>. You are given the integer <code>n</code> and a <strong>2D</strong> array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. The graph can be disconnected.</p>
|
||||
|
||||
<p>You can add <strong>at most</strong> two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if it is possible to make the degree of each node in the graph even, otherwise return </em><code>false</code><em>.</em></p>
|
||||
|
||||
<p>The degree of a node is the number of edges connected to it.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/agraphdrawio.png" style="width: 500px; height: 190px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram shows a valid way of adding an edge.
|
||||
Every node in the resulting graph is connected to an even number of edges.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aagraphdrawio.png" style="width: 400px; height: 120px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[1,2],[3,4]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram shows a valid way of adding two edges.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aaagraphdrawio.png" style="width: 150px; height: 158px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[1,2],[1,3],[1,4]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to obtain a valid graph with adding at most 2 edges.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= edges.length <= 10<sup>5</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 are no repeated edges.</li>
|
||||
</ul>
|
48
leetcode/problem/count-pairs-of-similar-strings.html
Normal file
48
leetcode/problem/count-pairs-of-similar-strings.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Two strings are <strong>similar</strong> if they consist of the same characters.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"abca"</code> and <code>"cba"</code> are similar since both consist of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li>
|
||||
<li>However, <code>"abacba"</code> and <code>"bcfd"</code> are not similar since they do not consist of the same characters.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of pairs </em><code>(i, j)</code><em> such that </em><code>0 <= i < j <= word.length - 1</code><em> and the two strings </em><code>words[i]</code><em> and </em><code>words[j]</code><em> are similar</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aba","aabb","abcd","bac","aabc"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 pairs that satisfy the conditions:
|
||||
- i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'.
|
||||
- i = 3 and j = 4 : both words[3] and words[4] only consist of characters 'a', 'b', and 'c'.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aabb","ab","ba"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 pairs that satisfy the conditions:
|
||||
- i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'.
|
||||
- i = 0 and j = 2 : both words[0] and words[2] only consist of characters 'a' and 'b'.
|
||||
- i = 1 and j = 2 : both words[1] and words[2] only consist of characters 'a' and 'b'.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["nba","cba","dba"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since there does not exist any pair that satisfies the conditions, we return 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
57
leetcode/problem/cycle-length-queries-in-a-tree.html
Normal file
57
leetcode/problem/cycle-length-queries-in-a-tree.html
Normal file
@@ -0,0 +1,57 @@
|
||||
<p>You are given an integer <code>n</code>. There is a <strong>complete binary tree</strong> with <code>2<sup>n</sup> - 1</code> nodes. The root of that tree is the node with the value <code>1</code>, and every node with a value <code>val</code> in the range <code>[1, 2<sup>n - 1</sup> - 1]</code> has two children where:</p>
|
||||
|
||||
<ul>
|
||||
<li>The left node has the value <code>2 * val</code>, and</li>
|
||||
<li>The right node has the value <code>2 * val + 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, solve the following problem:</p>
|
||||
|
||||
<ol>
|
||||
<li>Add an edge between the nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li>
|
||||
<li>Find the length of the cycle in the graph.</li>
|
||||
<li>Remove the added edge between nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li>
|
||||
</ol>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>cycle</strong> is a path that starts and ends at the same node, and each edge in the path is visited only once.</li>
|
||||
<li>The length of a cycle is the number of edges visited in the cycle.</li>
|
||||
<li>There could be multiple edges between two nodes in the tree after adding the edge of the query.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/bexample1.png" style="width: 647px; height: 128px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, queries = [[5,3],[4,7],[2,3]]
|
||||
<strong>Output:</strong> [4,5,3]
|
||||
<strong>Explanation:</strong> The diagrams above show the tree of 2<sup>3</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
|
||||
- After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query.
|
||||
- After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query.
|
||||
- After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/aexample2.png" style="width: 146px; height: 71px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, queries = [[1,2]]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> The diagram above shows the tree of 2<sup>2</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
|
||||
- After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 30</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= 2<sup>n</sup> - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
</ul>
|
24
leetcode/problem/non-decreasing-subsequences.html
Normal file
24
leetcode/problem/non-decreasing-subsequences.html
Normal file
@@ -0,0 +1,24 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>all the different possible non-decreasing subsequences of the given array with at least two elements</em>. You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,6,7,7]
|
||||
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,3,2,1]
|
||||
<strong>Output:</strong> [[4,4]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 15</code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a positive integer <code>n</code>.</p>
|
||||
|
||||
<p>Continuously replace <code>n</code> with the sum of its <strong>prime factors</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>Note that if a prime factor divides <code>n</code> multiple times, it should be included in the sum as many times as it divides <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the smallest value </em><code>n</code><em> will take on.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 15
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Initially, n = 15.
|
||||
15 = 3 * 5, so replace n with 3 + 5 = 8.
|
||||
8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
|
||||
6 = 2 * 3, so replace n with 2 + 3 = 5.
|
||||
5 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Initially, n = 3.
|
||||
3 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user