1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-06 07:51:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-04-24 21:29:06 +08:00
parent 3a5ef731a2
commit d48ef04fb6
56 changed files with 16204 additions and 9836 deletions

View File

@@ -0,0 +1,57 @@
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation">permutation</span></strong> of the numbers in the range <code>[1, n]</code>.</p>
<p>A <strong>XOR triplet</strong> is defined as the XOR of three elements <code>nums[i] XOR nums[j] XOR nums[k]</code> where <code>i &lt;= j &lt;= k</code>.</p>
<p>Return the number of <strong>unique</strong> XOR triplet values from all possible triplets <code>(i, j, k)</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The possible XOR triplet values are:</p>
<ul>
<li><code>(0, 0, 0) &rarr; 1 XOR 1 XOR 1 = 1</code></li>
<li><code>(0, 0, 1) &rarr; 1 XOR 1 XOR 2 = 2</code></li>
<li><code>(0, 1, 1) &rarr; 1 XOR 2 XOR 2 = 1</code></li>
<li><code>(1, 1, 1) &rarr; 2 XOR 2 XOR 2 = 2</code></li>
</ul>
<p>The unique XOR values are <code>{1, 2}</code>, so the output is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The possible XOR triplet values include:</p>
<ul>
<li><code>(0, 0, 0) &rarr; 3 XOR 3 XOR 3 = 3</code></li>
<li><code>(0, 0, 1) &rarr; 3 XOR 3 XOR 1 = 1</code></li>
<li><code>(0, 0, 2) &rarr; 3 XOR 3 XOR 2 = 2</code></li>
<li><code>(0, 1, 2) &rarr; 3 XOR 1 XOR 2 = 0</code></li>
</ul>
<p>The unique XOR values are <code>{0, 1, 2, 3}</code>, so the output is 4.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= n</code></li>
<li><code>nums</code> is a permutation of integers from <code>1</code> to <code>n</code>.</li>
</ul>

View File

