mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
9ac85e5cbf
commit
32ab538e5f
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.12.14**
|
||||
> 最后更新日期: **2022.12.24**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
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
183
leetcode-cn/originData/count-pairs-of-similar-strings.json
Normal file
183
leetcode-cn/originData/count-pairs-of-similar-strings.json
Normal file
File diff suppressed because one or more lines are too long
178
leetcode-cn/originData/cycle-length-queries-in-a-tree.json
Normal file
178
leetcode-cn/originData/cycle-length-queries-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
186
leetcode-cn/originData/non-decreasing-subsequences.json
Normal file
186
leetcode-cn/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,37 @@
|
||||
<p>给你一个正整数 <code>n</code> 。</p>
|
||||
|
||||
<p>请你将 <code>n</code> 的值替换为 <code>n</code> 的 <strong>质因数</strong> 之和,重复这一过程。</p>
|
||||
|
||||
<ul>
|
||||
<li>注意,如果 <code>n</code> 能够被某个质因数多次整除,则在求和时,应当包含这个质因数同样次数。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回<em> </em><code>n</code><em> </em>可以取到的最小值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 15
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>最开始,n = 15 。
|
||||
15 = 3 * 5 ,所以 n 替换为 3 + 5 = 8 。
|
||||
8 = 2 * 2 * 2 ,所以 n 替换为 2 + 2 + 2 = 6 。
|
||||
6 = 2 * 3 ,所以 n 替换为 2 + 3 = 5 。
|
||||
5 是 n 可以取到的最小值。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>最开始,n = 3 。
|
||||
3 是 n 可以取到的最小值。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,63 @@
|
||||
<p>给你一个整数 <code>n</code> ,表示你有一棵含有 <code>2<sup>n</sup> - 1</code> 个节点的 <strong>完全二叉树</strong> 。根节点的编号是 <code>1</code> ,树中编号在<code>[1, 2<sup>n - 1</sup> - 1]</code> 之间,编号为 <code>val</code> 的节点都有两个子节点,满足:</p>
|
||||
|
||||
<ul>
|
||||
<li>左子节点的编号为 <code>2 * val</code></li>
|
||||
<li>右子节点的编号为 <code>2 * val + 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个长度为 <code>m</code> 的查询数组 <code>queries</code> ,它是一个二维整数数组,其中 <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 。对于每个查询,求出以下问题的解:</p>
|
||||
|
||||
<ol>
|
||||
<li>在节点编号为 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间添加一条边。</li>
|
||||
<li>求出图中环的长度。</li>
|
||||
<li>删除节点编号为 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间新添加的边。</li>
|
||||
</ol>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>环</strong> 是开始和结束于同一节点的一条路径,路径中每条边都只会被访问一次。</li>
|
||||
<li>环的长度是环中边的数目。</li>
|
||||
<li>在树中添加额外的边后,两个点之间可能会有多条边。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>m</code> 的数组<em> </em><code>answer</code> ,其中 <code>answer[i]</code> 是第 <code>i</code> 个查询的结果<i>。</i></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/bexample1.png" style="width: 647px; height: 128px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, queries = [[5,3],[4,7],[2,3]]
|
||||
<b>输出:</b>[4,5,3]
|
||||
<b>解释:</b>上图是一棵有 2<sup>3</sup> - 1 个节点的树。红色节点表示添加额外边后形成环的节点。
|
||||
- 在节点 3 和节点 5 之间添加边后,环为 [5,2,1,3] ,所以第一个查询的结果是 4 。删掉添加的边后处理下一个查询。
|
||||
- 在节点 4 和节点 7 之间添加边后,环为 [4,2,1,3,7] ,所以第二个查询的结果是 5 。删掉添加的边后处理下一个查询。
|
||||
- 在节点 2 和节点 3 之间添加边后,环为 [2,1,3] ,所以第三个查询的结果是 3 。删掉添加的边。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/aexample2.png" style="width: 146px; height: 71px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2, queries = [[1,2]]
|
||||
<b>输出:</b>[2]
|
||||
<b>解释:</b>上图是一棵有 2<sup>2</sup> - 1 个节点的树。红色节点表示添加额外边后形成环的节点。
|
||||
- 在节点 1 和节点 2 之间添加边后,环为 [2,1] ,所以第一个查询的结果是 2 。删掉添加的边。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个有 <code>n</code> 个节点的 <strong>无向</strong> 图,节点编号为 <code>1</code> 到 <code>n</code> 。再给你整数 <code>n</code> 和一个二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条边。图不一定连通。</p>
|
||||
|
||||
<p>你可以给图中添加 <strong>至多</strong> 两条额外的边(也可以一条边都不添加),使得图中没有重边也没有自环。</p>
|
||||
|
||||
<p>如果添加额外的边后,可以使得图中所有点的度数都是偶数,返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p>点的度数是连接一个点的边的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/agraphdrawio.png" style="width: 500px; height: 190px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>上图展示了添加一条边的合法方案。
|
||||
最终图中每个节点都连接偶数条边。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aagraphdrawio.png" style="width: 400px; height: 120px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 4, edges = [[1,2],[3,4]]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>上图展示了添加两条边的合法方案。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aaagraphdrawio.png" style="width: 150px; height: 158px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 4, edges = [[1,2],[1,3],[1,4]]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>无法添加至多 2 条边得到一个符合要求的图。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>图中不会有重边</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>
|
||||
|
||||
<p>如果两个字符串由相同的字符组成,则认为这两个字符串 <strong>相似</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"abca"</code> 和 <code>"cba"</code> 相似,因为它们都由字符 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成。</li>
|
||||
<li>然而,<code>"abacba"</code> 和 <code>"bcfd"</code> 不相似,因为它们不是相同字符组成的。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你找出满足字符串 <code>words[i]</code><em> </em>和<em> </em><code>words[j]</code> 相似的下标对<em> </em><code>(i, j)</code><em> </em>,并返回下标对的数目,其中 <code>0 <= i < j <= word.length - 1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["aba","aabb","abcd","bac","aabc"]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>共有 2 对满足条件:
|
||||
- i = 0 且 j = 1 :words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。
|
||||
- i = 3 且 j = 4 :words[3] 和 words[4] 只由字符 'a'、'b' 和 'c' 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["aabb","ab","ba"]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>共有 3 对满足条件:
|
||||
- i = 0 且 j = 1 :words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。
|
||||
- i = 0 且 j = 2 :words[0] 和 words[2] 只由字符 'a' 和 'b' 组成。
|
||||
- i = 1 且 j = 2 :words[1] 和 words[2] 只由字符 'a' 和 'b' 组成。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["nba","cba","dba"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不存在满足条件的下标对,返回 0 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@ -0,0 +1,28 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,找出并返回所有该数组中不同的递增子序列,递增子序列中 <strong>至少有两个元素</strong> 。你可以按 <strong>任意顺序</strong> 返回答案。</p>
|
||||
|
||||
<p>数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,6,7,7]
|
||||
<strong>输出:</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>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,4,3,2,1]
|
||||
<strong>输出:</strong>[[4,4]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>
|
@ -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>
|
@ -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>
|
@ -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>
|
@ -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>
|
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>
|
Loading…
Reference in New Issue
Block a user