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,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node.</p>
|
||||
|
||||
<p>You start with a score of <code>0</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any node <code>i</code>.</li>
|
||||
<li>Add <code>values[i]</code> to your score.</li>
|
||||
<li>Set <code>values[i]</code> to <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A tree is <strong>healthy</strong> if the sum of values on the path from the root to any leaf node is different than zero.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum score</strong> you can obtain after performing these operations on the tree any number of times so that it remains <strong>healthy</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png" style="width: 515px; height: 443px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
|
||||
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png" style="width: 522px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> We can choose nodes 0, 2, 3, and 4.
|
||||
- The sum of values on the path from 0 to 4 is equal to 10.
|
||||
- The sum of values on the path from 0 to 3 is equal to 10.
|
||||
- The sum of values on the path from 0 to 5 is equal to 3.
|
||||
- The sum of values on the path from 0 to 6 is equal to 5.
|
||||
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
|
||||
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 2 * 10<sup>4</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>values.length == n</code></li>
|
||||
<li><code>1 <= values[i] <= 10<sup>9</sup></code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>k</code> and consisting of <strong>indices</strong> <code>i<sub>0</sub> < i<sub>1</sub> < ... < i<sub>k-1</sub></code> is <strong>balanced</strong> if the following holds:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i<sub>j</sub>] - nums[i<sub>j-1</sub>] >= i<sub>j</sub> - i<sub>j-1</sub></code>, for every <code>j</code> in the range <code>[1, k - 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>1</code> is considered balanced.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> possible <strong>sum of elements</strong> in a <strong>balanced</strong> subsequence of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,5,6]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
|
||||
nums[2] - nums[0] >= 2 - 0.
|
||||
nums[3] - nums[2] >= 3 - 2.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
The subsequence consisting of indices 1, 2, and 3 is also valid.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,-1,-3,8]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
|
||||
nums[3] - nums[0] >= 3 - 0.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,-1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, the subsequence [-1] can be selected.
|
||||
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array <code>nums</code>: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,6,25,30]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array <code>nums</code>: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
|
||||
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [500,520,2500,3000]
|
||||
<strong>Output:</strong> 1020
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
|
||||
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 2<sup>20</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> 2D boolean matrix <code>grid</code> of size <code>n * n</code>. For all <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>i != j</code> team <code>i</code> is <strong>stronger</strong> than team <code>j</code> if <code>grid[i][j] == 1</code>, otherwise, team <code>j</code> is <strong>stronger</strong> than team <code>i</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is stronger than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the champion of the tournament.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1],[0,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are two teams in this tournament.
|
||||
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,1],[1,0,1],[0,0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There are three teams in this tournament.
|
||||
grid[1][0] == 1 means that team 1 is stronger than team 0.
|
||||
grid[1][2] == 1 means that team 1 is stronger than team 2.
|
||||
So team 1 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>For all <code>i grid[i][i]</code> is <code>0.</code></li>
|
||||
<li>For all <code>i, j</code> that <code>i != j</code>, <code>grid[i][j] != grid[j][i]</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament; each team is also a node in a <strong>DAG</strong>.</p>
|
||||
|
||||
<p>You are given the integer <code>n</code> and a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code><font face="monospace">m</font></code> representing the <strong>DAG</strong>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a directed edge from team <code>u<sub>i</sub></code> to team <code>v<sub>i</sub></code> in the graph.</p>
|
||||
|
||||
<p>A directed edge from <code>a</code> to <code>b</code> in the graph means that team <code>a</code> is <strong>stronger</strong> than team <code>b</code> and team <code>b</code> is <strong>weaker</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is <strong>stronger</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the <strong>champion</strong> of the tournament if there is a <strong>unique</strong> champion, otherwise, return </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>cycle</strong> is a series of nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a<sub>n+1</sub></code> such that node <code>a<sub>1</sub></code> is the same node as node <code>a<sub>n+1</sub></code>, the nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> are distinct, and there is a directed edge from the node <code>a<sub>i</sub></code> to node <code>a<sub>i+1</sub></code> for every <code>i</code> in the range <code>[1, n]</code>.</li>
|
||||
<li>A <strong>DAG</strong> is a directed graph that does not have any <strong>cycle</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-3.png" width="300" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-4.png" width="300" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[0,2],[1,3],[1,2]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>m == edges.length</code></li>
|
||||
<li><code>0 <= m <= n * (n - 1) / 2</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= edge[i][j] <= n - 1</code></li>
|
||||
<li><code>edges[i][0] != edges[i][1]</code></li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code>, team <code>b</code> is not stronger than team <code>a</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays, <code>nums1</code> and <code>nums2</code>, both having length <code>n</code>.</p>
|
||||
|
||||
<p>You are allowed to perform a series of <strong>operations</strong> (<strong>possibly none</strong>).</p>
|
||||
|
||||
<p>In an operation, you select an index <code>i</code> in the range <code>[0, n - 1]</code> and <strong>swap</strong> the values of <code>nums1[i]</code> and <code>nums2[i]</code>.</p>
|
||||
|
||||
<p>Your task is to find the <strong>minimum</strong> number of operations required to satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1[n - 1]</code> is equal to the <strong>maximum value</strong> among all elements of <code>nums1</code>, i.e., <code>nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])</code>.</li>
|
||||
<li><code>nums2[n - 1]</code> is equal to the <strong>maximum</strong> <strong>value</strong> among all elements of <code>nums2</code>, i.e., <code>nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of operations needed to meet <strong>both</strong> conditions</em>, <em>or </em><code>-1</code><em> if it is <strong>impossible</strong> to satisfy both conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,7], nums2 = [4,5,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, an operation can be performed using index i = 2.
|
||||
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 1.
|
||||
So, the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the following operations can be performed:
|
||||
First operation using index i = 4.
|
||||
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
|
||||
Another operation using index i = 3.
|
||||
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 2.
|
||||
So, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,5,4], nums2 = [2,5,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, it is not possible to satisfy both conditions.
|
||||
So, the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums1.length == nums2.length <= 1000</code></li>
|
||||
<li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>1 <= limit <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= limit <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m * n</code> integer matrix <code>values</code>, representing the values of <code>m * n</code> different items in <code>m</code> different shops. Each shop has <code>n</code> items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value of <code>values[i][j]</code>. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is, <code>values[i][j] >= values[i][j + 1]</code> for all <code>0 <= j < n - 1</code>.</p>
|
||||
|
||||
<p>On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any shop <code>i</code>.</li>
|
||||
<li>Buy the rightmost available item <code>j</code> for the price of <code>values[i][j] * d</code>. That is, find the greatest index <code>j</code> such that item <code>j</code> was never bought before, and buy it for the price of <code>values[i][j] * d</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that all items are pairwise different. For example, if you have bought item <code>0</code> from shop <code>1</code>, you can still buy item <code>0</code> from any other shop.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum amount of money that can be spent</strong> on buying all </em> <code>m * n</code> <em>products</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[8,5,2],[6,4,1],[9,7,3]]
|
||||
<strong>Output:</strong> 285
|
||||
<strong>Explanation:</strong> On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
|
||||
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
|
||||
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
|
||||
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
|
||||
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
|
||||
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
|
||||
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
|
||||
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
|
||||
Hence, our total spending is equal to 285.
|
||||
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[10,8,6,4,2],[9,7,5,3,2]]
|
||||
<strong>Output:</strong> 386
|
||||
<strong>Explanation:</strong> On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
|
||||
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
|
||||
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
|
||||
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
|
||||
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
|
||||
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
|
||||
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
|
||||
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
|
||||
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
|
||||
Hence, our total spending is equal to 386.
|
||||
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m == values.length <= 10</code></li>
|
||||
<li><code>1 <= n == values[i].length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= values[i][j] <= 10<sup>6</sup></code></li>
|
||||
<li><code>values[i]</code> are sorted in non-increasing order.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given an integer <code>n</code>.</p>
|
||||
|
||||
<p>A string <code>s</code> is called <strong>good </strong>if it contains only lowercase English characters <strong>and</strong> it is possible to rearrange the characters of <code>s</code> such that the new string contains <code>"leet"</code> as a <strong>substring</strong>.</p>
|
||||
|
||||
<p>For example:</p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>"lteer"</code> is good because we can rearrange it to form <code>"leetr"</code> .</li>
|
||||
<li><code>"letl"</code> is not good because we cannot rearrange it to contain <code>"leet"</code> as a substring.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total</strong> number of good strings of length </em><code>n</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo </strong><code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 83943898
|
||||
<strong>Explanation:</strong> The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (10<sup>9</sup> + 7) = 83943898.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given a 2D <strong>0-indexed</strong> array of strings, <code>access_times</code>, with size <code>n</code>. For each <code>i</code> where <code>0 <= i <= n - 1</code>, <code>access_times[i][0]</code> represents the name of an employee, and <code>access_times[i][1]</code> represents the access time of that employee. All entries in <code>access_times</code> are within the same day.</p>
|
||||
|
||||
<p>The access time is represented as <strong>four digits</strong> using a <strong>24-hour</strong> time format, for example, <code>"0800"</code> or <code>"2250"</code>.</p>
|
||||
|
||||
<p>An employee is said to be <strong>high-access</strong> if he has accessed the system <strong>three or more</strong> times within a <strong>one-hour period</strong>.</p>
|
||||
|
||||
<p>Times with exactly one hour of difference are <strong>not</strong> considered part of the same one-hour period. For example, <code>"0815"</code> and <code>"0915"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Access times at the start and end of the day are <strong>not</strong> counted within the same one-hour period. For example, <code>"0005"</code> and <code>"2350"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Return <em>a list that contains the names of <strong>high-access</strong> employees with any order you want.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
|
||||
<strong>Output:</strong> ["a"]
|
||||
<strong>Explanation:</strong> "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
|
||||
But "b" does not have more than two access times at all.
|
||||
So the answer is ["a"].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
|
||||
<strong>Output:</strong> ["c","d"]
|
||||
<strong>Explanation:</strong> "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
|
||||
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
|
||||
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
|
||||
<strong>Output:</strong> ["ab","cd"]
|
||||
<strong>Explanation:</strong> "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
|
||||
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
|
||||
So the answer is ["ab","cd"].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= access_times.length <= 100</code></li>
|
||||
<li><code>access_times[i].length == 2</code></li>
|
||||
<li><code>1 <= access_times[i][0].length <= 10</code></li>
|
||||
<li><code>access_times[i][0]</code> consists only of English small letters.</li>
|
||||
<li><code>access_times[i][1].length == 4</code></li>
|
||||
<li><code>access_times[i][1]</code> is in 24-hour time format.</li>
|
||||
<li><code>access_times[i][1]</code> consists only of <code>'0'</code> to <code>'9'</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user