@@ -0,0 +1,47 @@
<p data-end="261" data-start="147">You are given an integer array <code>nums</code>.</p>
<p>A <strong>XOR triplet</strong> is defined as the XOR of three elements <code>nums[i] XOR nums[j] XOR nums[k]</code> where <code>i &lt;= j &lt;= k</code>.</p>
<p>Return the number of <strong>unique</strong> XOR triplet values from all possible triplets <code>(i, j, k)</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="158" data-start="101">The possible XOR triplet values are:</p>
<ul data-end="280" data-start="159">
<li data-end="188" data-start="159"><code>(0, 0, 0) &rarr; 1 XOR 1 XOR 1 = 1</code></li>
<li data-end="218" data-start="189"><code>(0, 0, 1) &rarr; 1 XOR 1 XOR 3 = 3</code></li>
<li data-end="248" data-start="219"><code>(0, 1, 1) &rarr; 1 XOR 3 XOR 3 = 1</code></li>
<li data-end="280" data-start="249"><code>(1, 1, 1) &rarr; 3 XOR 3 XOR 3 = 3</code></li>
</ul>
<p data-end="343" data-start="282">The unique XOR values are <code data-end="316" data-start="308">{1, 3}</code>. Thus, the output is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [6,7,8,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The possible XOR triplet values are <code data-end="275" data-start="267">{6, 7, 8, 9}</code>. Thus, the output is 4.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1500</code></li>
<li><code><font face="monospace">1 &lt;= nums[i] &lt;= 1500</font></code></li>
</ul>

View File

@@ -0,0 +1,61 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You can perform the following operation any number of times:</p>
<ul>
<li>Select an index <code>i</code> and replace <code>nums[i]</code> with <code>nums[i] - 1</code>.</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations required to make the sum of the array divisible by <code>k</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,9,7], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Perform 4 operations on <code>nums[1] = 9</code>. Now, <code>nums = [3, 5, 7]</code>.</li>
<li>The sum is 15, which is divisible by 5.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,1,3], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The sum is 8, which is already divisible by 4. Hence, no operations are needed.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2], k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Perform 3 operations on <code>nums[0] = 3</code> and 2 operations on <code>nums[1] = 2</code>. Now, <code>nums = [0, 0]</code>.</li>
<li>The sum is 0, which is divisible by 6.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
<li><code>1 &lt;= k &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,91 @@
<p>You are given an integer <code>n</code> and an undirected, weighted tree rooted at node 1 with <code>n</code> nodes numbered from 1 to <code>n</code>. This is represented by a 2D 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 an undirected edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>You are also given a 2D integer array <code>queries</code> of length <code>q</code>, where each <code>queries[i]</code> is either:</p>
<ul>
<li><code>[1, u, v, w&#39;]</code> &ndash; <strong>Update</strong> the weight of the edge between nodes <code>u</code> and <code>v</code> to <code>w&#39;</code>, where <code>(u, v)</code> is guaranteed to be an edge present in <code>edges</code>.</li>
<li><code>[2, x]</code> &ndash; <strong>Compute</strong> the <strong>shortest</strong> path distance from the root node 1 to node <code>x</code>.</li>
</ul>
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the <strong>shortest</strong> path distance from node 1 to <code>x</code> for the <code>i<sup>th</sup></code> query of <code>[2, x]</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[1,2,7]], queries = [[2,2],[1,1,2,4],[2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/13/screenshot-2025-03-13-at-133524.png" style="width: 200px; height: 75px;" /></p>
<ul>
<li>Query <code>[2,2]</code>: The shortest path from root node 1 to node 2 is 7.</li>
<li>Query <code>[1,1,2,4]</code>: The weight of edge <code>(1,2)</code> changes from 7 to 4.</li>
<li>Query <code>[2,2]</code>: The shortest path from root node 1 to node 2 is 4.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[1,2,2],[1,3,4]], queries = [[2,1],[2,3],[1,1,3,7],[2,2],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,4,2,7]</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/13/screenshot-2025-03-13-at-132247.png" style="width: 180px; height: 141px;" /></p>
<ul>
<li>Query <code>[2,1]</code>: The shortest path from root node 1 to node 1 is 0.</li>
<li>Query <code>[2,3]</code>: The shortest path from root node 1 to node 3 is 4.</li>
<li>Query <code>[1,1,3,7]</code>: The weight of edge <code>(1,3)</code> changes from 4 to 7.</li>
<li>Query <code>[2,2]</code>: The shortest path from root node 1 to node 2 is 2.</li>
<li>Query <code>[2,3]</code>: The shortest path from root node 1 to node 3 is 7.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[1,2,2],[2,3,1],[3,4,5]], queries = [[2,4],[2,3],[1,2,3,3],[2,2],[2,3]]</span></p>
<p><strong>Output:</strong> [8,3,2,5]</p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/13/screenshot-2025-03-13-at-133306.png" style="width: 400px; height: 85px;" /></p>
<ul>
<li>Query <code>[2,4]</code>: The shortest path from root node 1 to node 4 consists of edges <code>(1,2)</code>, <code>(2,3)</code>, and <code>(3,4)</code> with weights <code>2 + 1 + 5 = 8</code>.</li>
<li>Query <code>[2,3]</code>: The shortest path from root node 1 to node 3 consists of edges <code>(1,2)</code> and <code>(2,3)</code> with weights <code>2 + 1 = 3</code>.</li>
<li>Query <code>[1,2,3,3]</code>: The weight of edge <code>(2,3)</code> changes from 1 to 3.</li>
<li>Query <code>[2,2]</code>: The shortest path from root node 1 to node 2 is 2.</li>
<li>Query <code>[2,3]</code>: The shortest path from root node 1 to node 3 consists of edges <code>(1,2)</code> and <code>(2,3)</code> with updated weights <code>2 + 3 = 5</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code></li>
<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= queries.length == q &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2</code> or <code>4</code>
<ul>
<li><code>queries[i] == [1, u, v, w&#39;]</code> or,</li>
<li><code>queries[i] == [2, x]</code></li>
<li><code>1 &lt;= u, v, x &lt;= n</code></li>
<li><code data-end="37" data-start="29">(u, v)</code> is always an edge from <code data-end="74" data-start="67">edges</code>.</li>
<li><code>1 &lt;= w&#39; &lt;= 10<sup>4</sup></code></li>
</ul>
</li>
</ul>

View File

@@ -0,0 +1,93 @@
<p>You are given two arrays, <code>instructions</code> and <code>values</code>, both of size <code>n</code>.</p>
<p>You need to simulate a process based on the following rules:</p>
<ul>
<li>You start at the first instruction at index <code>i = 0</code> with an initial score of 0.</li>
<li>If <code>instructions[i]</code> is <code>&quot;add&quot;</code>:
<ul>
<li>Add <code>values[i]</code> to your score.</li>
<li>Move to the next instruction <code>(i + 1)</code>.</li>
</ul>
</li>
<li>If <code>instructions[i]</code> is <code>&quot;jump&quot;</code>:
<ul>
<li>Move to the instruction at index <code>(i + values[i])</code> without modifying your score.</li>
</ul>
</li>
</ul>
<p>The process ends when you either:</p>
<ul>
<li>Go out of bounds (i.e., <code>i &lt; 0 or i &gt;= n</code>), or</li>
<li>Attempt to revisit an instruction that has been previously executed. The revisited instruction is not executed.</li>
</ul>
<p>Return your score at the end of the process.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">instructions = [&quot;jump&quot;,&quot;add&quot;,&quot;add&quot;,&quot;jump&quot;,&quot;add&quot;,&quot;jump&quot;], values = [2,1,3,1,-2,-3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Simulate the process starting at instruction 0:</p>
<ul>
<li>At index 0: Instruction is <code>&quot;jump&quot;</code>, move to index <code>0 + 2 = 2</code>.</li>
<li>At index 2: Instruction is <code>&quot;add&quot;</code>, add <code>values[2] = 3</code> to your score and move to index 3. Your score becomes 3.</li>
<li>At index 3: Instruction is <code>&quot;jump&quot;</code>, move to index <code>3 + 1 = 4</code>.</li>
<li>At index 4: Instruction is <code>&quot;add&quot;</code>, add <code>values[4] = -2</code> to your score and move to index 5. Your score becomes 1.</li>
<li>At index 5: Instruction is <code>&quot;jump&quot;</code>, move to index <code>5 + (-3) = 2</code>.</li>
<li>At index 2: Already visited. The process ends.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">instructions = [&quot;jump&quot;,&quot;add&quot;,&quot;add&quot;], values = [3,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Simulate the process starting at instruction 0:</p>
<ul>
<li>At index 0: Instruction is <code>&quot;jump&quot;</code>, move to index <code>0 + 3 = 3</code>.</li>
<li>At index 3: Out of bounds. The process ends.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">instructions = [&quot;jump&quot;], values = [0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Simulate the process starting at instruction 0:</p>
<ul>
<li>At index 0: Instruction is <code>&quot;jump&quot;</code>, move to index <code>0 + 0 = 0</code>.</li>
<li>At index 0: Already visited. The process ends.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == instructions.length == values.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>instructions[i]</code> is either <code>&quot;add&quot;</code> or <code>&quot;jump&quot;</code>.</li>
<li><code>-10<sup>5</sup> &lt;= values[i] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,78 @@
<p data-end="116" data-start="0">You are given three integers <code data-end="33" data-start="30">x</code>, <code data-end="38" data-start="35">y</code>, and <code data-end="47" data-start="44">z</code>, representing the positions of three people on a number line:</p>
<ul data-end="252" data-start="118">
<li data-end="154" data-start="118"><code data-end="123" data-start="120">x</code> is the position of Person 1.</li>
<li data-end="191" data-start="155"><code data-end="160" data-start="157">y</code> is the position of Person 2.</li>
<li data-end="252" data-start="192"><code data-end="197" data-start="194">z</code> is the position of Person 3, who does <strong>not</strong> move.</li>
</ul>
<p data-end="322" data-start="254">Both Person 1 and Person 2 move toward Person 3 at the <strong>same</strong> speed.</p>
<p data-end="372" data-start="324">Determine which person reaches Person 3 <strong>first</strong>:</p>
<ul data-end="505" data-start="374">
<li data-end="415" data-start="374">Return 1 if Person 1 arrives first.</li>
<li data-end="457" data-start="416">Return 2 if Person 2 arrives first.</li>
<li data-end="505" data-start="458">Return 0 if both arrive at the <strong>same</strong> time.</li>
</ul>
<p data-end="537" data-is-last-node="" data-is-only-node="" data-start="507">Return the result accordingly.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">x = 2, y = 7, z = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul data-end="258" data-start="113">
<li data-end="193" data-start="113">Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.</li>
<li data-end="258" data-start="194">Person 2 is at position 7 and can reach Person 3 in 3 steps.</li>
</ul>
<p data-end="317" data-is-last-node="" data-is-only-node="" data-start="260">Since Person 1 reaches Person 3 first, the output is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">x = 2, y = 5, z = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul data-end="245" data-start="92">
<li data-end="174" data-start="92">Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.</li>
<li data-end="245" data-start="175">Person 2 is at position 5 and can reach Person 3 in 1 step.</li>
</ul>
<p data-end="304" data-is-last-node="" data-is-only-node="" data-start="247">Since Person 2 reaches Person 3 first, the output is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">x = 1, y = 5, z = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul data-end="245" data-start="92">
<li data-end="174" data-start="92">Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.</li>
<li data-end="245" data-start="175">Person 2 is at position 5 and can reach Person 3 in 2 steps.</li>
</ul>
<p data-end="304" data-is-last-node="" data-is-only-node="" data-start="247">Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= x, y, z &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,99 @@
<p>You are given an integer array <code>nums</code> and two integers, <code>k</code> and <code>limit</code>. Your task is to find a non-empty <strong><span data-keyword="subsequence-array">subsequence</span></strong> of <code>nums</code> that:</p>
<ul>
<li>Has an <strong>alternating sum</strong> equal to <code>k</code>.</li>
<li><strong>Maximizes</strong> the product of all its numbers <em>without the product exceeding</em> <code>limit</code>.</li>
</ul>
<p>Return the <em>product</em> of the numbers in such a subsequence. If no subsequence satisfies the requirements, return -1.</p>
<p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</p>
<p>&nbsp;</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], k = 2, limit = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The subsequences with an alternating sum of 2 are:</p>
<ul>
<li><code>[1, 2, 3]</code>
<ul>
<li>Alternating Sum: <code>1 - 2 + 3 = 2</code></li>
<li>Product: <code>1 * 2 * 3 = 6</code></li>
</ul>
</li>
<li><code>[2]</code>
<ul>
<li>Alternating Sum: 2</li>
<li>Product: 2</li>
</ul>
</li>
</ul>
<p>The maximum product within the limit is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,2,3], k = -5, limit = 12</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>A subsequence with an alternating sum of exactly -5 does not exist.</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,3,3], k = 0, limit = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>The subsequences with an alternating sum of 0 are:</p>
<ul>
<li><code>[2, 2]</code>
<ul>
<li>Alternating Sum: <code>2 - 2 = 0</code></li>
<li>Product: <code>2 * 2 = 4</code></li>
</ul>
</li>
<li><code>[3, 3]</code>
<ul>
<li>Alternating Sum: <code>3 - 3 = 0</code></li>
<li>Product: <code>3 * 3 = 9</code></li>
</ul>
</li>
<li><code>[2, 2, 3, 3]</code>
<ul>
<li>Alternating Sum: <code>2 - 2 + 3 - 3 = 0</code></li>
<li>Product: <code>2 * 2 * 3 * 3 = 36</code></li>
</ul>
</li>
</ul>
<p>The subsequence <code>[2, 2, 3, 3]</code> has the greatest product with an alternating sum equal to <code>k</code>, but <code>36 &gt; 9</code>. The next greatest product is 9, which is within the limit.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 150</code></li>
<li><code>0 &lt;= nums[i] &lt;= 12</code></li>
<li><code>-10<sup>5</sup> &lt;= k &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= limit &lt;= 5000</code></li>
</ul>

