mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-19 20:16:48 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2376",
|
||||
"questionFrontendId": "2238",
|
||||
"boundTopicId": null,
|
||||
"title": "Number of Times a Driver Was a Passenger",
|
||||
"titleSlug": "number-of-times-a-driver-was-a-passenger",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 12,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Hopper Company Queries I\", \"titleSlug\": \"hopper-company-queries-i\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Hopper Company Queries II\", \"titleSlug\": \"hopper-company-queries-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Hopper Company Queries III\", \"titleSlug\": \"hopper-company-queries-iii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": null,
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"397\", \"totalSubmission\": \"503\", \"totalAcceptedRaw\": 397, \"totalSubmissionRaw\": 503, \"acRate\": \"78.9%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Rides\":[\"ride_id\",\"driver_id\",\"passenger_id\"]},\"rows\":{\"Rides\":[[1,7,1],[2,7,2],[3,11,1],[4,11,7],[5,11,7],[6,11,3]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Rides (ride_id int, driver_id int, passenger_id int)\"\n ],\n \"mssql\": [\n \"Create table Rides (ride_id int, driver_id int, passenger_id int)\"\n ],\n \"oraclesql\": [\n \"Create table Rides (ride_id int, driver_id int, passenger_id int)\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Rides (ride_id int, driver_id int, passenger_id int)",
|
||||
"Truncate table Rides",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('1', '7', '1')",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('2', '7', '2')",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('3', '11', '1')",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('4', '11', '7')",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('5', '11', '7')",
|
||||
"insert into Rides (ride_id, driver_id, passenger_id) values ('6', '11', '3')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2371",
|
||||
"questionFrontendId": "2230",
|
||||
"boundTopicId": null,
|
||||
"title": "The Users That Are Eligible for Discount",
|
||||
"titleSlug": "the-users-that-are-eligible-for-discount",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 5,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"The Number of Users That Are Eligible for Discount\", \"titleSlug\": \"the-number-of-users-that-are-eligible-for-discount\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": null,
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"466\", \"totalSubmission\": \"925\", \"totalAcceptedRaw\": 466, \"totalSubmissionRaw\": 925, \"acRate\": \"50.4%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Purchases\": [\"user_id\", \"time_stamp\", \"amount\"]}, \"startDate\": \"2022-03-08\", \"endDate\": \"2022-03-20\", \"minAmount\": 1000, \"rows\": {\"Purchases\": [[1, \"2022-04-20 09:03:00\", 4416], [2, \"2022-03-19 19:24:02\", 678], [3, \"2022-03-18 12:03:09\", 4523], [3, \"2022-03-30 09:43:42\", 626]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Purchases (user_id int, time_stamp datetime, amount int)\"\n ],\n \"mssql\": [\n \"Create table Purchases (user_id int, time_stamp datetime, amount int)\"\n ],\n \"oraclesql\": [\n \"Create table Purchases (user_id int, time_stamp date, amount int)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD HH24:MI:SS'\"\n ],\n \"database\": true,\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Purchases (user_id int, time_stamp datetime, amount int)",
|
||||
"Truncate table Purchases",
|
||||
"insert into Purchases (user_id, time_stamp, amount) values ('1', '2022-04-20 09:03:00', '4416')",
|
||||
"insert into Purchases (user_id, time_stamp, amount) values ('2', '2022-03-19 19:24:02', '678')",
|
||||
"insert into Purchases (user_id, time_stamp, amount) values ('3', '2022-03-18 12:03:09', '4523')",
|
||||
"insert into Purchases (user_id, time_stamp, amount) values ('3', '2022-03-30 09:43:42', '626')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2370",
|
||||
"questionFrontendId": "2228",
|
||||
"boundTopicId": null,
|
||||
"title": "Users With Two Purchases Within Seven Days",
|
||||
"titleSlug": "users-with-two-purchases-within-seven-days",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 8,
|
||||
"dislikes": 3,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Biggest Window Between Visits\", \"titleSlug\": \"biggest-window-between-visits\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{\"headers\":{\"Purchases\":[\"purchase_id\",\"user_id\",\"purchase_date\"]},\"rows\":{\"Purchases\":[[4,2,\"2022-03-13\"],[1,5,\"2022-02-11\"],[3,7,\"2022-06-19\"],[6,2,\"2022-03-20\"],[5,7,\"2022-06-19\"],[2,2,\"2022-06-08\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"657\", \"totalSubmission\": \"1.4K\", \"totalAcceptedRaw\": 657, \"totalSubmissionRaw\": 1363, \"acRate\": \"48.2%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Purchases\":[\"purchase_id\",\"user_id\",\"purchase_date\"]},\"rows\":{\"Purchases\":[[4,2,\"2022-03-13\"],[1,5,\"2022-02-11\"],[3,7,\"2022-06-19\"],[6,2,\"2022-03-20\"],[5,7,\"2022-06-19\"],[2,2,\"2022-06-08\"]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Purchases (purchase_id int, user_id int, purchase_date date)\"\n ],\n \"mssql\": [\n \"Create table Purchases (purchase_id int, user_id int, purchase_date date)\"\n ],\n \"oraclesql\": [\n \"Create table Purchases (purchase_id int, user_id int, purchase_date date)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Purchases (purchase_id int, user_id int, purchase_date date)",
|
||||
"Truncate table Purchases",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('4', '2', '2022-03-13')",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('1', '5', '2022-02-11')",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('3', '7', '2022-06-19')",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('6', '2', '2022-03-20')",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('5', '7', '2022-06-19')",
|
||||
"insert into Purchases (purchase_id, user_id, purchase_date) values ('2', '2', '2022-06-08')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
159
leetcode/originData/add-two-integers.json
Normal file
159
leetcode/originData/add-two-integers.json
Normal file
File diff suppressed because one or more lines are too long
167
leetcode/originData/calculate-digit-sum-of-a-string.json
Normal file
167
leetcode/originData/calculate-digit-sum-of-a-string.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/design-an-atm-machine.json
Normal file
174
leetcode/originData/design-an-atm-machine.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/find-closest-number-to-zero.json
Normal file
162
leetcode/originData/find-closest-number-to-zero.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
175
leetcode/originData/maximum-product-after-k-increments.json
Normal file
175
leetcode/originData/maximum-product-after-k-increments.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/maximum-score-of-a-node-sequence.json
Normal file
182
leetcode/originData/maximum-score-of-a-node-sequence.json
Normal file
File diff suppressed because one or more lines are too long
188
leetcode/originData/maximum-total-beauty-of-the-gardens.json
Normal file
188
leetcode/originData/maximum-total-beauty-of-the-gardens.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
181
leetcode/originData/minimum-rounds-to-complete-all-tasks.json
Normal file
181
leetcode/originData/minimum-rounds-to-complete-all-tasks.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/number-of-ways-to-buy-pens-and-pencils.json
Normal file
168
leetcode/originData/number-of-ways-to-buy-pens-and-pencils.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode/originData/root-equals-sum-of-children.json
Normal file
165
leetcode/originData/root-equals-sum-of-children.json
Normal file
File diff suppressed because one or more lines are too long
24
leetcode/problem/add-two-integers.html
Normal file
24
leetcode/problem/add-two-integers.html
Normal file
@@ -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>
|
47
leetcode/problem/calculate-digit-sum-of-a-string.html
Normal file
47
leetcode/problem/calculate-digit-sum-of-a-string.html
Normal file
@@ -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>
|
54
leetcode/problem/design-an-atm-machine.html
Normal file
54
leetcode/problem/design-an-atm-machine.html
Normal file
@@ -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>
|
31
leetcode/problem/find-closest-number-to-zero.html
Normal file
31
leetcode/problem/find-closest-number-to-zero.html
Normal file
@@ -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>
|
34
leetcode/problem/maximum-product-after-k-increments.html
Normal file
34
leetcode/problem/maximum-product-after-k-increments.html
Normal file
@@ -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>
|
50
leetcode/problem/maximum-score-of-a-node-sequence.html
Normal file
50
leetcode/problem/maximum-score-of-a-node-sequence.html
Normal file
@@ -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>
|
58
leetcode/problem/maximum-total-beauty-of-the-gardens.html
Normal file
58
leetcode/problem/maximum-total-beauty-of-the-gardens.html
Normal file
@@ -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,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>
|
@@ -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>
|
33
leetcode/problem/minimum-rounds-to-complete-all-tasks.html
Normal file
33
leetcode/problem/minimum-rounds-to-complete-all-tasks.html
Normal file
@@ -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>
|
31
leetcode/problem/number-of-ways-to-buy-pens-and-pencils.html
Normal file
31
leetcode/problem/number-of-ways-to-buy-pens-and-pencils.html
Normal file
@@ -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>
|
30
leetcode/problem/root-equals-sum-of-children.html
Normal file
30
leetcode/problem/root-equals-sum-of-children.html
Normal file
@@ -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>
|
Reference in New Issue
Block a user