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,41 @@
|
||||
<p>You are given a binary string <code>s</code>. In one second, <strong>all</strong> occurrences of <code>"01"</code> are <strong>simultaneously</strong> replaced with <code>"10"</code>. This process <strong>repeats</strong> until no occurrences of <code>"01"</code> exist.</p>
|
||||
|
||||
<p>Return<em> the number of seconds needed to complete this process.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0110101"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
After one second, s becomes "1011010".
|
||||
After another second, s becomes "1101100".
|
||||
After the third second, s becomes "1110100".
|
||||
After the fourth second, s becomes "1111000".
|
||||
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
|
||||
so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
|
||||
so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong></p>
|
||||
|
||||
<p>Can you solve this problem in O(n) time complexity?</p>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>tasks</code>, representing tasks that need to be completed <strong>in order</strong>, where <code>tasks[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> task.</p>
|
||||
|
||||
<p>You are also given a positive integer <code>space</code>, which represents the <strong>minimum</strong> number of days that must pass <strong>after</strong> the completion of a task before another task of the <strong>same</strong> type can be performed.</p>
|
||||
|
||||
<p>Each day, until all tasks have been completed, you must either:</p>
|
||||
|
||||
<ul>
|
||||
<li>Complete the next task from <code>tasks</code>, or</li>
|
||||
<li>Take a break.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of days needed to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [1,2,1,2,3,1], space = 3
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 9 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
Day 7: Take a break.
|
||||
Day 8: Complete the 4th task.
|
||||
Day 9: Complete the 5th task.
|
||||
It can be shown that the tasks cannot be completed in less than 9 days.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [5,8,8,5], space = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 6 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
It can be shown that the tasks cannot be completed in less than 6 days.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= tasks.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given a non-negative integer array <code>nums</code>. In one operation, you must:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a positive integer <code>x</code> such that <code>x</code> is less than or equal to the <strong>smallest non-zero</strong> element in <code>nums</code>.</li>
|
||||
<li>Subtract <code>x</code> from every <strong>positive</strong> element in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make every element in </em><code>nums</code><em> equal to </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,0,3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
In the first operation, choose x = 1. Now, nums = [0,4,0,2,4].
|
||||
In the second operation, choose x = 2. Now, nums = [0,2,0,0,2].
|
||||
In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each element in nums is already 0 so no operations are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given a positive integer array <code>grades</code> which represents the grades of students in a university. You would like to enter <strong>all</strong> these students into a competition in <strong>ordered</strong> non-empty groups, such that the ordering meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The sum of the grades of students in the <code>i<sup>th</sup></code> group is <strong>less than</strong> the sum of the grades of students in the <code>(i + 1)<sup>th</sup></code> group, for all groups (except the last).</li>
|
||||
<li>The total number of students in the <code>i<sup>th</sup></code> group is <strong>less than</strong> the total number of students in the <code>(i + 1)<sup>th</sup></code> group, for all groups (except the last).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of groups that can be formed</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grades = [10,6,12,7,3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following is a possible way to form 3 groups of students:
|
||||
- 1<sup>st</sup> group has the students with grades = [12]. Sum of grades: 12. Student count: 1
|
||||
- 2<sup>nd</sup> group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
|
||||
- 3<sup>rd</sup> group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
|
||||
It can be shown that it is not possible to form more than 3 groups.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grades = [8,8]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grades.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grades[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>removeQueries</code>, both of length <code>n</code>. For the <code>i<sup>th</sup></code> query, the element in <code>nums</code> at the index <code>removeQueries[i]</code> is removed, splitting <code>nums</code> into different segments.</p>
|
||||
|
||||
<p>A <strong>segment</strong> is a contiguous sequence of <strong>positive</strong> integers in <code>nums</code>. A <strong>segment sum</strong> is the sum of every element in a segment.</p>
|
||||
|
||||
<p>Return<em> an integer array </em><code>answer</code><em>, of length </em><code>n</code><em>, where </em><code>answer[i]</code><em> is the <strong>maximum</strong> segment sum after applying the </em><code>i<sup>th</sup></code> <em>removal.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> The same index will <strong>not</strong> be removed more than once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]
|
||||
<strong>Output:</strong> [14,7,2,2,0]
|
||||
<strong>Explanation:</strong> Using 0 to indicate a removed element, the answer is as follows:
|
||||
Query 1: Remove the 0th element, nums becomes [0,2,5,6,1] and the maximum segment sum is 14 for segment [2,5,6,1].
|
||||
Query 2: Remove the 3rd element, nums becomes [0,2,5,0,1] and the maximum segment sum is 7 for segment [2,5].
|
||||
Query 3: Remove the 2nd element, nums becomes [0,2,0,0,1] and the maximum segment sum is 2 for segment [2].
|
||||
Query 4: Remove the 4th element, nums becomes [0,2,0,0,0] and the maximum segment sum is 2 for segment [2].
|
||||
Query 5: Remove the 1st element, nums becomes [0,0,0,0,0] and the maximum segment sum is 0, since there are no segments.
|
||||
Finally, we return [14,7,2,2,0].</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,11,1], removeQueries = [3,2,1,0]
|
||||
<strong>Output:</strong> [16,5,3,0]
|
||||
<strong>Explanation:</strong> Using 0 to indicate a removed element, the answer is as follows:
|
||||
Query 1: Remove the 3rd element, nums becomes [3,2,11,0] and the maximum segment sum is 16 for segment [3,2,11].
|
||||
Query 2: Remove the 2nd element, nums becomes [3,2,0,0] and the maximum segment sum is 5 for segment [3,2].
|
||||
Query 3: Remove the 1st element, nums becomes [3,0,0,0] and the maximum segment sum is 3 for segment [3].
|
||||
Query 4: Remove the 0th element, nums becomes [0,0,0,0] and the maximum segment sum is 0, since there are no segments.
|
||||
Finally, we return [16,5,3,0].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == removeQueries.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= removeQueries[i] < n</code></li>
|
||||
<li>All the values of <code>removeQueries</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>edges</code> of length <code>n - 1</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> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
|
||||
|
||||
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The diagram above shows the tree.
|
||||
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The diagram above shows the tree.
|
||||
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= 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>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>1 <= restricted.length < n</code></li>
|
||||
<li><code>1 <= restricted[i] < n</code></li>
|
||||
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given two 2D integer arrays, <code>items1</code> and <code>items2</code>, representing two sets of items. Each array <code>items</code> has the following properties:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>items[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code> where <code>value<sub>i</sub></code> represents the <strong>value</strong> and <code>weight<sub>i</sub></code> represents the <strong>weight </strong>of the <code>i<sup>th</sup></code> item.</li>
|
||||
<li>The value of each item in <code>items</code> is <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a 2D integer array</em> <code>ret</code> <em>where</em> <code>ret[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code><em>,</em> <em>with</em> <code>weight<sub>i</sub></code> <em>being the <strong>sum of weights</strong> of all items with value</em> <code>value<sub>i</sub></code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> <code>ret</code> should be returned in <strong>ascending</strong> order by value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
|
||||
<strong>Output:</strong> [[1,6],[3,9],[4,5]]
|
||||
<strong>Explanation:</strong>
|
||||
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.
|
||||
The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.
|
||||
The item with value = 4 occurs in items1 with weight = 5, total weight = 5.
|
||||
Therefore, we return [[1,6],[3,9],[4,5]].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
|
||||
<strong>Output:</strong> [[1,4],[2,4],[3,4]]
|
||||
<strong>Explanation:</strong>
|
||||
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.
|
||||
The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.
|
||||
The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
|
||||
Therefore, we return [[1,4],[2,4],[3,4]].</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
|
||||
<strong>Output:</strong> [[1,7],[2,4],[7,1]]
|
||||
<strong>Explanation:
|
||||
</strong>The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7.
|
||||
The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
|
||||
The item with value = 7 occurs in items2 with weight = 1, total weight = 1.
|
||||
Therefore, we return [[1,7],[2,4],[7,1]].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= items1.length, items2.length <= 1000</code></li>
|
||||
<li><code>items1[i].length == items2[i].length == 2</code></li>
|
||||
<li><code>1 <= value<sub>i</sub>, weight<sub>i</sub> <= 1000</code></li>
|
||||
<li>Each <code>value<sub>i</sub></code> in <code>items1</code> is <strong>unique</strong>.</li>
|
||||
<li>Each <code>value<sub>i</sub></code> in <code>items2</code> is <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
|
||||
|
||||
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from node <code>i</code>, then <code>edges[i] == -1</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>
|
||||
|
||||
<p>A cycle is a path that starts and ends at the <strong>same</strong> node.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/08/graph4drawio-5.png" style="width: 335px; height: 191px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [3,3,4,2,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
|
||||
The length of this cycle is 3, so 3 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-1.png" style="width: 171px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [2,-1,3,1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There are no cycles in this graph.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-1 <= edges[i] < n</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given a string <code>s</code> of lowercase English letters and a 2D integer array <code>shifts</code> where <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code>. For every <code>i</code>, <strong>shift</strong> the characters in <code>s</code> from the index <code>start<sub>i</sub></code> to the index <code>end<sub>i</sub></code> (<strong>inclusive</strong>) forward if <code>direction<sub>i</sub> = 1</code>, or shift the characters backward if <code>direction<sub>i</sub> = 0</code>.</p>
|
||||
|
||||
<p>Shifting a character <strong>forward</strong> means replacing it with the <strong>next</strong> letter in the alphabet (wrapping around so that <code>'z'</code> becomes <code>'a'</code>). Similarly, shifting a character <strong>backward</strong> means replacing it with the <strong>previous</strong> letter in the alphabet (wrapping around so that <code>'a'</code> becomes <code>'z'</code>).</p>
|
||||
|
||||
<p>Return <em>the final string after all such shifts to </em><code>s</code><em> are applied</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
|
||||
<strong>Output:</strong> "ace"
|
||||
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 1 backward. Now s = "zac".
|
||||
Secondly, shift the characters from index 1 to index 2 forward. Now s = "zbd".
|
||||
Finally, shift the characters from index 0 to index 2 forward. Now s = "ace".</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "dztz", shifts = [[0,0,0],[1,1,1]]
|
||||
<strong>Output:</strong> "catz"
|
||||
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 0 backward. Now s = "cztz".
|
||||
Finally, shift the characters from index 1 to index 1 forward. Now s = "catz".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, shifts.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>shifts[i].length == 3</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < s.length</code></li>
|
||||
<li><code>0 <= direction<sub>i</sub> <= 1</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,9,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
|
||||
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
|
||||
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
|
||||
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>blocks</code> of length <code>n</code>, where <code>blocks[i]</code> is either <code>'W'</code> or <code>'B'</code>, representing the color of the <code>i<sup>th</sup></code> block. The characters <code>'W'</code> and <code>'B'</code> denote the colors white and black, respectively.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>, which is the desired number of <strong>consecutive</strong> black blocks.</p>
|
||||
|
||||
<p>In one operation, you can <strong>recolor</strong> a white block such that it becomes a black block.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of operations needed such that there is at least <strong>one</strong> occurrence of </em><code>k</code><em> consecutive black blocks.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> blocks = "WBBWWBBWBW", k = 7
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks
|
||||
so that blocks = "BBBBBBBWBW".
|
||||
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
|
||||
Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> blocks = "WBWBBBW", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No changes need to be made, since 2 consecutive black blocks already exist.
|
||||
Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == blocks.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>blocks[i]</code> is either <code>'W'</code> or <code>'B'</code>.</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
|
||||
|
||||
<p>Each minute, a node becomes infected if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The node is currently uninfected.</li>
|
||||
<li>The node is adjacent to an infected node.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The following nodes are infected during:
|
||||
- Minute 0: Node 3
|
||||
- Minute 1: Nodes 1, 10 and 6
|
||||
- Minute 2: Node 5
|
||||
- Minute 3: Node 4
|
||||
- Minute 4: Nodes 9 and 2
|
||||
It takes 4 minutes for the whole tree to be infected so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1], start = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>Each node has a <strong>unique</strong> value.</li>
|
||||
<li>A node with a value of <code>start</code> exists in the tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. You can choose any <strong>subsequence</strong> of the array and sum all of its elements together.</p>
|
||||
|
||||
<p>We define the <strong>K-Sum</strong> of the array as the <code>k<sup>th</sup></code> <strong>largest</strong> subsequence sum that can be obtained (<strong>not</strong> necessarily distinct).</p>
|
||||
|
||||
<p>Return <em>the K-Sum of the array</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p><strong>Note</strong> that the empty subsequence is considered to have a sum of <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,-2], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> All the possible subsequence sums that we can obtain are the following sorted in decreasing order:
|
||||
- 6, 4, 4, 2, <u>2</u>, 0, 0, -2.
|
||||
The 5-Sum of the array is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-2,3,4,-10,12], k = 16
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The 16-Sum of the array is 10.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= min(2000, 2<sup>n</sup>)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
|
||||
|
||||
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from <code>i</code>, then <code>edges[i] == -1</code>.</p>
|
||||
|
||||
<p>You are also given two integers <code>node1</code> and <code>node2</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>index</strong> of the node that can be reached from both </em><code>node1</code><em> and </em><code>node2</code><em>, such that the <strong>maximum</strong> between the distance from </em><code>node1</code><em> to that node, and from </em><code>node2</code><em> to that node is <strong>minimized</strong></em>. If there are multiple answers, return the node with the <strong>smallest</strong> index, and if no possible answer exists, return <code>-1</code>.</p>
|
||||
|
||||
<p>Note that <code>edges</code> may contain cycles.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png" style="width: 321px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [2,2,3,-1], node1 = 0, node2 = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
|
||||
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png" style="width: 195px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [1,2,-1], node1 = 0, node2 = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
|
||||
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-1 <= edges[i] < n</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
<li><code>0 <= node1, node2 < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a string <code>num</code> consisting of digits only.</p>
|
||||
|
||||
<p>Return <em>the <strong>largest palindromic</strong> integer (in the form of a string) that can be formed using digits taken from </em><code>num</code>. It should not contain <strong>leading zeroes</strong>.</p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>You do <strong>not</strong> need to use all the digits of <code>num</code>, but you must use <strong>at least</strong> one digit.</li>
|
||||
<li>The digits can be reordered.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "444947137"
|
||||
<strong>Output:</strong> "7449447"
|
||||
<strong>Explanation:</strong>
|
||||
Use the digits "4449477" from "<u><strong>44494</strong></u><u><strong>7</strong></u>13<u><strong>7</strong></u>" to form the palindromic integer "7449447".
|
||||
It can be shown that "7449447" is the largest palindromic integer that can be formed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "00009"
|
||||
<strong>Output:</strong> "9"
|
||||
<strong>Explanation:</strong>
|
||||
It can be shown that "9" is the largest palindromic integer that can be formed.
|
||||
Note that the integer returned should not contain leading zeroes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>num</code> consists of digits.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
|
||||
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
|
||||
|
||||
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "acfgbd", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
|
||||
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd", k = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 25</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>pattern</code> of length <code>n</code> consisting of the characters <code>'I'</code> meaning <strong>increasing</strong> and <code>'D'</code> meaning <strong>decreasing</strong>.</p>
|
||||
|
||||
<p>A <strong>0-indexed</strong> string <code>num</code> of length <code>n + 1</code> is created using the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num</code> consists of the digits <code>'1'</code> to <code>'9'</code>, where each digit is used <strong>at most</strong> once.</li>
|
||||
<li>If <code>pattern[i] == 'I'</code>, then <code>num[i] < num[i + 1]</code>.</li>
|
||||
<li>If <code>pattern[i] == 'D'</code>, then <code>num[i] > num[i + 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the lexicographically <strong>smallest</strong> possible string </em><code>num</code><em> that meets the conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pattern = "IIIDIDDD"
|
||||
<strong>Output:</strong> "123549876"
|
||||
<strong>Explanation:
|
||||
</strong>At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
|
||||
At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
|
||||
Some possible values of num are "245639871", "135749862", and "123849765".
|
||||
It can be proven that "123549876" is the smallest possible num that meets the conditions.
|
||||
Note that "123414321" is not possible because the digit '1' is used more than once.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> pattern = "DDD"
|
||||
<strong>Output:</strong> "4321"
|
||||
<strong>Explanation:</strong>
|
||||
Some possible values of num are "9876", "7321", and "8742".
|
||||
It can be proven that "4321" is the smallest possible num that meets the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pattern.length <= 8</code></li>
|
||||
<li><code>pattern</code> consists of only the letters <code>'I'</code> and <code>'D'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
|
||||
|
||||
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
|
||||
|
||||
<ol>
|
||||
<li>The subarray consists of <strong>exactly</strong> <code>2</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
|
||||
<li>The subarray consists of <strong>exactly</strong> <code>3</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
|
||||
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4,5,6]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
|
||||
This partition is valid, so we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> There is no valid partition for this array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
|
||||
|
||||
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
|
||||
|
||||
<p>Return <em>the generated matrix</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
|
||||
<strong>Output:</strong> [[9,9],[8,6]]
|
||||
<strong>Explanation:</strong> The diagram above shows the original matrix and the generated matrix.
|
||||
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
|
||||
<strong>Output:</strong> [[2,2,2],[2,2,2],[2,2,2]]
|
||||
<strong>Explanation:</strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong>, <strong>strictly increasing</strong> integer array <code>nums</code> and a positive integer <code>diff</code>. A triplet <code>(i, j, k)</code> is an <strong>arithmetic triplet</strong> if the following conditions are met:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code>,</li>
|
||||
<li><code>nums[j] - nums[i] == diff</code>, and</li>
|
||||
<li><code>nums[k] - nums[j] == diff</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of unique <strong>arithmetic triplets</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,4,6,7,10], diff = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
(1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
|
||||
(2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,6,7,8,9], diff = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
|
||||
(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 200</code></li>
|
||||
<li><code>0 <= nums[i] <= 200</code></li>
|
||||
<li><code>1 <= diff <= 50</code></li>
|
||||
<li><code>nums</code> is <strong>strictly</strong> increasing.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of indices <code>(i, j)</code> is a <strong>bad pair</strong> if <code>i < j</code> and <code>j - i != nums[j] - nums[i]</code>.</p>
|
||||
|
||||
<p>Return<em> the total number of <strong>bad pairs</strong> in </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,3,3]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
|
||||
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
|
||||
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
|
||||
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
|
||||
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
|
||||
There are a total of 5 bad pairs, so we return 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no bad pairs.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>We call a positive integer <strong>special</strong> if all of its digits are <strong>distinct</strong>.</p>
|
||||
|
||||
<p>Given a <strong>positive</strong> integer <code>n</code>, return <em>the number of special integers that belong to the interval </em><code>[1, n]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 20
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> All the integers from 1 to 5 are special.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 135
|
||||
<strong>Output:</strong> 110
|
||||
<strong>Explanation:</strong> There are 110 integers from 1 to 135 that are special.
|
||||
Some of the integers that are not special are: 22, 114, and 131.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are entering a competition, and are given two <strong>positive</strong> integers <code>initialEnergy</code> and <code>initialExperience</code> denoting your initial energy and initial experience respectively.</p>
|
||||
|
||||
<p>You are also given two <strong>0-indexed</strong> integer arrays <code>energy</code> and <code>experience</code>, both of length <code>n</code>.</p>
|
||||
|
||||
<p>You will face <code>n</code> opponents <strong>in order</strong>. The energy and experience of the <code>i<sup>th</sup></code> opponent is denoted by <code>energy[i]</code> and <code>experience[i]</code> respectively. When you face an opponent, you need to have both <strong>strictly</strong> greater experience and energy to defeat them and move to the next opponent if available.</p>
|
||||
|
||||
<p>Defeating the <code>i<sup>th</sup></code> opponent <strong>increases</strong> your experience by <code>experience[i]</code>, but <strong>decreases</strong> your energy by <code>energy[i]</code>.</p>
|
||||
|
||||
<p>Before starting the competition, you can train for some number of hours. After each hour of training, you can <strong>either</strong> choose to increase your initial experience by one, or increase your initial energy by one.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of training hours required to defeat all </em><code>n</code><em> opponents</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
|
||||
You face the opponents in the following order:
|
||||
- You have more energy and experience than the 0<sup>th</sup> opponent so you win.
|
||||
Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.
|
||||
- You have more energy and experience than the 1<sup>st</sup> opponent so you win.
|
||||
Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.
|
||||
- You have more energy and experience than the 2<sup>nd</sup> opponent so you win.
|
||||
Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.
|
||||
- You have more energy and experience than the 3<sup>rd</sup> opponent so you win.
|
||||
Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.
|
||||
You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
|
||||
It can be proven that no smaller answer exists.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You do not need any additional energy or experience to win the competition, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == energy.length == experience.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a directed graph with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, where each node has <strong>exactly one</strong> outgoing edge.</p>
|
||||
|
||||
<p>The graph is represented by a given <strong>0-indexed</strong> integer array <code>edges</code> of length <code>n</code>, where <code>edges[i]</code> indicates that there is a <strong>directed</strong> edge from node <code>i</code> to node <code>edges[i]</code>.</p>
|
||||
|
||||
<p>The <strong>edge score</strong> of a node <code>i</code> is defined as the sum of the <strong>labels</strong> of all the nodes that have an edge pointing to <code>i</code>.</p>
|
||||
|
||||
<p>Return <em>the node with the highest <strong>edge score</strong></em>. If multiple nodes have the same <strong>edge score</strong>, return the node with the <strong>smallest</strong> index.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png" style="width: 450px; height: 260px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [1,0,0,0,0,7,7,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong>
|
||||
- The nodes 1, 2, 3 and 4 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 + 3 + 4 = 10.
|
||||
- The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.
|
||||
- The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.
|
||||
- The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.
|
||||
Node 7 has the highest edge score so return 7.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png" style="width: 150px; height: 155px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [2,0,0,2]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
- The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.
|
||||
- The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.
|
||||
Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges[i] < n</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user