View File

@@ -0,0 +1,49 @@
<p>You are given a <strong><span data-keyword="palindrome-string">palindromic</span></strong> string <code>s</code>.</p>
<p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> palindromic <span data-keyword="permutation-string">permutation</span> of <code>s</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;z&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;z&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>A string of only one character is already the lexicographically smallest palindrome.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;babab&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;abbba&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>Rearranging <code>&quot;babab&quot;</code> &rarr; <code>&quot;abbba&quot;</code> gives the smallest lexicographic palindrome.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;daccad&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;acddca&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>Rearranging <code>&quot;daccad&quot;</code> &rarr; <code>&quot;acddca&quot;</code> gives the smallest lexicographic palindrome.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>s</code> is guaranteed to be palindromic.</li>
</ul>

View File

@@ -0,0 +1,61 @@
<p data-end="332" data-start="99">You are given a <strong><span data-keyword="palindrome-string">palindromic</span></strong> string <code>s</code> and an integer <code>k</code>.</p>
<p>Return the <strong>k-th</strong> <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> palindromic <span data-keyword="permutation-string">permutation</span> of <code>s</code>. If there are fewer than <code>k</code> distinct palindromic permutations, return an empty string.</p>
<p><strong>Note:</strong> Different rearrangements that yield the same palindromic string are considered identical and are counted once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;abba&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;baab&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The two distinct palindromic rearrangements of <code>&quot;abba&quot;</code> are <code>&quot;abba&quot;</code> and <code>&quot;baab&quot;</code>.</li>
<li>Lexicographically, <code>&quot;abba&quot;</code> comes before <code>&quot;baab&quot;</code>. Since <code>k = 2</code>, the output is <code>&quot;baab&quot;</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>There is only one palindromic rearrangement: <code data-end="1112" data-start="1106">&quot;aa&quot;</code>.</li>
<li>The output is an empty string since <code>k = 2</code> exceeds the number of possible rearrangements.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;bacab&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;abcba&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The two distinct palindromic rearrangements of <code>&quot;bacab&quot;</code> are <code>&quot;abcba&quot;</code> and <code>&quot;bacab&quot;</code>.</li>
<li>Lexicographically, <code>&quot;abcba&quot;</code> comes before <code>&quot;bacab&quot;</code>. Since <code>k = 1</code>, the output is <code>&quot;abcba&quot;</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>s</code> is guaranteed to be palindromic.</li>
<li><code>1 &lt;= k &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,126 @@
<p>Table: <code>ProductPurchases</code></p>
<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| product_id | int |
| quantity | int |
+-------------+------+
(user_id, product_id) is the unique key for this table.
Each row represents a purchase of a product by a user in a specific quantity.
</pre>
<p>Table: <code>ProductInfo</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| category | varchar |
| price | decimal |
+-------------+---------+
product_id is the primary key for this table.
Each row assigns a category and price to a product.
</pre>
<p>Amazon wants to implement the <strong>Customers who bought this also bought...</strong> feature based on <strong>co-purchase patterns</strong>. Write a solution to :</p>
<ol>
<li>Identify <strong>distinct</strong> product pairs frequently <strong>purchased together by the same customers</strong> (where <code>product1_id</code> &lt; <code>product2_id</code>)</li>
<li>For <strong>each product pair</strong>, determine how many customers purchased <strong>both</strong> products</li>
</ol>
<p><strong>A product pair </strong>is considered for recommendation <strong>if</strong> <strong>at least</strong> <code>3</code> <strong>different</strong> customers have purchased <strong>both products</strong>.</p>
<p>Return <em>the </em><em>result table ordered by <strong>customer_count</strong> in <strong>descending</strong> order, and in case of a tie, by </em><code>product1_id</code><em> in <strong>ascending</strong> order, and then by </em><code>product2_id</code><em> in <strong>ascending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>ProductPurchases table:</p>
<pre class="example-io">
+---------+------------+----------+
| user_id | product_id | quantity |
+---------+------------+----------+
| 1 | 101 | 2 |
| 1 | 102 | 1 |
| 1 | 103 | 3 |
| 2 | 101 | 1 |
| 2 | 102 | 5 |
| 2 | 104 | 1 |
| 3 | 101 | 2 |
| 3 | 103 | 1 |
| 3 | 105 | 4 |
| 4 | 101 | 1 |
| 4 | 102 | 1 |
| 4 | 103 | 2 |
| 4 | 104 | 3 |
| 5 | 102 | 2 |
| 5 | 104 | 1 |
+---------+------------+----------+
</pre>
<p>ProductInfo table:</p>
<pre class="example-io">
+------------+-------------+-------+
| product_id | category | price |
+------------+-------------+-------+
| 101 | Electronics | 100 |
| 102 | Books | 20 |
| 103 | Clothing | 35 |
| 104 | Kitchen | 50 |
| 105 | Sports | 75 |
+------------+-------------+-------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------+-------------+-------------------+-------------------+----------------+
| product1_id | product2_id | product1_category | product2_category | customer_count |
+-------------+-------------+-------------------+-------------------+----------------+
| 101 | 102 | Electronics | Books | 3 |
| 101 | 103 | Electronics | Clothing | 3 |
| 102 | 104 | Books | Kitchen | 3 |
+-------------+-------------+-------------------+-------------------+----------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Product pair (101, 102):</strong>
<ul>
<li>Purchased by users 1, 2, and 4 (3 customers)</li>
<li>Product 101 is in Electronics category</li>
<li>Product 102 is in Books category</li>
</ul>
</li>
<li><strong>Product pair (101, 103):</strong>
<ul>
<li>Purchased by users 1, 3, and 4 (3 customers)</li>
<li>Product 101 is in Electronics category</li>
<li>Product 103 is in Clothing category</li>
</ul>
</li>
<li><strong>Product pair (102, 104):</strong>
<ul>
<li>Purchased by users 2, 4, and 5 (3 customers)</li>
<li>Product 102 is in Books category</li>
<li>Product 104 is in Kitchen category</li>
</ul>
</li>
</ul>
<p>The result is ordered by customer_count in descending order. For pairs with the same customer_count, they are ordered by product1_id and then product2_id in ascending order.</p>
</div>

