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,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>
|
Reference in New Issue
Block a user