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,34 @@
|
||||
<p>You are given an array of non-negative integers <code>nums</code> and an integer <code>k</code>. In one operation, you may choose <strong>any</strong> element from <code>nums</code> and <strong>increment</strong> it by <code>1</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> <strong>product</strong> of </em><code>nums</code><em> after <strong>at most</strong> </em><code>k</code><em> operations. </em>Since the answer may be very large, return it <b>modulo</b> <code>10<sup>9</sup> + 7</code>. Note that you should maximize the product before taking the modulo. </p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,4], k = 5
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> Increment the first number 5 times.
|
||||
Now nums = [5, 4], with a product of 5 * 4 = 20.
|
||||
It can be shown that 20 is maximum product possible, so we return 20.
|
||||
Note that there may be other ways to increment nums to have the maximum product.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,3,3,2], k = 2
|
||||
<strong>Output:</strong> 216
|
||||
<strong>Explanation:</strong> Increment the second number 1 time and increment the fourth number 1 time.
|
||||
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
|
||||
It can be shown that 216 is maximum product possible, so we return 216.
|
||||
Note that there may be other ways to increment nums to have the maximum product.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
Given two integers <code>num1</code> and <code>num2</code>, return <em>the <strong>sum</strong> of the two integers</em>.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 12, num2 = 5
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = -10, num2 = 4
|
||||
<strong>Output:</strong> -6
|
||||
<strong>Explanation:</strong> num1 + num2 = -6, so -6 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-100 <= num1, num2 <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>You are given an integer <code>total</code> indicating the amount of money you have. You are also given two integers <code>cost1</code> and <code>cost2</code> indicating the price of a pen and pencil respectively. You can spend <strong>part or all</strong> of your money to buy multiple quantities (or none) of each kind of writing utensil.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of distinct ways</strong> you can buy some number of pens and pencils.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> total = 20, cost1 = 10, cost2 = 5
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The price of a pen is 10 and the price of a pencil is 5.
|
||||
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
|
||||
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
|
||||
- If you buy 2 pens, you cannot buy any pencils.
|
||||
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> total = 5, cost1 = 10, cost2 = 10
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= total, cost1, cost2 <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary tree</strong> that consists of exactly <code>3</code> nodes: the root, its left child, and its right child.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if the value of the root is equal to the <strong>sum</strong> of the values of its two children, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/08/graph3drawio.png" style="width: 281px; height: 199px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [10,4,6]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The values of the root, its left child, and its right child are 10, 4, and 6, respectively.
|
||||
10 is equal to 4 + 6, so we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/08/graph3drawio-1.png" style="width: 281px; height: 199px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,1]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The values of the root, its left child, and its right child are 5, 3, and 1, respectively.
|
||||
5 is not equal to 3 + 1, so we return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The tree consists only of the root, its left child, and its right child.</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>expression</code> of the form <code>"<num1>+<num2>"</code> where <code><num1></code> and <code><num2></code> represent positive integers.</p>
|
||||
|
||||
<p>Add a pair of parentheses to <code>expression</code> such that after the addition of parentheses, <code>expression</code> is a <strong>valid</strong> mathematical expression and evaluates to the <strong>smallest</strong> possible value. The left parenthesis <strong>must</strong> be added to the left of <code>'+'</code> and the right parenthesis <strong>must</strong> be added to the right of <code>'+'</code>.</p>
|
||||
|
||||
<p>Return <code>expression</code><em> after adding a pair of parentheses such that </em><code>expression</code><em> evaluates to the <strong>smallest</strong> possible value.</em> If there are multiple answers that yield the same result, return any of them.</p>
|
||||
|
||||
<p>The input has been generated such that the original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "247+38"
|
||||
<strong>Output:</strong> "2(47+38)"
|
||||
<strong>Explanation:</strong> The <code>expression</code> evaluates to 2 * (47 + 38) = 2 * 85 = 170.
|
||||
Note that "2(4)7+38" is invalid because the right parenthesis must be to the right of the <code>'+'</code>.
|
||||
It can be shown that 170 is the smallest possible value.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "12+34"
|
||||
<strong>Output:</strong> "1(2+3)4"
|
||||
<strong>Explanation:</strong> The expression evaluates to 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "999+999"
|
||||
<strong>Output:</strong> "(999+999)"
|
||||
<strong>Explanation:</strong> The <code>expression</code> evaluates to 999 + 999 = 1998.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= expression.length <= 10</code></li>
|
||||
<li><code>expression</code> consists of digits from <code>'1'</code> to <code>'9'</code> and <code>'+'</code>.</li>
|
||||
<li><code>expression</code> starts and ends with digits.</li>
|
||||
<li><code>expression</code> contains exactly one <code>'+'</code>.</li>
|
||||
<li>The original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
Given a 2D integer array <code>nums</code> where <code>nums[i]</code> is a non-empty array of <strong>distinct</strong> positive integers, return <em>the list of integers that are present in <strong>each array</strong> of</em> <code>nums</code><em> sorted in <strong>ascending order</strong></em>.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[<u><strong>3</strong></u>,1,2,<u><strong>4</strong></u>,5],[1,2,<u><strong>3</strong></u>,<u><strong>4</strong></u>],[<u><strong>3</strong></u>,<u><strong>4</strong></u>,5,6]]
|
||||
<strong>Output:</strong> [3,4]
|
||||
<strong>Explanation:</strong>
|
||||
The only integers present in each of nums[0] = [<u><strong>3</strong></u>,1,2,<u><strong>4</strong></u>,5], nums[1] = [1,2,<u><strong>3</strong></u>,<u><strong>4</strong></u>], and nums[2] = [<u><strong>3</strong></u>,<u><strong>4</strong></u>,5,6] are 3 and 4, so we return [3,4].</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[4,5,6]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= sum(nums[i].length) <= 1000</code></li>
|
||||
<li><code>1 <= nums[i][j] <= 1000</code></li>
|
||||
<li>All the values of <code>nums[i]</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> rounds required to complete all the tasks, or </em><code>-1</code><em> if it is not possible to complete all the tasks.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [2,2,3,3,2,4,4,4,4,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> To complete all the tasks, a possible plan is:
|
||||
- In the first round, you complete 3 tasks of difficulty level 2.
|
||||
- In the second round, you complete 2 tasks of difficulty level 3.
|
||||
- In the third round, you complete 3 tasks of difficulty level 4.
|
||||
- In the fourth round, you complete 2 tasks of difficulty level 4.
|
||||
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [2,3,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the number with the value <strong>closest</strong> to </em><code>0</code><em> in </em><code>nums</code>. If there are multiple answers, return <em>the number with the <strong>largest</strong> value</em>.</p>
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-4,-2,1,4,8]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The distance from -4 to 0 is |-4| = 4.
|
||||
The distance from -2 to 0 is |-2| = 2.
|
||||
The distance from 1 to 0 is |1| = 1.
|
||||
The distance from 4 to 0 is |4| = 4.
|
||||
The distance from 8 to 0 is |8| = 8.
|
||||
Thus, the closest number to 0 in the array is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a positive integer <code>num</code>. You may swap any two digits of <code>num</code> that have the same <strong>parity</strong> (i.e. both odd digits or both even digits).</p>
|
||||
|
||||
<p>Return<em> the <strong>largest</strong> possible value of </em><code>num</code><em> after <strong>any</strong> number of swaps.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 1234
|
||||
<strong>Output:</strong> 3412
|
||||
<strong>Explanation:</strong> Swap the digit 3 with the digit 1, this results in the number 3214.
|
||||
Swap the digit 2 with the digit 4, this results in the number 3412.
|
||||
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
|
||||
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 65875
|
||||
<strong>Output:</strong> 87655
|
||||
<strong>Explanation:</strong> Swap the digit 8 with the digit 6, this results in the number 85675.
|
||||
Swap the first digit 5 with the digit 7, this results in the number 87655.
|
||||
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>
|
||||
|
||||
<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to node <code>i</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest path</strong> in the tree such that no pair of <strong>adjacent</strong> nodes on the path have the same character assigned to them.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/testingdrawio.png" style="width: 201px; height: 241px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> parent = [-1,0,0,1,1,2], s = "abacbe"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.
|
||||
It can be proven that there is no longer path that satisfies the conditions.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/graph2drawio.png" style="width: 201px; height: 221px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> parent = [-1,0,0,0], s = "aabc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == parent.length == s.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= parent[i] <= n - 1</code> for all <code>i >= 1</code></li>
|
||||
<li><code>parent[0] == -1</code></li>
|
||||
<li><code>parent</code> represents a valid tree.</li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a 2D integer array <code>rectangles</code> where <code>rectangles[i] = [l<sub>i</sub>, h<sub>i</sub>]</code> indicates that <code>i<sup>th</sup></code> rectangle has a length of <code>l<sub>i</sub></code> and a height of <code>h<sub>i</sub></code>. You are also given a 2D integer array <code>points</code> where <code>points[j] = [x<sub>j</sub>, y<sub>j</sub>]</code> is a point with coordinates <code>(x<sub>j</sub>, y<sub>j</sub>)</code>.</p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> rectangle has its <strong>bottom-left corner</strong> point at the coordinates <code>(0, 0)</code> and its <strong>top-right corner</strong> point at <code>(l<sub>i</sub>, h<sub>i</sub>)</code>.</p>
|
||||
|
||||
<p>Return<em> an integer array </em><code>count</code><em> of length </em><code>points.length</code><em> where </em><code>count[j]</code><em> is the number of rectangles that <strong>contain</strong> the </em><code>j<sup>th</sup></code><em> point.</em></p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> rectangle <strong>contains</strong> the <code>j<sup>th</sup></code> point if <code>0 <= x<sub>j</sub> <= l<sub>i</sub></code> and <code>0 <= y<sub>j</sub> <= h<sub>i</sub></code>. Note that points that lie on the <strong>edges</strong> of a rectangle are also considered to be contained by that rectangle.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/example1.png" style="width: 300px; height: 509px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[1,2],[2,3],[2,5]], points = [[2,1],[1,4]]
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong>
|
||||
The first rectangle contains no points.
|
||||
The second rectangle contains only the point (2, 1).
|
||||
The third rectangle contains the points (2, 1) and (1, 4).
|
||||
The number of rectangles that contain the point (2, 1) is 2.
|
||||
The number of rectangles that contain the point (1, 4) is 1.
|
||||
Therefore, we return [2, 1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/example2.png" style="width: 300px; height: 312px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[1,1],[2,2],[3,3]], points = [[1,3],[1,1]]
|
||||
<strong>Output:</strong> [1,3]
|
||||
<strong>Explanation:
|
||||
</strong>The first rectangle contains only the point (1, 1).
|
||||
The second rectangle contains only the point (1, 1).
|
||||
The third rectangle contains the points (1, 3) and (1, 1).
|
||||
The number of rectangles that contain the point (1, 3) is 1.
|
||||
The number of rectangles that contain the point (1, 1) is 3.
|
||||
Therefore, we return [1, 3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= rectangles.length, points.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>rectangles[i].length == points[j].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub>, x<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= h<sub>i</sub>, y<sub>j</sub> <= 100</code></li>
|
||||
<li>All the <code>rectangles</code> are <strong>unique</strong>.</li>
|
||||
<li>All the <code>points</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>Given a 2D integer array <code>circles</code> where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> represents the center <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code> of the <code>i<sup>th</sup></code> circle drawn on a grid, return <em>the <strong>number of lattice points</strong> </em><em>that are present inside <strong>at least one</strong> circle</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>lattice point</strong> is a point with integer coordinates.</li>
|
||||
<li>Points that lie <strong>on the circumference of a circle</strong> are also considered to be inside it.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-11.png" style="width: 300px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> circles = [[2,2,1]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows the given circle.
|
||||
The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.
|
||||
Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.
|
||||
Hence, the number of lattice points present inside at least one circle is 5.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-22.png" style="width: 300px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> circles = [[2,2,2],[3,4,1]]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows the given circles.
|
||||
There are exactly 16 lattice points which are present inside at least one circle.
|
||||
Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= circles.length <= 200</code></li>
|
||||
<li><code>circles[i].length == 3</code></li>
|
||||
<li><code>1 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
||||
<li><code>1 <= r<sub>i</sub> <= min(x<sub>i</sub>, y<sub>i</sub>)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>scores</code> of length <code>n</code> where <code>scores[i]</code> denotes the score of node <code>i</code>. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>A node sequence is <b>valid</b> if it meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>There is an edge connecting every pair of <strong>adjacent</strong> nodes in the sequence.</li>
|
||||
<li>No node appears more than once in the sequence.</li>
|
||||
</ul>
|
||||
|
||||
<p>The score of a node sequence is defined as the <strong>sum</strong> of the scores of the nodes in the sequence.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum score</strong> of a valid node sequence with a length of </em><code>4</code><em>. </em>If no such sequence exists, return<em> </em><code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/15/ex1new3.png" style="width: 290px; height: 215px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
|
||||
<strong>Output:</strong> 24
|
||||
<strong>Explanation:</strong> The figure above shows the graph and the chosen node sequence [0,1,2,3].
|
||||
The score of the node sequence is 5 + 2 + 9 + 8 = 24.
|
||||
It can be shown that no other node sequence has a score of more than 24.
|
||||
Note that the sequences [3,1,2,0] and [1,0,2,3] are also valid and have a score of 24.
|
||||
The sequence [0,3,2,4] is not valid since no edge connects nodes 0 and 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/17/ex2.png" style="width: 333px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The figure above shows the graph.
|
||||
There are no valid node sequences of length 4, so we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == scores.length</code></li>
|
||||
<li><code>4 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= scores[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no duplicate edges.</li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>Alice is a caretaker of <code>n</code> gardens and she wants to plant flowers to maximize the total beauty of all her gardens.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>flowers</code> of size <code>n</code>, where <code>flowers[i]</code> is the number of flowers already planted in the <code>i<sup>th</sup></code> garden. Flowers that are already planted <strong>cannot</strong> be removed. You are then given another integer <code>newFlowers</code>, which is the <strong>maximum</strong> number of flowers that Alice can additionally plant. You are also given the integers <code>target</code>, <code>full</code>, and <code>partial</code>.</p>
|
||||
|
||||
<p>A garden is considered <strong>complete</strong> if it has <strong>at least</strong> <code>target</code> flowers. The <strong>total beauty</strong> of the gardens is then determined as the <strong>sum</strong> of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number of <strong>complete</strong> gardens multiplied by <code>full</code>.</li>
|
||||
<li>The <strong>minimum</strong> number of flowers in any of the <strong>incomplete</strong> gardens multiplied by <code>partial</code>. If there are no incomplete gardens, then this value will be <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> total beauty that Alice can obtain after planting at most </em><code>newFlowers</code><em> flowers.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> Alice can plant
|
||||
- 2 flowers in the 0<sup>th</sup> garden
|
||||
- 3 flowers in the 1<sup>st</sup> garden
|
||||
- 1 flower in the 2<sup>nd</sup> garden
|
||||
- 1 flower in the 3<sup>rd</sup> garden
|
||||
The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers.
|
||||
There is 1 garden that is complete.
|
||||
The minimum number of flowers in the incomplete gardens is 2.
|
||||
Thus, the total beauty is 1 * 12 + 2 * 1 = 12 + 2 = 14.
|
||||
No other way of planting flowers can obtain a total beauty higher than 14.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> Alice can plant
|
||||
- 3 flowers in the 0<sup>th</sup> garden
|
||||
- 0 flowers in the 1<sup>st</sup> garden
|
||||
- 0 flowers in the 2<sup>nd</sup> garden
|
||||
- 2 flowers in the 3<sup>rd</sup> garden
|
||||
The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers.
|
||||
There are 3 gardens that are complete.
|
||||
The minimum number of flowers in the incomplete gardens is 4.
|
||||
Thus, the total beauty is 3 * 2 + 4 * 6 = 6 + 24 = 30.
|
||||
No other way of planting flowers can obtain a total beauty higher than 30.
|
||||
Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= flowers.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= flowers[i], target <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= newFlowers <= 10<sup>10</sup></code></li>
|
||||
<li><code>1 <= full, partial <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>flowers</code>, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in <strong>full bloom</strong> from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (<strong>inclusive</strong>). You are also given a <strong>0-indexed</strong> integer array <code>persons</code> of size <code>n</code>, where <code>persons[i]</code> is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>n</code><em>, where </em><code>answer[i]</code><em> is the <strong>number</strong> of flowers that are in full bloom when the </em><code>i<sup>th</sup></code><em> person arrives.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg" style="width: 550px; height: 216px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
|
||||
<strong>Output:</strong> [1,2,2,2]
|
||||
<strong>Explanation: </strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.
|
||||
For each person, we return the number of flowers in full bloom during their arrival.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg" style="width: 450px; height: 195px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [[1,10],[3,3]], persons = [3,3,2]
|
||||
<strong>Output:</strong> [2,2,1]
|
||||
<strong>Explanation:</strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.
|
||||
For each person, we return the number of flowers in full bloom during their arrival.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= flowers.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>flowers[i].length == 2</code></li>
|
||||
<li><code>1 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= persons.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= persons[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a string <code>s</code> consisting of digits and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A <strong>round</strong> can be completed if the length of <code>s</code> is greater than <code>k</code>. In one round, do the following:</p>
|
||||
|
||||
<ol>
|
||||
<li><strong>Divide</strong> <code>s</code> into <strong>consecutive groups</strong> of size <code>k</code> such that the first <code>k</code> characters are in the first group, the next <code>k</code> characters are in the second group, and so on. <strong>Note</strong> that the size of the last group can be smaller than <code>k</code>.</li>
|
||||
<li><strong>Replace</strong> each group of <code>s</code> with a string representing the sum of all its digits. For example, <code>"346"</code> is replaced with <code>"13"</code> because <code>3 + 4 + 6 = 13</code>.</li>
|
||||
<li><strong>Merge</strong> consecutive groups together to form a new string. If the length of the string is greater than <code>k</code>, repeat from step <code>1</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <code>s</code> <em>after all rounds have been completed</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11111222223", k = 3
|
||||
<strong>Output:</strong> "135"
|
||||
<strong>Explanation:</strong>
|
||||
- For the first round, we divide s into groups of size 3: "111", "112", "222", and "23".
|
||||
Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
|
||||
So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round.
|
||||
- For the second round, we divide s into "346" and "5".
|
||||
Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
|
||||
So, s becomes "13" + "5" = "135" after second round.
|
||||
Now, s.length <= k, so we return "135" as the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "00000000", k = 3
|
||||
<strong>Output:</strong> "000"
|
||||
<strong>Explanation:</strong>
|
||||
We divide s into "000", "000", and "00".
|
||||
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
|
||||
s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>2 <= k <= 100</code></li>
|
||||
<li><code>s</code> consists of digits only.</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>There is an ATM machine that stores banknotes of <code>5</code> denominations: <code>20</code>, <code>50</code>, <code>100</code>, <code>200</code>, and <code>500</code> dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money.</p>
|
||||
|
||||
<p>When withdrawing, the machine prioritizes using banknotes of <strong>larger</strong> values.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if you want to withdraw <code>$300</code> and there are <code>2</code> <code>$50</code> banknotes, <code>1</code> <code>$100</code> banknote, and <code>1</code> <code>$200</code> banknote, then the machine will use the <code>$100</code> and <code>$200</code> banknotes.</li>
|
||||
<li>However, if you try to withdraw <code>$600</code> and there are <code>3</code> <code>$200</code> banknotes and <code>1</code> <code>$500</code> banknote, then the withdraw request will be rejected because the machine will first try to use the <code>$500</code> banknote and then be unable to use banknotes to complete the remaining <code>$100</code>. Note that the machine is <strong>not</strong> allowed to use the <code>$200</code> banknotes instead of the <code>$500</code> banknote.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the ATM class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>ATM()</code> Initializes the ATM object.</li>
|
||||
<li><code>void deposit(int[] banknotesCount)</code> Deposits new banknotes in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>.</li>
|
||||
<li><code>int[] withdraw(int amount)</code> Returns an array of length <code>5</code> of the number of banknotes that will be handed to the user in the order <code>$20</code>, <code>$50</code>, <code>$100</code>, <code>$200</code>, and <code>$500</code>, and update the number of banknotes in the ATM after withdrawing. Returns <code>[-1]</code> if it is not possible (do <strong>not</strong> withdraw any banknotes in this case).</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"]
|
||||
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
|
||||
<strong>Output</strong>
|
||||
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
ATM atm = new ATM();
|
||||
atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes,
|
||||
// and 1 $500 banknote.
|
||||
atm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote
|
||||
// and 1 $500 banknote. The banknotes left over in the
|
||||
// machine are [0,0,0,2,0].
|
||||
atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote.
|
||||
// The banknotes in the machine are now [0,1,0,3,1].
|
||||
atm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote
|
||||
// and then be unable to complete the remaining $100,
|
||||
// so the withdraw request will be rejected.
|
||||
// Since the request is rejected, the number of banknotes
|
||||
// in the machine is not modified.
|
||||
atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote
|
||||
// and 1 $500 banknote.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>banknotesCount.length == 5</code></li>
|
||||
<li><code>0 <= banknotesCount[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= amount <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>5000</code> calls <strong>in total</strong> will be made to <code>withdraw</code> and <code>deposit</code>.</li>
|
||||
<li>At least <strong>one</strong> call will be made to each function <code>withdraw</code> and <code>deposit</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a 2D integer array <code>grid</code> of size <code>m x n</code>, where each cell contains a positive integer.</p>
|
||||
|
||||
<p>A <strong>cornered path</strong> is defined as a set of adjacent cells with <strong>at most</strong> one turn. More specifically, the path should exclusively move either <strong>horizontally</strong> or <strong>vertically</strong> up to the turn (if there is one), without returning to a previously visited cell. After the turn, the path will then move exclusively in the <strong>alternate</strong> direction: move vertically if it moved horizontally, and vice versa, also without returning to a previously visited cell.</p>
|
||||
|
||||
<p>The <strong>product</strong> of a path is defined as the product of all the values in the path.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of <strong>trailing zeros</strong> in the product of a cornered path found in </em><code>grid</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Horizontal</strong> movement means moving in either the left or right direction.</li>
|
||||
<li><strong>Vertical</strong> movement means moving in either the up or down direction.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/23/ex1new2.jpg" style="width: 577px; height: 190px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The grid on the left shows a valid cornered path.
|
||||
It has a product of 15 * 20 * 6 * 1 * 10 = 18000 which has 3 trailing zeros.
|
||||
It can be shown that this is the maximum trailing zeros in the product of a cornered path.
|
||||
|
||||
The grid in the middle is not a cornered path as it has more than one turn.
|
||||
The grid on the right is not a cornered path as it requires a return to a previously visited cell.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/ex2.jpg" style="width: 150px; height: 157px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[4,3,2],[7,6,1],[8,8,8]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The grid is shown in the figure above.
|
||||
There are no cornered paths in the grid that result in a product with a trailing zero.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grid[i][j] <= 1000</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user