mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<p>You are given a 2D integer array <code>edges</code> representing an <strong>undirected</strong> graph having <code>n</code> nodes, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Construct a 2D grid that satisfies these conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The grid contains <strong>all nodes</strong> from <code>0</code> to <code>n - 1</code> in its cells, with each node appearing exactly <strong>once</strong>.</li>
|
||||
<li>Two nodes should be in adjacent grid cells (<strong>horizontally</strong> or <strong>vertically</strong>) <strong>if and only if</strong> there is an edge between them in <code>edges</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>It is guaranteed that <code>edges</code> can form a 2D grid that satisfies the conditions.</p>
|
||||
|
||||
<p>Return a 2D integer array satisfying the conditions above. If there are multiple solutions, return <em>any</em> of them.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[0,1],[0,2],[1,3],[2,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[3,1],[2,0]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-07-59.png" style="width: 133px; height: 92px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[0,1],[1,3],[2,3],[2,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[4,2,3,1,0]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-06-02.png" style="width: 325px; height: 50px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 9, edges = [[0,1],[0,4],[0,5],[1,7],[2,3],[2,4],[2,5],[3,6],[4,6],[4,7],[6,8],[7,8]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[8,6,3],[7,4,2],[1,0,5]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-06-38.png" style="width: 198px; height: 133px;" /></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= edges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= u<sub>i</sub> < v<sub>i</sub> < n</code></li>
|
||||
<li>All the edges are distinct.</li>
|
||||
<li>The input is generated such that <code>edges</code> can form a 2D grid that satisfies the conditions.</li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given a string <code>word</code> and a <strong>non-negative</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Return the total number of <span data-keyword="substring-nonempty">substrings</span> of <code>word</code> that contain every vowel (<code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>) <strong>at least</strong> once and <strong>exactly</strong> <code>k</code> consonants.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "aeioqq", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There is no substring with every vowel.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "aeiou", k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only substring with every vowel and zero consonants is <code>word[0..4]</code>, which is <code>"aeiou"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "</span>ieaouqqieaouqq<span class="example-io">", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> 3</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substrings with every vowel and one consonant are:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>word[0..5]</code>, which is <code>"ieaouq"</code>.</li>
|
||||
<li><code>word[6..11]</code>, which is <code>"qieaou"</code>.</li>
|
||||
<li><code>word[7..12]</code>, which is <code>"ieaouq"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>5 <= word.length <= 250</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
<li><code>0 <= k <= word.length - 5</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given a string <code>word</code> and a <strong>non-negative</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Return the total number of <span data-keyword="substring-nonempty">substrings</span> of <code>word</code> that contain every vowel (<code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>) <strong>at least</strong> once and <strong>exactly</strong> <code>k</code> consonants.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "aeioqq", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There is no substring with every vowel.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "aeiou", k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only substring with every vowel and zero consonants is <code>word[0..4]</code>, which is <code>"aeiou"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word = "</span>ieaouqqieaouqq<span class="example-io">", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> 3</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substrings with every vowel and one consonant are:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>word[0..5]</code>, which is <code>"ieaouq"</code>.</li>
|
||||
<li><code>word[6..11]</code>, which is <code>"qieaou"</code>.</li>
|
||||
<li><code>word[7..12]</code>, which is <code>"ieaouq"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>5 <= word.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
<li><code>0 <= k <= word.length - 5</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Alice and Bob are playing a game. Initially, Alice has a string <code>word = "a"</code>.</p>
|
||||
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Now Bob will ask Alice to perform the following operation <strong>forever</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Generate a new string by <strong>changing</strong> each character in <code>word</code> to its <strong>next</strong> character in the English alphabet, and <strong>append</strong> it to the <em>original</em> <code>word</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, performing the operation on <code>"c"</code> generates <code>"cd"</code> and performing the operation on <code>"zb"</code> generates <code>"zbac"</code>.</p>
|
||||
|
||||
<p>Return the value of the <code>k<sup>th</sup></code> character in <code>word</code>, after enough operations have been done for <code>word</code> to have <strong>at least</strong> <code>k</code> characters.</p>
|
||||
|
||||
<p><strong>Note</strong> that the character <code>'z'</code> can be changed to <code>'a'</code> in the operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"b"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Initially, <code>word = "a"</code>. We need to do the operation three times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Generated string is <code>"b"</code>, <code>word</code> becomes <code>"ab"</code>.</li>
|
||||
<li>Generated string is <code>"bc"</code>, <code>word</code> becomes <code>"abbc"</code>.</li>
|
||||
<li>Generated string is <code>"bccd"</code>, <code>word</code> becomes <code>"abbcbccd"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 10</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"c"</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 500</code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>Alice and Bob are playing a game. Initially, Alice has a string <code>word = "a"</code>.</p>
|
||||
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given an integer array <code>operations</code>, where <code>operations[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> operation.</p>
|
||||
|
||||
<p>Now Bob will ask Alice to perform <strong>all</strong> operations in sequence:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>operations[i] == 0</code>, <strong>append</strong> a copy of <code>word</code> to itself.</li>
|
||||
<li>If <code>operations[i] == 1</code>, generate a new string by <strong>changing</strong> each character in <code>word</code> to its <strong>next</strong> character in the English alphabet, and <strong>append</strong> it to the <em>original</em> <code>word</code>. For example, performing the operation on <code>"c"</code> generates <code>"cd"</code> and performing the operation on <code>"zb"</code> generates <code>"zbac"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the value of the <code>k<sup>th</sup></code> character in <code>word</code> after performing all the operations.</p>
|
||||
|
||||
<p><strong>Note</strong> that the character <code>'z'</code> can be changed to <code>'a'</code> in the second type of operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 5, operations = [0,0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"a"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Initially, <code>word == "a"</code>. Alice performs the three operations as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Appends <code>"a"</code> to <code>"a"</code>, <code>word</code> becomes <code>"aa"</code>.</li>
|
||||
<li>Appends <code>"aa"</code> to <code>"aa"</code>, <code>word</code> becomes <code>"aaaa"</code>.</li>
|
||||
<li>Appends <code>"aaaa"</code> to <code>"aaaa"</code>, <code>word</code> becomes <code>"aaaaaaaa"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 10, operations = [0,1,0,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"b"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Initially, <code>word == "a"</code>. Alice performs the four operations as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Appends <code>"a"</code> to <code>"a"</code>, <code>word</code> becomes <code>"aa"</code>.</li>
|
||||
<li>Appends <code>"bb"</code> to <code>"aa"</code>, <code>word</code> becomes <code>"aabb"</code>.</li>
|
||||
<li>Appends <code>"aabb"</code> to <code>"aabb"</code>, <code>word</code> becomes <code>"aabbaabb"</code>.</li>
|
||||
<li>Appends <code>"bbccbbcc"</code> to <code>"aabbaabb"</code>, <code>word</code> becomes <code>"aabbaabbbbccbbcc"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>14</sup></code></li>
|
||||
<li><code>1 <= operations.length <= 100</code></li>
|
||||
<li><code>operations[i]</code> is either 0 or 1.</li>
|
||||
<li>The input is generated such that <code>word</code> has <strong>at least</strong> <code>k</code> characters after all operations.</li>
|
||||
</ul>
|
@@ -0,0 +1,79 @@
|
||||
<p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
|
||||
|
||||
<p>A string <code>x</code> is called <strong>almost equal</strong> to <code>y</code> if you can change <strong>at most</strong> one character in <code>x</code> to make it <em>identical</em> to <code>y</code>.</p>
|
||||
|
||||
<p>A sequence of indices <code>seq</code> is called <strong>valid</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The indices are sorted in <strong>ascending</strong> order.</li>
|
||||
<li><em>Concatenating</em> the characters at these indices in <code>word1</code> in <strong>the same</strong> order results in a string that is <strong>almost equal</strong> to <code>word2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array of size <code>word2.length</code> representing the <span data-keyword="lexicographically-smaller-array">lexicographically smallest</span> <strong>valid</strong> sequence of indices. If no such sequence of indices exists, return an <strong>empty</strong> array.</p>
|
||||
|
||||
<p><strong>Note</strong> that the answer must represent the <em>lexicographically smallest array</em>, <strong>not</strong> the corresponding string formed by those indices.<!-- notionvc: 2ff8e782-bd6f-4813-a421-ec25f7e84c1e --></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "vbcca", word2 = "abc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The lexicographically smallest valid sequence of indices is <code>[0, 1, 2]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Change <code>word1[0]</code> to <code>'a'</code>.</li>
|
||||
<li><code>word1[1]</code> is already <code>'b'</code>.</li>
|
||||
<li><code>word1[2]</code> is already <code>'c'</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "bacdc", word2 = "abc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,2,4]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The lexicographically smallest valid sequence of indices is <code>[1, 2, 4]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>word1[1]</code> is already <code>'a'</code>.</li>
|
||||
<li>Change <code>word1[2]</code> to <code>'b'</code>.</li>
|
||||
<li><code>word1[4]</code> is already <code>'c'</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "aaaaaa", word2 = "aaabc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There is no valid sequence of indices.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "abc", word2 = "ab"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word2.length < word1.length <= 3 * 10<sup>5</sup></code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given two strings <code>s</code> and <code>pattern</code>.</p>
|
||||
|
||||
<p>A string <code>x</code> is called <strong>almost equal</strong> to <code>y</code> if you can change <strong>at most</strong> one character in <code>x</code> to make it <em>identical</em> to <code>y</code>.</p>
|
||||
|
||||
<p>Return the <strong>smallest</strong> <em>starting index</em> of a <span data-keyword="substring-nonempty">substring</span> in <code>s</code> that is <strong>almost equal</strong> to <code>pattern</code>. If no such index exists, return <code>-1</code>.</p>
|
||||
A <strong>substring</strong> is a contiguous <b>non-empty</b> sequence of characters within a string.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "abcdefg", pattern = "bcdffg"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substring <code>s[1..6] == "bcdefg"</code> can be converted to <code>"bcdffg"</code> by changing <code>s[4]</code> to <code>"f"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "ababbababa", pattern = "bacaba"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substring <code>s[4..9] == "bababa"</code> can be converted to <code>"bacaba"</code> by changing <code>s[6]</code> to <code>"c"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "abcd", pattern = "dba"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "dde", pattern = "d"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pattern.length < s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> and <code>pattern</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow-up:</strong> Could you solve the problem if <strong>at most</strong> <code>k</code> <strong>consecutive</strong> characters can be changed?
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given an array <code>maximumHeight</code>, where <code>maximumHeight[i]</code> denotes the <strong>maximum</strong> height the <code>i<sup>th</sup></code> tower can be assigned.</p>
|
||||
|
||||
<p>Your task is to assign a height to each tower so that:</p>
|
||||
|
||||
<ol>
|
||||
<li>The height of the <code>i<sup>th</sup></code> tower is a positive integer and does not exceed <code>maximumHeight[i]</code>.</li>
|
||||
<li>No two towers have the same height.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible total sum of the tower heights. If it's not possible to assign heights, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> maximumHeight<span class="example-io"> = [2,3,4,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can assign heights in the following way: <code>[1, 2, 4, 3]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> maximumHeight<span class="example-io"> = [15,10]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">25</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can assign heights in the following way: <code>[15, 10]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> maximumHeight<span class="example-io"> = [2,2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It's impossible to assign positive heights to each index so that no two towers have the same height.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= maximumHeight.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= maximumHeight[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given an array of integers <code>nums</code> of size 3.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible number whose <em>binary representation</em> can be formed by <strong>concatenating</strong> the <em>binary representation</em> of <strong>all</strong> elements in <code>nums</code> in some order.</p>
|
||||
|
||||
<p><strong>Note</strong> that the binary representation of any number <em>does not</em> contain leading zeros.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> 30</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Concatenate the numbers in the order <code>[3, 1, 2]</code> to get the result <code>"11110"</code>, which is the binary representation of 30.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,16]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> 1296</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Concatenate the numbers in the order <code>[2, 8, 16]</code> to get the result <code>"10100010000"</code>, which is the binary representation of 1296.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 127</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>You replace each element in <code>nums</code> with the <strong>sum</strong> of its digits.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> element in <code>nums</code> after all replacements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [10,12,13,14]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>nums</code> becomes <code>[1, 3, 4, 5]</code> after all replacements, with minimum element 1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>nums</code> becomes <code>[1, 2, 3, 4]</code> after all replacements, with minimum element 1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [999,19,199]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>nums</code> becomes <code>[27, 10, 19]</code> after all replacements, with minimum element 10.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
65
leetcode/problem/remove-methods-from-project.html
Normal file
65
leetcode/problem/remove-methods-from-project.html
Normal file
@@ -0,0 +1,65 @@
|
||||
<p>You are maintaining a project that has <code>n</code> methods numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given two integers <code>n</code> and <code>k</code>, and a 2D integer array <code>invocations</code>, where <code>invocations[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that method <code>a<sub>i</sub></code> invokes method <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>There is a known bug in method <code>k</code>. Method <code>k</code>, along with any method invoked by it, either <strong>directly</strong> or <strong>indirectly</strong>, are considered <strong>suspicious</strong> and we aim to remove them.</p>
|
||||
|
||||
<p>A group of methods can only be removed if no method <strong>outside</strong> the group invokes any methods <strong>within</strong> it.</p>
|
||||
|
||||
<p>Return an array containing all the remaining methods after removing all the <strong>suspicious</strong> methods. You may return the answer in <em>any order</em>. If it is not possible to remove <strong>all</strong> the suspicious methods, <strong>none</strong> should be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1,2,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/18/graph-2.png" style="width: 200px; height: 200px;" /></p>
|
||||
|
||||
<p>Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,4]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/18/graph-3.png" style="width: 200px; height: 200px;" /></p>
|
||||
|
||||
<p>Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/20/graph.png" style="width: 200px; height: 200px;" /></p>
|
||||
|
||||
<p>All methods are suspicious. We can remove them.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= n - 1</code></li>
|
||||
<li><code>0 <= invocations.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>invocations[i] == [a<sub>i</sub>, b<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>invocations[i] != invocations[j]</code></li>
|
||||
</ul>
|
60
leetcode/problem/sorted-gcd-pair-queries.html
Normal file
60
leetcode/problem/sorted-gcd-pair-queries.html
Normal file
@@ -0,0 +1,60 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer array <code>queries</code>.</p>
|
||||
|
||||
<p>Let <code>gcdPairs</code> denote an array obtained by calculating the <span data-keyword="gcd-function">GCD</span> of all possible pairs <code>(nums[i], nums[j])</code>, where <code>0 <= i < j < n</code>, and then sorting these values in <strong>ascending</strong> order.</p>
|
||||
|
||||
<p>For each query <code>queries[i]</code>, you need to find the element at index <code>queries[i]</code> in <code>gcdPairs</code>.</p>
|
||||
|
||||
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the value at <code>gcdPairs[queries[i]]</code> for each query.</p>
|
||||
|
||||
<p>The term <code>gcd(a, b)</code> denotes the <strong>greatest common divisor</strong> of <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4], queries = [0,2,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,2,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>gcdPairs = [gcd(nums[0], nums[1]), gcd(nums[0], nums[2]), gcd(nums[1], nums[2])] = [1, 2, 1]</code>.</p>
|
||||
|
||||
<p>After sorting in ascending order, <code>gcdPairs = [1, 1, 2]</code>.</p>
|
||||
|
||||
<p>So, the answer is <code>[gcdPairs[queries[0]], gcdPairs[queries[1]], gcdPairs[queries[2]]] = [1, 2, 2]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,2,1], queries = [5,3,1,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[4,2,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>gcdPairs</code> sorted in ascending order is <code>[1, 1, 1, 2, 2, 4]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,2], queries = [0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>gcdPairs = [2]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= queries[i] < n * (n - 1) / 2</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user