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 an integer array <code>nums</code> and two positive integers <code>m</code> and <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum sum</strong> out of all <strong>almost unique</strong> subarrays of length </em><code>k</code><em> of</em> <code>nums</code>. If no such subarray exists, return <code>0</code>.</p>
|
||||
|
||||
<p>A subarray of <code>nums</code> is <strong>almost unique</strong> if it contains at least <code>m</code> distinct elements.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,6,7,3,1,7], m = 3, k = 4
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> There are 3 almost unique subarrays of size <code>k = 4</code>. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,9,9,2,4,5,4], m = 1, k = 3
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong> There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1,2,1], m = 3, k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no subarrays of size <code>k = 3</code> that contain at least <code>m = 3</code> distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m <= k <= nums.length</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>4</code>, consisting of <strong>lowercase</strong> English letters.</p>
|
||||
|
||||
<p>You can apply the following operation on any of the two strings <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>j - i = 2</code>, then <strong>swap</strong> the two characters at those indices in the string.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abcd", s2 = "cdab"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can do the following operations on s1:
|
||||
- Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad".
|
||||
- Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abcd", s2 = "dacb"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to make the two strings equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>s1.length == s2.length == 4</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>n</code>, consisting of <strong>lowercase</strong> English letters.</p>
|
||||
|
||||
<p>You can apply the following operation on <strong>any</strong> of the two strings <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>i < j</code> and the difference <code>j - i</code> is <strong>even</strong>, then <strong>swap</strong> the two characters at those indices in the string.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abcdba", s2 = "cabdab"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can apply the following operations on s1:
|
||||
- Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba".
|
||||
- Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa".
|
||||
- Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abe", s2 = "bea"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to make the two strings equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s1.length == s2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>Given a function <code>fn</code>, an array of arguments <code>args</code>, and a timeout <code>t</code> in milliseconds, return a cancel function <code>cancelFn</code>.</p>
|
||||
|
||||
<p>After a delay of <code>t</code>, <code>fn</code> should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code> ms. In that case, <code>fn</code> should never be called.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelT = 50
|
||||
<strong>Output:</strong> [{"time": 20, "returned": 10}]
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) called at t=20ms
|
||||
setTimeout(cancel, 50);
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelT = 50
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x) => x**2, [2], 100); // fn(2) not called
|
||||
setTimeout(cancel, 50);
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100
|
||||
<strong>Output:</strong> [{"time": 30, "returned": 8}]
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
|
||||
setTimeout(cancel, 100);
|
||||
|
||||
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>fn is a function</code></li>
|
||||
<li><code>args is a valid JSON array</code></li>
|
||||
<li><code>1 <= args.length <= 10</code></li>
|
||||
<li><code><font face="monospace">20 <= t <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">10 <= cancelT <= 1000</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>num</code> representing a non-negative integer.</p>
|
||||
|
||||
<p>In one operation, you can pick any digit of <code>num</code> and delete it. Note that if you delete all the digits of <code>num</code>, <code>num</code> becomes <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> required to make</em> <code>num</code> <i>special</i>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is considered <strong>special</strong> if it is divisible by <code>25</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "2245047"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25.
|
||||
It can be shown that 2 is the minimum number of operations required to get a special number.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "2908305"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25.
|
||||
It can be shown that 3 is the minimum number of operations required to get a special number.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "10"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25.
|
||||
It can be shown that 1 is the minimum number of operations required to get a special number.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 100</code></li>
|
||||
<li><code>num</code> only consists of digits <code>'0'</code> through <code>'9'</code>.</li>
|
||||
<li><code>num</code> does not contain any leading zeros.</li>
|
||||
</ul>
|
@@ -0,0 +1,70 @@
|
||||
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A <strong>k-subsequence</strong> is a <strong>subsequence</strong> of <code>s</code>, having length <code>k</code>, and all its characters are <strong>unique</strong>, <strong>i.e</strong>., every character occurs once.</p>
|
||||
|
||||
<p>Let <code>f(c)</code> denote the number of times the character <code>c</code> occurs in <code>s</code>.</p>
|
||||
|
||||
<p>The <strong>beauty</strong> of a <strong>k-subsequence</strong> is the <strong>sum</strong> of <code>f(c)</code> for every character <code>c</code> in the k-subsequence.</p>
|
||||
|
||||
<p>For example, consider <code>s = "abbbdd"</code> and <code>k = 2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>f('a') = 1</code>, <code>f('b') = 3</code>, <code>f('d') = 2</code></li>
|
||||
<li>Some k-subsequences of <code>s</code> are:
|
||||
<ul>
|
||||
<li><code>"<u><strong>ab</strong></u>bbdd"</code> -> <code>"ab"</code> having a beauty of <code>f('a') + f('b') = 4</code></li>
|
||||
<li><code>"<u><strong>a</strong></u>bbb<strong><u>d</u></strong>d"</code> -> <code>"ad"</code> having a beauty of <code>f('a') + f('d') = 3</code></li>
|
||||
<li><code>"a<strong><u>b</u></strong>bb<u><strong>d</strong></u>d"</code> -> <code>"bd"</code> having a beauty of <code>f('b') + f('d') = 5</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the number of k-subsequences </em><em>whose <strong>beauty</strong> is the <strong>maximum</strong> among all <strong>k-subsequences</strong></em>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.</p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>f(c)</code> is the number of times a character <code>c</code> occurs in <code>s</code>, not a k-subsequence.</li>
|
||||
<li>Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bcca", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> <span style="white-space: normal">From s we have f('a') = 1, f('b') = 1, and f('c') = 2.</span>
|
||||
The k-subsequences of s are:
|
||||
<strong><u>bc</u></strong>ca having a beauty of f('b') + f('c') = 3
|
||||
<strong><u>b</u></strong>c<u><strong>c</strong></u>a having a beauty of f('b') + f('c') = 3
|
||||
<strong><u>b</u></strong>cc<strong><u>a</u></strong> having a beauty of f('b') + f('a') = 2
|
||||
b<strong><u>c</u></strong>c<u><strong>a</strong></u><strong> </strong>having a beauty of f('c') + f('a') = 3
|
||||
bc<strong><u>ca</u></strong> having a beauty of f('c') + f('a') = 3
|
||||
There are 4 k-subsequences that have the maximum beauty, 3.
|
||||
Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbcd", k = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1.
|
||||
The k-subsequences of s are:
|
||||
<u><strong>ab</strong></u>b<strong><u>cd</u></strong> having a beauty of f('a') + f('b') + f('c') + f('d') = 5
|
||||
<u style="white-space: normal;"><strong>a</strong></u>b<u><strong>bcd</strong></u> having a beauty of f('a') + f('b') + f('c') + f('d') = 5
|
||||
There are 2 k-subsequences that have the maximum beauty, 5.
|
||||
Hence, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= s.length</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>You are given two positive integers <code>low</code> and <code>high</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> consisting of <code>2 * n</code> digits is <strong>symmetric</strong> if the sum of the first <code>n</code> digits of <code>x</code> is equal to the sum of the last <code>n</code> digits of <code>x</code>. Numbers with an odd number of digits are never symmetric.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of symmetric</strong> integers in the range</em> <code>[low, high]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 1, high = 100
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 1200, high = 1230
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, an integer <code>modulo</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Your task is to find the count of subarrays that are <strong>interesting</strong>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> <code>nums[l..r]</code> is <strong>interesting</strong> if the following condition holds:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>cnt</code> be the number of indices <code>i</code> in the range <code>[l, r]</code> such that <code>nums[i] % modulo == k</code>. Then, <code>cnt % modulo == k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the count of interesting subarrays. </em></p>
|
||||
|
||||
<p><span><strong>Note:</strong> A subarray is <em>a contiguous non-empty sequence of elements within an array</em>.</span></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,4], modulo = 2, k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example the interesting subarrays are:
|
||||
The subarray nums[0..0] which is [3].
|
||||
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
|
||||
- Hence, cnt = 1 and cnt % modulo == k.
|
||||
The subarray nums[0..1] which is [3,2].
|
||||
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
|
||||
- Hence, cnt = 1 and cnt % modulo == k.
|
||||
The subarray nums[0..2] which is [3,2,4].
|
||||
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
|
||||
- Hence, cnt = 1 and cnt % modulo == k.
|
||||
It can be shown that there are no other interesting subarrays. So, the answer is 3.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,9,6], modulo = 3, k = 0
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>In this example the interesting subarrays are:
|
||||
The subarray nums[0..3] which is [3,1,9,6].
|
||||
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
|
||||
- Hence, cnt = 3 and cnt % modulo == k.
|
||||
The subarray nums[1..1] which is [1].
|
||||
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
|
||||
- Hence, cnt = 0 and cnt % modulo == k.
|
||||
It can be shown that there are no other interesting subarrays. So, the answer is 2.</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>
|
||||
<li><code>1 <= modulo <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k < modulo</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, find the <strong>minimum number of operations</strong> required to make the weight of every edge on the path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> equal. In one operation, you can choose any edge of the tree and change its weight to any value.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Queries are <strong>independent</strong> of each other, meaning that the tree returns to its <strong>initial state</strong> on each new query.</li>
|
||||
<li>The path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> is a sequence of <strong>distinct</strong> nodes starting with node <code>a<sub>i</sub></code> and ending with node <code>b<sub>i</sub></code> such that every two adjacent nodes in the sequence share an edge in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
|
||||
<strong>Output:</strong> [0,0,1,3]
|
||||
<strong>Explanation:</strong> In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0.
|
||||
In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0.
|
||||
In the third query, we change the weight of edge [2,3] to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1.
|
||||
In the fourth query, we change the weights of edges [0,1], [1,2] and [2,3] to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3.
|
||||
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
|
||||
<strong>Output:</strong> [1,2,2,3]
|
||||
<strong>Explanation:</strong> In the first query, we change the weight of edge [1,3] to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1.
|
||||
In the second query, we change the weight of edges [0,3] and [3,1] to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2.
|
||||
In the third query, we change the weight of edges [1,3] and [5,2] to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2.
|
||||
In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3.
|
||||
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= 26</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
<li><code>1 <= queries.length == m <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user