View File

@@ -0,0 +1,92 @@
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>You are allowed to perform an operation <strong>once</strong> on <code>nums</code>, where in each operation you can remove any <strong>non-overlapping</strong> prefix and suffix from <code>nums</code> such that <code>nums</code> remains <strong>non-empty</strong>.</p>
<p>You need to find the <strong>x-value</strong> of <code>nums</code>, which is the number of ways to perform this operation so that the <strong>product</strong> of the remaining elements leaves a <em>remainder</em> of <code>x</code> when divided by <code>k</code>.</p>
<p>Return an array <code>result</code> of size <code>k</code> where <code>result[x]</code> is the <strong>x-value</strong> of <code>nums</code> for <code>0 &lt;= x &lt;= k - 1</code>.</p>
<p>A <strong>prefix</strong> of an array is a <span data-keyword="subarray">subarray</span> that starts from the beginning of the array and extends to any point within it.</p>
<p>A <strong>suffix</strong> of an array is a <span data-keyword="subarray">subarray</span> that starts at any point within the array and extends to the end of the array.</p>
<p><strong>Note</strong> that the prefix and suffix to be chosen for the operation can be <strong>empty</strong>.</p>
<p>&nbsp;</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,4,5], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[9,2,4]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>x = 0</code>, the possible operations include all possible ways to remove non-overlapping prefix/suffix that do not remove <code>nums[2] == 3</code>.</li>
<li>For <code>x = 1</code>, the possible operations are:
<ul>
<li>Remove the empty prefix and the suffix <code>[2, 3, 4, 5]</code>. <code>nums</code> becomes <code>[1]</code>.</li>
<li>Remove the prefix <code>[1, 2, 3]</code> and the suffix <code>[5]</code>. <code>nums</code> becomes <code>[4]</code>.</li>
</ul>
</li>
<li>For <code>x = 2</code>, the possible operations are:
<ul>
<li>Remove the empty prefix and the suffix <code>[3, 4, 5]</code>. <code>nums</code> becomes <code>[1, 2]</code>.</li>
<li>Remove the prefix <code>[1]</code> and the suffix <code>[3, 4, 5]</code>. <code>nums</code> becomes <code>[2]</code>.</li>
<li>Remove the prefix <code>[1, 2, 3]</code> and the empty suffix. <code>nums</code> becomes <code>[4, 5]</code>.</li>
<li>Remove the prefix <code>[1, 2, 3, 4]</code> and the empty suffix. <code>nums</code> becomes <code>[5]</code>.</li>
</ul>
</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,8,16,32], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">[18,1,2,0]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>x = 0</code>, the only operations that <strong>do not</strong> result in <code>x = 0</code> are:
<ul>
<li>Remove the empty prefix and the suffix <code>[4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[1, 2]</code>.</li>
<li>Remove the empty prefix and the suffix <code>[2, 4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[1]</code>.</li>
<li>Remove the prefix <code>[1]</code> and the suffix <code>[4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[2]</code>.</li>
</ul>
</li>
<li>For <code>x = 1</code>, the only possible operation is:
<ul>
<li>Remove the empty prefix and the suffix <code>[2, 4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[1]</code>.</li>
</ul>
</li>
<li>For <code>x = 2</code>, the possible operations are:
<ul>
<li>Remove the empty prefix and the suffix <code>[4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[1, 2]</code>.</li>
<li>Remove the prefix <code>[1]</code> and the suffix <code>[4, 8, 16, 32]</code>. <code>nums</code> becomes <code>[2]</code>.</li>
</ul>
</li>
<li>For <code>x = 3</code>, there is no possible way to perform the operation.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,1,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[9,6]</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= 5</code></li>
</ul>

View File

@@ -0,0 +1,98 @@
<p>You are given an array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. You are also given a 2D array <code>queries</code>, where <code>queries[i] = [index<sub>i</sub>, value<sub>i</sub>, start<sub>i</sub>, x<sub>i</sub>]</code>.</p>
<p>You are allowed to perform an operation <strong>once</strong> on <code>nums</code>, where you can remove any <strong>suffix</strong> from <code>nums</code> such that <code>nums</code> remains <strong>non-empty</strong>.</p>
<p>The <strong>x-value</strong> of <code>nums</code> <strong>for a given</strong> <code>x</code> is defined as the number of ways to perform this operation so that the <strong>product</strong> of the remaining elements leaves a <em>remainder</em> of <code>x</code> <strong>modulo</strong> <code>k</code>.</p>
<p>For each query in <code>queries</code> you need to determine the <strong>x-value</strong> of <code>nums</code> for <code>x<sub>i</sub></code> after performing the following actions:</p>
<ul>
<li>Update <code>nums[index<sub>i</sub>]</code> to <code>value<sub>i</sub></code>. Only this step persists for the rest of the queries.</li>
<li><strong>Remove</strong> the prefix <code>nums[0..(start<sub>i</sub> - 1)]</code> (where <code>nums[0..(-1)]</code> will be used to represent the <strong>empty</strong> prefix).</li>
</ul>
<p>Return an array <code>result</code> of size <code>queries.length</code> where <code>result[i]</code> is the answer for the <code>i<sup>th</sup></code> query.</p>
<p>A <strong>prefix</strong> of an array is a <span data-keyword="subarray">subarray</span> that starts from the beginning of the array and extends to any point within it.</p>
<p>A <strong>suffix</strong> of an array is a <span data-keyword="subarray">subarray</span> that starts at any point within the array and extends to the end of the array.</p>
<p><strong>Note</strong> that the prefix and suffix to be chosen for the operation can be <strong>empty</strong>.</p>
<p><strong>Note</strong> that x-value has a <em>different</em> definition in this version.</p>
<p>&nbsp;</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,4,5], k = 3, queries = [[2,2,0,2],[3,3,3,0],[0,1,0,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,2]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For query 0, <code>nums</code> becomes <code>[1, 2, 2, 4, 5]</code>, and the empty prefix <strong>must</strong> be removed. The possible operations are:
<ul>
<li>Remove the suffix <code>[2, 4, 5]</code>. <code>nums</code> becomes <code>[1, 2]</code>.</li>
<li>Remove the empty suffix. <code>nums</code> becomes <code>[1, 2, 2, 4, 5]</code> with a product 80, which gives remainder 2 when divided by 3.</li>
</ul>
</li>
<li>For query 1, <code>nums</code> becomes <code>[1, 2, 2, 3, 5]</code>, and the prefix <code>[1, 2, 2]</code> <strong>must</strong> be removed. The possible operations are:
<ul>
<li>Remove the empty suffix. <code>nums</code> becomes <code>[3, 5]</code>.</li>
<li>Remove the suffix <code>[5]</code>. <code>nums</code> becomes <code>[3]</code>.</li>
</ul>
</li>
<li>For query 2, <code>nums</code> becomes <code>[1, 2, 2, 3, 5]</code>, and the empty prefix <strong>must</strong> be removed. The possible operations are:
<ul>
<li>Remove the suffix <code>[2, 2, 3, 5]</code>. <code>nums</code> becomes <code>[1]</code>.</li>
<li>Remove the suffix <code>[3, 5]</code>. <code>nums</code> becomes <code>[1, 2, 2]</code>.</li>
</ul>
</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,8,16,32], k = 4, queries = [[0,2,0,2],[0,2,0,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,0]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For query 0, <code>nums</code> becomes <code>[2, 2, 4, 8, 16, 32]</code>. The only possible operation is:
<ul>
<li>Remove the suffix <code>[2, 4, 8, 16, 32]</code>.</li>
</ul>
</li>
<li>For query 1, <code>nums</code> becomes <code>[2, 2, 4, 8, 16, 32]</code>. There is no possible way to perform the operation.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,1,1], k = 2, queries = [[2,1,0,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5]</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= 5</code></li>
<li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>queries[i] == [index<sub>i</sub>, value<sub>i</sub>, start<sub>i</sub>, x<sub>i</sub>]</code></li>
<li><code>0 &lt;= index<sub>i</sub> &lt;= nums.length - 1</code></li>
<li><code>1 &lt;= value<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= start<sub>i</sub> &lt;= nums.length - 1</code></li>
<li><code>0 &lt;= x<sub>i</sub> &lt;= k - 1</code></li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>Given an array <code>nums</code>, you can perform the following operation any number of times:</p>
<ul>
<li>Select the <strong>adjacent</strong> pair with the <strong>minimum</strong> sum in <code>nums</code>. If multiple such pairs exist, choose the leftmost one.</li>
<li>Replace the pair with their sum.</li>
</ul>
<p>Return the <strong>minimum number of operations</strong> needed to make the array <strong>non-decreasing</strong>.</p>
<p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous element (if it exists).</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The pair <code>(3,1)</code> has the minimum sum of 4. After replacement, <code>nums = [5,2,4]</code>.</li>
<li>The pair <code>(2,4)</code> has the minimum sum of 6. After replacement, <code>nums = [5,6]</code>.</li>
</ul>
<p>The array <code>nums</code> became non-decreasing in two operations.</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,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The array <code>nums</code> is already sorted.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>Given an array <code>nums</code>, you can perform the following operation any number of times:</p>
<ul>
<li>Select the <strong>adjacent</strong> pair with the <strong>minimum</strong> sum in <code>nums</code>. If multiple such pairs exist, choose the leftmost one.</li>
<li>Replace the pair with their sum.</li>
</ul>
<p>Return the <strong>minimum number of operations</strong> needed to make the array <strong>non-decreasing</strong>.</p>
<p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous element (if it exists).</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The pair <code>(3,1)</code> has the minimum sum of 4. After replacement, <code>nums = [5,2,4]</code>.</li>
<li>The pair <code>(2,4)</code> has the minimum sum of 6. After replacement, <code>nums = [5,6]</code>.</li>
</ul>
<p>The array <code>nums</code> became non-decreasing in two operations.</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,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The array <code>nums</code> is already sorted.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,47 @@
<p>You are given two integers, <code>l</code> and <code>r</code>, represented as strings, and an integer <code>b</code>. Return the count of integers in the inclusive range <code>[l, r]</code> whose digits are in <strong>non-decreasing</strong> order when represented in base <code>b</code>.</p>
<p>An integer is considered to have <strong>non-decreasing</strong> digits if, when read from left to right (from the most significant digit to the least significant digit), each digit is greater than or equal to the previous one.</p>
<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">l = &quot;23&quot;, r = &quot;28&quot;, b = 8</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The numbers from 23 to 28 in base 8 are: 27, 30, 31, 32, 33, and 34.</li>
<li>Out of these, 27, 33, and 34 have non-decreasing digits. Hence, the output is 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">l = &quot;2&quot;, r = &quot;7&quot;, b = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The numbers from 2 to 7 in base 2 are: 10, 11, 100, 101, 110, and 111.</li>
<li>Out of these, 11 and 111 have non-decreasing digits. Hence, the output is 2.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code><font face="monospace">1 &lt;= l.length &lt;= r.length &lt;= 100</font></code></li>
<li><code>2 &lt;= b &lt;= 10</code></li>
<li><code>l</code> and <code>r</code> consist only of digits.</li>
<li>The value represented by <code>l</code> is less than or equal to the value represented by <code>r</code>.</li>
<li><code>l</code> and <code>r</code> do not contain leading zeros.</li>
</ul>

View File

@@ -0,0 +1,89 @@
<p>Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:</p>
<ul>
<li><code>source</code>: A unique identifier for the machine that generated the packet.</li>
<li><code>destination</code>: A unique identifier for the target machine.</li>
<li><code>timestamp</code>: The time at which the packet arrived at the router.</li>
</ul>
<p>Implement the <code>Router</code> class:</p>
<p><code>Router(int memoryLimit)</code>: Initializes the Router object with a fixed memory limit.</p>
<ul>
<li><code>memoryLimit</code> is the <strong>maximum</strong> number of packets the router can store at any given time.</li>
<li>If adding a new packet would exceed this limit, the <strong>oldest</strong> packet must be removed to free up space.</li>
</ul>
<p><code>bool addPacket(int source, int destination, int timestamp)</code>: Adds a packet with the given attributes to the router.</p>
<ul>
<li>A packet is considered a duplicate if another packet with the same <code>source</code>, <code>destination</code>, and <code>timestamp</code> already exists in the router.</li>
<li>Return <code>true</code> if the packet is successfully added (i.e., it is not a duplicate); otherwise return <code>false</code>.</li>
</ul>
<p><code>int[] forwardPacket()</code>: Forwards the next packet in FIFO (First In First Out) order.</p>
<ul>
<li>Remove the packet from storage.</li>
<li>Return the packet as an array <code>[source, destination, timestamp]</code>.</li>
<li>If there are no packets to forward, return an empty array.</li>
</ul>
<p><code>int getCount(int destination, int startTime, int endTime)</code>:</p>
<ul>
<li>Returns the number of packets currently stored in the router (i.e., not yet forwarded) that have the specified destination and have timestamps in the inclusive range <code>[startTime, endTime]</code>.</li>
</ul>
<p><strong>Note</strong> that queries for <code>addPacket</code> will be made in increasing order of <code>timestamp</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong><br />
<span class="example-io">[&quot;Router&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;forwardPacket&quot;, &quot;addPacket&quot;, &quot;getCount&quot;]<br />
[[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]]</span></p>
<p><strong>Output:</strong><br />
<span class="example-io">[null, true, true, false, true, true, [2, 5, 90], true, 1] </span></p>
<p><strong>Explanation</strong></p>
Router router = new Router(3); // Initialize Router with memoryLimit of 3.<br />
router.addPacket(1, 4, 90); // Packet is added. Return True.<br />
router.addPacket(2, 5, 90); // Packet is added. Return True.<br />
router.addPacket(1, 4, 90); // This is a duplicate packet. Return False.<br />
router.addPacket(3, 5, 95); // Packet is added. Return True<br />
router.addPacket(4, 5, 105); // Packet is added, <code>[1, 4, 90]</code> is removed as number of packets exceeds memoryLimit. Return True.<br />
router.forwardPacket(); // Return <code>[2, 5, 90]</code> and remove it from router.<br />
router.addPacket(5, 2, 110); // Packet is added. Return True.<br />
router.getCount(5, 100, 110); // The only packet with destination 5 and timestamp in the inclusive range <code>[100, 110]</code> is <code>[4, 5, 105]</code>. Return 1.</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong><br />
<span class="example-io">[&quot;Router&quot;, &quot;addPacket&quot;, &quot;forwardPacket&quot;, &quot;forwardPacket&quot;]<br />
[[2], [7, 4, 90], [], []]</span></p>
<p><strong>Output:</strong><br />
<span class="example-io">[null, true, [7, 4, 90], []] </span></p>
<p><strong>Explanation</strong></p>
Router router = new Router(2); // Initialize <code>Router</code> with <code>memoryLimit</code> of 2.<br />
router.addPacket(7, 4, 90); // Return True.<br />
router.forwardPacket(); // Return <code>[7, 4, 90]</code>.<br />
router.forwardPacket(); // There are no packets left, return <code>[]</code>.</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= memoryLimit &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= source, destination &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= timestamp &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= startTime &lt;= endTime &lt;= 10<sup>9</sup></code></li>
<li>At most <code>10<sup>5</sup></code> calls will be made to <code>addPacket</code>, <code>forwardPacket</code>, and <code>getCount</code> methods altogether.</li>
<li>queries for <code>addPacket</code> will be made in increasing order of <code>timestamp</code>.</li>
</ul>

View File

@@ -0,0 +1,43 @@
<p>You are given an integer array <code>nums</code>. In one operation, you can select a <span data-keyword="subarray-nonempty">subarray</span> and replace it with a single element equal to its <strong>maximum</strong> value.</p>
<p>Return the <strong>maximum possible size</strong> of the array after performing zero or more operations such that the resulting array is <strong>non-decreasing</strong>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,5,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>One way to achieve the maximum size is:</p>
<ol>
<li>Replace subarray <code>nums[1..2] = [2, 5]</code> with <code>5</code> &rarr; <code>[4, 5, 3, 5]</code>.</li>
<li>Replace subarray <code>nums[2..3] = [3, 5]</code> with <code>5</code> &rarr; <code>[4, 5, 5]</code>.</li>
</ol>
<p>The final array <code>[4, 5, 5]</code> is non-decreasing with size <font face="monospace">3.</font></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]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>No operation is needed as the array <code>[1,2,3]</code> is already non-decreasing.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>5</sup></code></li>
</ul>