mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
83a5997ef9
commit
cb4f82d60a
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.09.20**
|
||||
> 最后更新日期: **2023.09.24**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
165
leetcode-cn/originData/beautiful-towers-i.json
Normal file
165
leetcode-cn/originData/beautiful-towers-i.json
Normal file
File diff suppressed because one or more lines are too long
167
leetcode-cn/originData/beautiful-towers-ii.json
Normal file
167
leetcode-cn/originData/beautiful-towers-ii.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode-cn/originData/count-valid-paths-in-a-tree.json
Normal file
168
leetcode-cn/originData/count-valid-paths-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/maximum-odd-binary-number.json
Normal file
163
leetcode-cn/originData/maximum-odd-binary-number.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,35 @@
|
||||
<p>给你一个 <strong>二进制</strong> 字符串 <code>s</code> ,其中至少包含一个 <code>'1'</code> 。</p>
|
||||
|
||||
<p>你必须按某种方式 <strong>重新排列</strong> 字符串中的位,使得到的二进制数字是可以由该组合生成的 <strong>最大二进制奇数</strong> 。</p>
|
||||
|
||||
<p>以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。</p>
|
||||
|
||||
<p><strong>注意 </strong>返回的结果字符串 <strong>可以</strong> 含前导零。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "010"
|
||||
<strong>输出:</strong>"001"
|
||||
<strong>解释:</strong>因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "0101"
|
||||
<strong>输出:</strong>"1001"
|
||||
<strong>解释:</strong>其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 仅由 <code>'0'</code> 和 <code>'1'</code> 组成</li>
|
||||
<li><code>s</code> 中至少包含一个 <code>'1'</code></li>
|
||||
</ul>
|
@ -0,0 +1,58 @@
|
||||
<p>给你一棵 <code>n</code> 个节点的无向树,节点编号为 <code>1</code> 到 <code>n</code> 。给你一个整数 <code>n</code> 和一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 在树中有一条边。</p>
|
||||
|
||||
<p>请你返回树中的 <strong>合法路径数目</strong> 。</p>
|
||||
|
||||
<p>如果在节点 <code>a</code> 到节点 <code>b</code> 之间 <strong>恰好有一个</strong> 节点的编号是质数,那么我们称路径 <code>(a, b)</code> 是 <strong>合法的</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>路径 <code>(a, b)</code> 指的是一条从节点 <code>a</code> 开始到节点 <code>b</code> 结束的一个节点序列,序列中的节点 <strong>互不相同</strong> ,且相邻节点之间在树上有一条边。</li>
|
||||
<li>路径 <code>(a, b)</code> 和路径 <code>(b, a)</code> 视为 <strong>同一条</strong> 路径,且只计入答案 <strong>一次</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example1.png" style="width: 440px; height: 357px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>恰好有一个质数编号的节点路径有:
|
||||
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
|
||||
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
|
||||
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
|
||||
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
|
||||
只有 4 条合法路径。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example2.png" style="width: 488px; height: 384px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>恰好有一个质数编号的节点路径有:
|
||||
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
|
||||
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
|
||||
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
|
||||
- (1, 6) 因为路径 1 到 6 只包含一个质数 3 。
|
||||
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
|
||||
- (3, 6) 因为路径 3 到 6 只包含一个质数 3 。
|
||||
只有 6 条合法路径。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li>输入保证 <code>edges</code> 形成一棵合法的树。</li>
|
||||
</ul>
|
@ -0,0 +1,62 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>maxHeights</code> 。</p>
|
||||
|
||||
<p>你的任务是在坐标轴上建 <code>n</code> 座塔。第 <code>i</code> 座塔的下标为 <code>i</code> ,高度为 <code>heights[i]</code> 。</p>
|
||||
|
||||
<p>如果以下条件满足,我们称这些塔是 <strong>美丽</strong> 的:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> 是一个 <strong>山状</strong> 数组。</li>
|
||||
</ol>
|
||||
|
||||
<p>如果存在下标 <code>i</code> 满足以下条件,那么我们称数组 <code>heights</code> 是一个 <strong>山状</strong> 数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于所有 <code>0 < j <= i</code> ,都有 <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>对于所有 <code>i <= k < n - 1</code> ,都有 <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足 <b>美丽塔</b> 要求的方案中,<strong>高度和的最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [5,3,4,1,1]
|
||||
<b>输出:</b>13
|
||||
<b>解释:</b>和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 0 处。
|
||||
13 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [6,5,3,9,2,7]
|
||||
<b>输出:</b>22
|
||||
<strong>解释:</strong> 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 3 处。
|
||||
22 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [3,2,5,5,2,3]
|
||||
<b>输出:</b>18
|
||||
<strong>解释:</strong>和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,最大值在 i = 2 处。
|
||||
注意,在这个方案中,i = 3 也是一个峰值。
|
||||
18 是所有美丽塔方案中的最大高度和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,62 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>maxHeights</code> 。</p>
|
||||
|
||||
<p>你的任务是在坐标轴上建 <code>n</code> 座塔。第 <code>i</code> 座塔的下标为 <code>i</code> ,高度为 <code>heights[i]</code> 。</p>
|
||||
|
||||
<p>如果以下条件满足,我们称这些塔是 <strong>美丽</strong> 的:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> 是一个 <strong>山状</strong> 数组。</li>
|
||||
</ol>
|
||||
|
||||
<p>如果存在下标 <code>i</code> 满足以下条件,那么我们称数组 <code>heights</code> 是一个 <strong>山状</strong> 数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于所有 <code>0 < j <= i</code> ,都有 <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>对于所有 <code>i <= k < n - 1</code> ,都有 <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足 <b>美丽塔</b> 要求的方案中,<strong>高度和的最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [5,3,4,1,1]
|
||||
<b>输出:</b>13
|
||||
<b>解释:</b>和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 0 处。
|
||||
13 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [6,5,3,9,2,7]
|
||||
<b>输出:</b>22
|
||||
<strong>解释:</strong> 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,峰值在 i = 3 处。
|
||||
22 是所有美丽塔方案中的最大高度和。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxHeights = [3,2,5,5,2,3]
|
||||
<b>输出:</b>18
|
||||
<strong>解释:</strong>和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights 是个山状数组,最大值在 i = 2 处。
|
||||
注意,在这个方案中,i = 3 也是一个峰值。
|
||||
18 是所有美丽塔方案中的最大高度和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>binary</strong> string <code>s</code> that contains at least one <code>'1'</code>.</p>
|
||||
|
||||
<p>You have to <strong>rearrange</strong> the bits in such a way that the resulting binary number is the <strong>maximum odd binary number</strong> that can be created from this combination.</p>
|
||||
|
||||
<p>Return <em>a string representing the maximum odd binary number that can be created from the given combination.</em></p>
|
||||
|
||||
<p><strong>Note </strong>that the resulting string <strong>can</strong> have leading zeros.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "010"
|
||||
<strong>Output:</strong> "001"
|
||||
<strong>Explanation:</strong> Because there is just one '1', it must be in the last position. So the answer is "001".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0101"
|
||||
<strong>Output:</strong> "1001"
|
||||
<strong>Explanation: </strong>One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of <code>'0'</code> and <code>'1'</code>.</li>
|
||||
<li><code>s</code> contains at least one <code>'1'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of valid paths</strong> in the tree</em>.</p>
|
||||
|
||||
<p>A path <code>(a, b)</code> is <strong>valid</strong> if there exists <strong>exactly one</strong> prime number among the node labels in the path from <code>a</code> to <code>b</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path <code>(a, b)</code> is a sequence of <strong>distinct</strong> nodes starting with node <code>a</code> and ending with node <code>b</code> such that every two adjacent nodes in the sequence share an edge in the tree.</li>
|
||||
<li>Path <code>(a, b)</code> and path <code>(b, a)</code> are considered the <strong>same</strong> and counted only <strong>once</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example1.png" style="width: 440px; height: 357px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pairs with exactly one prime number on the path between them are:
|
||||
- (1, 2) since the path from 1 to 2 contains prime number 2.
|
||||
- (1, 3) since the path from 1 to 3 contains prime number 3.
|
||||
- (1, 4) since the path from 1 to 4 contains prime number 2.
|
||||
- (2, 4) since the path from 2 to 4 contains prime number 2.
|
||||
It can be shown that there are only 4 valid paths.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example2.png" style="width: 488px; height: 384px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The pairs with exactly one prime number on the path between them are:
|
||||
- (1, 2) since the path from 1 to 2 contains prime number 2.
|
||||
- (1, 3) since the path from 1 to 3 contains prime number 3.
|
||||
- (1, 4) since the path from 1 to 4 contains prime number 2.
|
||||
- (1, 6) since the path from 1 to 6 contains prime number 3.
|
||||
- (2, 4) since the path from 2 to 4 contains prime number 2.
|
||||
- (3, 6) since the path from 3 to 6 contains prime number 3.
|
||||
It can be shown that there are only 6 valid paths.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represent a valid tree.</li>
|
||||
</ul>
|
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>maxHeights</code> of <code>n</code> integers.</p>
|
||||
|
||||
<p>You are tasked with building <code>n</code> towers in the coordinate line. The <code>i<sup>th</sup></code> tower is built at coordinate <code>i</code> and has a height of <code>heights[i]</code>.</p>
|
||||
|
||||
<p>A configuration of towers is <strong>beautiful</strong> if the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> is a <strong>mountain</strong> array.</li>
|
||||
</ol>
|
||||
|
||||
<p>Array <code>heights</code> is a <strong>mountain</strong> if there exists an index <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>For all <code>0 < j <= i</code>, <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>For all <code>i <= k < n - 1</code>, <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible sum of heights</strong> of a beautiful configuration of towers</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [5,3,4,1,1]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 0.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [6,5,3,9,2,7]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 3.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [3,2,5,5,2,3]
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 2.
|
||||
Note that, for this configuration, i = 3 can also be considered a peak.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>maxHeights</code> of <code>n</code> integers.</p>
|
||||
|
||||
<p>You are tasked with building <code>n</code> towers in the coordinate line. The <code>i<sup>th</sup></code> tower is built at coordinate <code>i</code> and has a height of <code>heights[i]</code>.</p>
|
||||
|
||||
<p>A configuration of towers is <strong>beautiful</strong> if the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> is a <strong>mountain</strong> array.</li>
|
||||
</ol>
|
||||
|
||||
<p>Array <code>heights</code> is a <strong>mountain</strong> if there exists an index <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>For all <code>0 < j <= i</code>, <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>For all <code>i <= k < n - 1</code>, <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible sum of heights</strong> of a beautiful configuration of towers</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [5,3,4,1,1]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 0.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [6,5,3,9,2,7]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 3.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [3,2,5,5,2,3]
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 2.
|
||||
Note that, for this configuration, i = 3 can also be considered a peak.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
162
leetcode/originData/beautiful-towers-i.json
Normal file
162
leetcode/originData/beautiful-towers-i.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode/originData/beautiful-towers-ii.json
Normal file
164
leetcode/originData/beautiful-towers-ii.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode/originData/count-valid-paths-in-a-tree.json
Normal file
165
leetcode/originData/count-valid-paths-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/maximum-odd-binary-number.json
Normal file
160
leetcode/originData/maximum-odd-binary-number.json
Normal file
File diff suppressed because one or more lines are too long
60
leetcode/problem/beautiful-towers-i.html
Normal file
60
leetcode/problem/beautiful-towers-i.html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>maxHeights</code> of <code>n</code> integers.</p>
|
||||
|
||||
<p>You are tasked with building <code>n</code> towers in the coordinate line. The <code>i<sup>th</sup></code> tower is built at coordinate <code>i</code> and has a height of <code>heights[i]</code>.</p>
|
||||
|
||||
<p>A configuration of towers is <strong>beautiful</strong> if the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> is a <strong>mountain</strong> array.</li>
|
||||
</ol>
|
||||
|
||||
<p>Array <code>heights</code> is a <strong>mountain</strong> if there exists an index <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>For all <code>0 < j <= i</code>, <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>For all <code>i <= k < n - 1</code>, <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible sum of heights</strong> of a beautiful configuration of towers</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [5,3,4,1,1]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 0.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [6,5,3,9,2,7]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 3.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [3,2,5,5,2,3]
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 2.
|
||||
Note that, for this configuration, i = 3 can also be considered a peak.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
60
leetcode/problem/beautiful-towers-ii.html
Normal file
60
leetcode/problem/beautiful-towers-ii.html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>maxHeights</code> of <code>n</code> integers.</p>
|
||||
|
||||
<p>You are tasked with building <code>n</code> towers in the coordinate line. The <code>i<sup>th</sup></code> tower is built at coordinate <code>i</code> and has a height of <code>heights[i]</code>.</p>
|
||||
|
||||
<p>A configuration of towers is <strong>beautiful</strong> if the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= heights[i] <= maxHeights[i]</code></li>
|
||||
<li><code>heights</code> is a <strong>mountain</strong> array.</li>
|
||||
</ol>
|
||||
|
||||
<p>Array <code>heights</code> is a <strong>mountain</strong> if there exists an index <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>For all <code>0 < j <= i</code>, <code>heights[j - 1] <= heights[j]</code></li>
|
||||
<li>For all <code>i <= k < n - 1</code>, <code>heights[k + 1] <= heights[k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible sum of heights</strong> of a beautiful configuration of towers</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [5,3,4,1,1]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 0.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [6,5,3,9,2,7]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 3.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxHeights = [3,2,5,5,2,3]
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
|
||||
- 1 <= heights[i] <= maxHeights[i]
|
||||
- heights is a mountain of peak i = 2.
|
||||
Note that, for this configuration, i = 3 can also be considered a peak.
|
||||
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == maxHeights <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= maxHeights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
52
leetcode/problem/count-valid-paths-in-a-tree.html
Normal file
52
leetcode/problem/count-valid-paths-in-a-tree.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of valid paths</strong> in the tree</em>.</p>
|
||||
|
||||
<p>A path <code>(a, b)</code> is <strong>valid</strong> if there exists <strong>exactly one</strong> prime number among the node labels in the path from <code>a</code> to <code>b</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path <code>(a, b)</code> is a sequence of <strong>distinct</strong> nodes starting with node <code>a</code> and ending with node <code>b</code> such that every two adjacent nodes in the sequence share an edge in the tree.</li>
|
||||
<li>Path <code>(a, b)</code> and path <code>(b, a)</code> are considered the <strong>same</strong> and counted only <strong>once</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example1.png" style="width: 440px; height: 357px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pairs with exactly one prime number on the path between them are:
|
||||
- (1, 2) since the path from 1 to 2 contains prime number 2.
|
||||
- (1, 3) since the path from 1 to 3 contains prime number 3.
|
||||
- (1, 4) since the path from 1 to 4 contains prime number 2.
|
||||
- (2, 4) since the path from 2 to 4 contains prime number 2.
|
||||
It can be shown that there are only 4 valid paths.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/27/example2.png" style="width: 488px; height: 384px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The pairs with exactly one prime number on the path between them are:
|
||||
- (1, 2) since the path from 1 to 2 contains prime number 2.
|
||||
- (1, 3) since the path from 1 to 3 contains prime number 3.
|
||||
- (1, 4) since the path from 1 to 4 contains prime number 2.
|
||||
- (1, 6) since the path from 1 to 6 contains prime number 3.
|
||||
- (2, 4) since the path from 2 to 4 contains prime number 2.
|
||||
- (3, 6) since the path from 3 to 6 contains prime number 3.
|
||||
It can be shown that there are only 6 valid paths.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represent a valid tree.</li>
|
||||
</ul>
|
33
leetcode/problem/maximum-odd-binary-number.html
Normal file
33
leetcode/problem/maximum-odd-binary-number.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>binary</strong> string <code>s</code> that contains at least one <code>'1'</code>.</p>
|
||||
|
||||
<p>You have to <strong>rearrange</strong> the bits in such a way that the resulting binary number is the <strong>maximum odd binary number</strong> that can be created from this combination.</p>
|
||||
|
||||
<p>Return <em>a string representing the maximum odd binary number that can be created from the given combination.</em></p>
|
||||
|
||||
<p><strong>Note </strong>that the resulting string <strong>can</strong> have leading zeros.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "010"
|
||||
<strong>Output:</strong> "001"
|
||||
<strong>Explanation:</strong> Because there is just one '1', it must be in the last position. So the answer is "001".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0101"
|
||||
<strong>Output:</strong> "1001"
|
||||
<strong>Explanation: </strong>One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of <code>'0'</code> and <code>'1'</code>.</li>
|
||||
<li><code>s</code> contains at least one <code>'1'</code>.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user