mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>. Initially, you have an array <code>nums = [1]</code>.</p>
|
||||
|
||||
<p>You can perform <strong>any</strong> of the following operations on the array <strong>any</strong> number of times (<strong>possibly zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any element in the array and <strong>increase</strong> its value by <code>1</code>.</li>
|
||||
<li>Duplicate any element in the array and add it to the end of the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make the <strong>sum</strong> of elements of the final array greater than or equal to </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 11</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can do the following operations on the array <code>nums = [1]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase the element by <code>1</code> three times. The resulting array is <code>nums = [4]</code>.</li>
|
||||
<li>Duplicate the element two times. The resulting array is <code>nums = [4,4,4]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The sum of the final array is <code>4 + 4 + 4 = 12</code> which is greater than or equal to <code>k = 11</code>.<br />
|
||||
The total number of operations performed is <code>3 + 2 = 5</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of the original array is already greater than or equal to <code>1</code>, so no operations are needed.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given two arrays of strings <code>wordsContainer</code> and <code>wordsQuery</code>.</p>
|
||||
|
||||
<p>For each <code>wordsQuery[i]</code>, you need to find a string from <code>wordsContainer</code> that has the <strong>longest common suffix</strong> with <code>wordsQuery[i]</code>. If there are two or more strings in <code>wordsContainer</code> that share the longest common suffix, find the string that is the <strong>smallest</strong> in length. If there are two or more such strings that have the <strong>same</strong> smallest length, find the one that occurred <strong>earlier</strong> in <code>wordsContainer</code>.</p>
|
||||
|
||||
<p>Return <em>an array of integers </em><code>ans</code><em>, where </em><code>ans[i]</code><em> is the index of the string in </em><code>wordsContainer</code><em> that has the <strong>longest common suffix</strong> with </em><code>wordsQuery[i]</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "cd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"cd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[1] = "bcd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"bcd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[2] = "xyz"</code>, there is no string from <code>wordsContainer</code> that shares a common suffix. Hence the longest common suffix is <code>""</code>, that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "gh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
<li>For <code>wordsQuery[1] = "acbfgh"</code>, only the string at index 0 shares the longest common suffix <code>"fgh"</code>. Hence it is the answer, even though the string at index 2 is shorter.</li>
|
||||
<li>For <code>wordsQuery[2] = "acbfegh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordsContainer.length, wordsQuery.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= wordsContainer[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= wordsQuery[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>wordsContainer[i]</code> consists only of lowercase English letters.</li>
|
||||
<li><code>wordsQuery[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>Sum of <code>wordsContainer[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
<li>Sum of <code>wordsQuery[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, <code>nums</code> and <code>freq</code>, of equal length <code>n</code>. Each element in <code>nums</code> represents an ID, and the corresponding element in <code>freq</code> indicates how many times that ID should be added to or removed from the collection at each step.</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Addition of IDs:</strong> If <code>freq[i]</code> is positive, it means <code>freq[i]</code> IDs with the value <code>nums[i]</code> are added to the collection at step <code>i</code>.</li>
|
||||
<li><strong>Removal of IDs:</strong> If <code>freq[i]</code> is negative, it means <code>-freq[i]</code> IDs with the value <code>nums[i]</code> are removed from the collection at step <code>i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array <code>ans</code> of length <code>n</code>, where <code>ans[i]</code> represents the <strong>count</strong> of the <em>most frequent ID</em> in the collection after the <code>i<sup>th</sup></code> step. If the collection is empty at any step, <code>ans[i]</code> should be 0 for that step.</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,2,1], freq = [3,2,-3,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,3,2,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>After step 0, we have 3 IDs with the value of 2. So <code>ans[0] = 3</code>.<br />
|
||||
After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So <code>ans[1] = 3</code>.<br />
|
||||
After step 2, we have 2 IDs with the value of 3. So <code>ans[2] = 2</code>.<br />
|
||||
After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So <code>ans[3] = 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 = [5,5,3], freq = [2,-2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>After step 0, we have 2 IDs with the value of 5. So <code>ans[0] = 2</code>.<br />
|
||||
After step 1, there are no IDs. So <code>ans[1] = 0</code>.<br />
|
||||
After step 2, we have 1 ID with the value of 3. So <code>ans[2] = 1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == freq.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= freq[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>freq[i] != 0</code></li>
|
||||
<li>The input is generated<!-- notionvc: a136b55a-f319-4fa6-9247-11be9f3b1db8 --> such that the occurrences of an ID will not be negative in any step.</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
Given a string <code>s</code>, return the <strong>maximum</strong> length of a <span data-keyword="substring">substring</span> such that it contains <em>at most two occurrences</em> of each character.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "bcbbbcba"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
The following substring has a length of 4 and contains at most two occurrences of each character: <code>"bcbb<u>bcba</u>"</code>.</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "aaaa"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
The following substring has a length of 2 and contains at most two occurrences of each character: <code>"<u>aa</u>aa"</code>.</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user