mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-03 06:22:54 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
175
leetcode/originData/alternating-groups-iii.json
Normal file
175
leetcode/originData/alternating-groups-iii.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
192
leetcode/originData/design-neighbor-sum-service.json
Normal file
192
leetcode/originData/design-neighbor-sum-service.json
Normal file
File diff suppressed because one or more lines are too long
173
leetcode/originData/find-if-digit-game-can-be-won.json
Normal file
173
leetcode/originData/find-if-digit-game-can-be-won.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
180
leetcode/originData/find-the-number-of-winning-players.json
Normal file
180
leetcode/originData/find-the-number-of-winning-players.json
Normal file
File diff suppressed because one or more lines are too long
181
leetcode/originData/find-the-winning-player-in-coin-game.json
Normal file
181
leetcode/originData/find-the-winning-player-in-coin-game.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
189
leetcode/originData/maximum-score-from-grid-operations.json
Normal file
189
leetcode/originData/maximum-score-from-grid-operations.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
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
File diff suppressed because one or more lines are too long
90
leetcode/originData/odd-and-even-transactions.json
Normal file
90
leetcode/originData/odd-and-even-transactions.json
Normal file
@@ -0,0 +1,90 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3530",
|
||||
"questionFrontendId": "3220",
|
||||
"boundTopicId": null,
|
||||
"title": "Odd and Even Transactions",
|
||||
"titleSlug": "odd-and-even-transactions",
|
||||
"content": "<p>Table: <code>transactions</code></p>\n\n<pre>\n+------------------+------+\n| Column Name | Type | \n+------------------+------+\n| transaction_id | int |\n| amount | int |\n| transaction_date | date |\n+------------------+------+\nThe transactions_id column uniquely identifies each row in this table.\nEach row of this table contains the transaction id, amount and transaction date.\n</pre>\n\n<p>Write a solution to find the <strong>sum of amounts</strong> for <strong>odd</strong> and <strong>even</strong> transactions for each day. If there are no odd or even transactions for a specific date, display as <code>0</code>.</p>\n\n<p>Return <em>the result table ordered by</em> <code>transaction_date</code> <em>in <strong>ascending</strong> order</em>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p><code>transactions</code> table:</p>\n\n<pre class=\"example-io\">\n+----------------+--------+------------------+\n| transaction_id | amount | transaction_date |\n+----------------+--------+------------------+\n| 1 | 150 | 2024-07-01 |\n| 2 | 200 | 2024-07-01 |\n| 3 | 75 | 2024-07-01 |\n| 4 | 300 | 2024-07-02 |\n| 5 | 50 | 2024-07-02 |\n| 6 | 120 | 2024-07-03 |\n+----------------+--------+------------------+\n </pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+------------------+---------+----------+\n| transaction_date | odd_sum | even_sum |\n+------------------+---------+----------+\n| 2024-07-01 | 75 | 350 |\n| 2024-07-02 | 0 | 350 |\n| 2024-07-03 | 0 | 120 |\n+------------------+---------+----------+\n </pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li>For transaction dates:\n\t<ul>\n\t\t<li>2024-07-01:\n\t\t<ul>\n\t\t\t<li>Sum of amounts for odd transactions: 75</li>\n\t\t\t<li>Sum of amounts for even transactions: 150 + 200 = 350</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>2024-07-02:\n\t\t<ul>\n\t\t\t<li>Sum of amounts for odd transactions: 0</li>\n\t\t\t<li>Sum of amounts for even transactions: 300 + 50 = 350</li>\n\t\t</ul>\n\t\t</li>\n\t\t<li>2024-07-03:\n\t\t<ul>\n\t\t\t<li>Sum of amounts for odd transactions: 0</li>\n\t\t\t<li>Sum of amounts for even transactions: 120</li>\n\t\t</ul>\n\t\t</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p><strong>Note:</strong> The output table is ordered by <code>transaction_date</code> in ascending order.</p>\n</div>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 16,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\":{\"transactions\":[\"transaction_id\",\"amount\",\"transaction_date\"]},\"rows\":{\"transactions\":[[1,150,\"2024-07-01\"],[2,200,\"2024-07-01\"],[3,75,\"2024-07-01\"],[4,300,\"2024-07-02\"],[5,50,\"2024-07-02\"],[6,120,\"2024-07-03\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "MySQL",
|
||||
"langSlug": "mysql",
|
||||
"code": "# Write your MySQL query statement below\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "MS SQL Server",
|
||||
"langSlug": "mssql",
|
||||
"code": "/* Write your T-SQL query statement below */\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "Oracle",
|
||||
"langSlug": "oraclesql",
|
||||
"code": "/* Write your PL/SQL query statement below */\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "Pandas",
|
||||
"langSlug": "pythondata",
|
||||
"code": "import pandas as pd\n\ndef sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame:\n ",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "PostgreSQL",
|
||||
"langSlug": "postgresql",
|
||||
"code": "-- Write your PostgreSQL query statement below\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"2.3K\", \"totalSubmission\": \"3.3K\", \"totalAcceptedRaw\": 2337, \"totalSubmissionRaw\": 3253, \"acRate\": \"71.8%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"transactions\":[\"transaction_id\",\"amount\",\"transaction_date\"]},\"rows\":{\"transactions\":[[1,150,\"2024-07-01\"],[2,200,\"2024-07-01\"],[3,75,\"2024-07-01\"],[4,300,\"2024-07-02\"],[5,50,\"2024-07-02\"],[6,120,\"2024-07-03\"]]}}",
|
||||
"metaData": "{\"mysql\": [\"Create table if not exists transactions ( transaction_id int, amount int, transaction_date date)\"], \"mssql\": [\"Create table transactions ( transaction_id int, amount int, transaction_date date)\"], \"oraclesql\": [\"Create table transactions ( transaction_id int, amount int, transaction_date date)\", \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"], \"database\": true, \"name\": \"sum_daily_odd_even\", \"postgresql\": [\"CREATE TABLE IF NOT EXISTS transactions (\\n transaction_id int,\\n amount int,\\n transaction_date date\\n);\\n\"], \"pythondata\": [\"transactions = pd.DataFrame(\\n columns=[\\\"transaction_id\\\", \\\"amount\\\", \\\"transaction_date\\\"],\\n dtype={\\n \\\"transaction_id\\\": \\\"int\\\",\\n \\\"amount\\\": \\\"int\\\",\\n \\\"transaction_date\\\": \\\"datetime64[ns]\\\",\\n },\\n)\\n\"], \"database_schema\": {\"transactions\": {\"transaction_id\": \"INT\", \"amount\": \"INT\", \"transaction_date\": \"DATE\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table if not exists transactions ( transaction_id int, amount int, transaction_date date)",
|
||||
"Truncate table transactions",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('1', '150', '2024-07-01')",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('2', '200', '2024-07-01')",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('3', '75', '2024-07-01')",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('4', '300', '2024-07-02')",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('5', '50', '2024-07-02')",
|
||||
"insert into transactions (transaction_id, amount, transaction_date) values ('6', '120', '2024-07-03')"
|
||||
],
|
||||
"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>\"], \"pythondata\": [\"Pandas\", \"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0</p>\"], \"postgresql\": [\"PostgreSQL\", \"<p>PostgreSQL 16</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
187
leetcode/originData/time-taken-to-mark-all-nodes.json
Normal file
187
leetcode/originData/time-taken-to-mark-all-nodes.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/vowels-game-in-a-string.json
Normal file
187
leetcode/originData/vowels-game-in-a-string.json
Normal file
File diff suppressed because one or more lines are too long
84
leetcode/problem/alternating-groups-iii.html
Normal file
84
leetcode/problem/alternating-groups-iii.html
Normal file
@@ -0,0 +1,84 @@
|
||||
<p>There are some red and blue tiles arranged circularly. You are given an array of integers <code>colors</code> and a 2D integers array <code>queries</code>.</p>
|
||||
|
||||
<p>The color of tile <code>i</code> is represented by <code>colors[i]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>colors[i] == 0</code> means that tile <code>i</code> is <strong>red</strong>.</li>
|
||||
<li><code>colors[i] == 1</code> means that tile <code>i</code> is <strong>blue</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>An <strong>alternating</strong> group is a contiguous subset of tiles in the circle with <strong>alternating</strong> colors (each tile in the group except the first and last one has a different color from its <b>adjacent</b> tiles in the group).</p>
|
||||
|
||||
<p>You have to process queries of two types:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>queries[i] = [1, size<sub>i</sub>]</code>, determine the count of <strong>alternating</strong> groups with size <code>size<sub>i</sub></code>.</li>
|
||||
<li><code>queries[i] = [2, index<sub>i</sub>, color<sub>i</sub>]</code>, change <code>colors[index<sub>i</sub>]</code> to <code>color<font face="monospace"><sub>i</sub></font></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array <code>answer</code> containing the results of the queries of the first type <em>in order</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that since <code>colors</code> represents a <strong>circle</strong>, the <strong>first</strong> and the <strong>last</strong> tiles are considered to be next to each other.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-14-44.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></strong></p>
|
||||
|
||||
<p>First query:</p>
|
||||
|
||||
<p>Change <code>colors[1]</code> to 0.</p>
|
||||
|
||||
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-20-25.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
|
||||
|
||||
<p>Second query:</p>
|
||||
|
||||
<p>Count of the alternating groups with size 4:</p>
|
||||
|
||||
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-25-02-2.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-24-12.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-35-50.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
|
||||
|
||||
<p>First query:</p>
|
||||
|
||||
<p>Count of the alternating groups with size 3:</p>
|
||||
|
||||
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-37-13.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-from-2024-06-03-20-36-40.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
|
||||
|
||||
<p>Second query: <code>colors</code> will not change.</p>
|
||||
|
||||
<p>Third query: There is no alternating group with size 5.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= colors.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= colors[i] <= 1</code></li>
|
||||
<li><code>1 <= queries.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li>
|
||||
<li>For all <code>i</code> that:
|
||||
<ul>
|
||||
<li><code>queries[i][0] == 1</code>: <code>queries[i].length == 2</code>, <code>3 <= queries[i][1] <= colors.length - 1</code></li>
|
||||
<li><code>queries[i][0] == 2</code>: <code>queries[i].length == 3</code>, <code>0 <= queries[i][1] <= colors.length - 1</code>, <code>0 <= queries[i][2] <= 1</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
@@ -0,0 +1,70 @@
|
||||
<p>You are given two positive integers <code>X</code> and <code>Y</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p>
|
||||
|
||||
<p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(X, Y)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p>
|
||||
|
||||
<p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">X = 3, Y = 4, circles = [[2,1,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example2circle1.png" style="width: 346px; height: 264px;" /></p>
|
||||
|
||||
<p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">X = 3, Y = 3, circles = [[1,1,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example1circle.png" style="width: 346px; height: 264px;" /></p>
|
||||
|
||||
<p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">X = 3, Y = 3, circles = [[2,1,1],[1,2,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example0circle.png" style="width: 346px; height: 264px;" /></p>
|
||||
|
||||
<p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">X = 4, Y = 4, circles = [[5,5,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/04/rectangles.png" style="width: 346px; height: 264px;" /></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= X, Y <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= circles.length <= 1000</code></li>
|
||||
<li><code>circles[i].length == 3</code></li>
|
||||
<li><code>1 <= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,138 @@
|
||||
<p>You are given a binary string <code>s</code>.</p>
|
||||
|
||||
<p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p>
|
||||
|
||||
<p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>square</strong> of the number of zeros in the string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "00011"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substrings with dominant ones are shown in the table below.</p>
|
||||
</div>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>i</th>
|
||||
<th>j</th>
|
||||
<th>s[i..j]</th>
|
||||
<th>Number of Zeros</th>
|
||||
<th>Number of Ones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>3</td>
|
||||
<td>3</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>4</td>
|
||||
<td>4</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>3</td>
|
||||
<td>01</td>
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>3</td>
|
||||
<td>4</td>
|
||||
<td>11</td>
|
||||
<td>0</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>4</td>
|
||||
<td>011</td>
|
||||
<td>1</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "101101"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">16</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substrings with <strong>non-dominant</strong> ones are shown in the table below.</p>
|
||||
|
||||
<p>Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.</p>
|
||||
</div>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>i</th>
|
||||
<th>j</th>
|
||||
<th>s[i..j]</th>
|
||||
<th>Number of Zeros</th>
|
||||
<th>Number of Ones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>4</td>
|
||||
<td>4</td>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>4</td>
|
||||
<td>0110</td>
|
||||
<td>2</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>0</td>
|
||||
<td>4</td>
|
||||
<td>10110</td>
|
||||
<td>2</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>5</td>
|
||||
<td>01101</td>
|
||||
<td>2</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists only of characters <code>'0'</code> and <code>'1'</code>.</li>
|
||||
</ul>
|
67
leetcode/problem/design-neighbor-sum-service.html
Normal file
67
leetcode/problem/design-neighbor-sum-service.html
Normal file
@@ -0,0 +1,67 @@
|
||||
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p>
|
||||
|
||||
<p>Implement the <code>neighborSum</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>neighborSum(int [][]grid)</code> initializes the object.</li>
|
||||
<li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li>
|
||||
<li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/design.png" style="width: 400px; height: 248px;" /></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong></p>
|
||||
|
||||
<p>["neighborSum", "adjacentSum", "adjacentSum", "diagonalSum", "diagonalSum"]</p>
|
||||
|
||||
<p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p>
|
||||
|
||||
<p><strong>Output:</strong> [null, 6, 16, 16, 4]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/designexample0.png" style="width: 250px; height: 249px;" /></strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The adjacent neighbors of 1 are 0, 2, and 4.</li>
|
||||
<li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li>
|
||||
<li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li>
|
||||
<li>The diagonal neighbor of 8 is 4.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong></p>
|
||||
|
||||
<p>["neighborSum", "adjacentSum", "diagonalSum"]</p>
|
||||
|
||||
<p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p>
|
||||
|
||||
<p><strong>Output:</strong> [null, 23, 45]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/designexample2.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li>
|
||||
<li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == grid.length == grid[0].length <= 10</code></li>
|
||||
<li><code>0 <= grid[i][j] <= n<sup>2</sup> - 1</code></li>
|
||||
<li>All <code>grid[i][j]</code> are distinct.</li>
|
||||
<li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li>
|
||||
<li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li>
|
||||
</ul>
|
50
leetcode/problem/find-if-digit-game-can-be-won.html
Normal file
50
leetcode/problem/find-if-digit-game-can-be-won.html
Normal file
@@ -0,0 +1,50 @@
|
||||
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||
|
||||
<p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob's numbers.</p>
|
||||
|
||||
<p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Alice cannot win by choosing either single-digit or double-digit numbers.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 99</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p>
|
||||
|
||||
<p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li>
|
||||
<li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are no special numbers in the range <code>[5, 7]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">11</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= l <= r <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
62
leetcode/problem/find-the-number-of-winning-players.html
Normal file
62
leetcode/problem/find-the-number-of-winning-players.html
Normal file
@@ -0,0 +1,62 @@
|
||||
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p>
|
||||
|
||||
<ul>
|
||||
<li>Player 0 wins if they pick any ball.</li>
|
||||
<li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li>
|
||||
<li>...</li>
|
||||
<li>Player <code>i</code> wins if they pick at least<code>i + 1</code> balls of the <em>same</em> color.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the number of players who <strong>win</strong> the game.</p>
|
||||
|
||||
<p><strong>Note</strong> that <em>multiple</em> players can win the game.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>No player wins the game.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Player 2 wins the game by picking 3 balls with color 4.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10</code></li>
|
||||
<li><code>1 <= pick.length <= 100</code></li>
|
||||
<li><code>pick[i].length == 2</code></li>
|
||||
<li><code>0 <= x<sub>i</sub> <= n - 1 </code></li>
|
||||
<li><code>0 <= y<sub>i</sub> <= 10</code></li>
|
||||
</ul>
|
46
leetcode/problem/find-the-winning-player-in-coin-game.html
Normal file
46
leetcode/problem/find-the-winning-player-in-coin-game.html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>You are given two <strong>positive</strong> integers <code>x</code> and <code>y</code>, denoting the number of coins with values 75 and 10 <em>respectively</em>.</p>
|
||||
|
||||
<p>Alice and Bob are playing a game. Each turn, starting with <strong>Alice</strong>, the player must pick up coins with a <strong>total</strong> value 115. If the player is unable to do so, they <strong>lose</strong> the game.</p>
|
||||
|
||||
<p>Return the <em>name</em> of the player who wins the game if both players play <strong>optimally</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 2, y = 7</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"Alice"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The game ends in a single turn:</p>
|
||||
|
||||
<ul>
|
||||
<li>Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 4, y = 11</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"Bob"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The game ends in 2 turns:</p>
|
||||
|
||||
<ul>
|
||||
<li>Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.</li>
|
||||
<li>Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x, y <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p>
|
||||
|
||||
<p>You can perform the following operation on the string <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 < s.length</code> such that <code>s[i] == '1'</code> and <code>s[i + 1] == '0'</code>.</li>
|
||||
<li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>'1'</code>. For example, for <code>s = "010010"</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = "0<strong><u>001</u></strong>10"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>maximum</strong> number of operations that you can perform.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "1001101"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose index <code>i = 0</code>. The resulting string is <code>s = "<u><strong>001</strong></u>1101"</code>.</li>
|
||||
<li>Choose index <code>i = 4</code>. The resulting string is <code>s = "0011<u><strong>01</strong></u>1"</code>.</li>
|
||||
<li>Choose index <code>i = 3</code>. The resulting string is <code>s = "001<strong><u>01</u></strong>11"</code>.</li>
|
||||
<li>Choose index <code>i = 2</code>. The resulting string is <code>s = "00<strong><u>01</u></strong>111"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "00111"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
39
leetcode/problem/maximum-score-from-grid-operations.html
Normal file
39
leetcode/problem/maximum-score-from-grid-operations.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<sup>th</sup></code> row.</p>
|
||||
|
||||
<p>The grid score is the sum of all <code>grid[i][j]</code> such that cell <code>(i, j)</code> is white and it has a horizontally adjacent black cell.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> score that can be achieved after some number of operations.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">11</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/05/11/one.png" style="width: 300px; height: 200px;" />
|
||||
<p>In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is <code>grid[3][0] + grid[1][2] + grid[3][3]</code> which is equal to 11.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">94</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/05/11/two-1.png" style="width: 300px; height: 200px;" />
|
||||
<p>We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is <code>grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]</code> which is equal to 94.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == grid.length <= 100</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p>
|
||||
|
||||
<p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p>
|
||||
|
||||
<ul>
|
||||
<li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 <= i < n)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
We can perform the following changes:</p>
|
||||
|
||||
<ul>
|
||||
<li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li>
|
||||
<li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The integer <code>X</code> will be 2.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
We can perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li>
|
||||
<li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The integer <code>X</code> will be 4.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a string <code>s</code>.</p>
|
||||
|
||||
<p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li>
|
||||
<li>Delete the <strong>closest</strong> character to the <strong>left</strong> of index <code>i</code> that is equal to <code>s[i]</code>.</li>
|
||||
<li>Delete the <strong>closest</strong> character to the <strong>right</strong> of index <code>i</code> that is equal to <code>s[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "abaacbcbb"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
We do the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = "bacbcbb"</code>.</li>
|
||||
<li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = "acbcb"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "aa"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
We cannot perform any operations, so we return the length of the original string.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p>
|
||||
|
||||
<p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p>
|
||||
|
||||
<p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p>
|
||||
|
||||
<p>Flipping the highlighted cells makes all the rows palindromic.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/07/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p>
|
||||
|
||||
<p>Flipping the highlighted cell makes all the columns palindromic.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>All rows are already palindromic.</p>
|
||||
</div>
|
||||
|
||||
<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 <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] <= 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p>
|
||||
|
||||
<p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p>
|
||||
|
||||
<p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>'s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2024/08/01/image.png" style="width: 400px; height: 105px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/08/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/01/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p>
|
||||
</div>
|
||||
|
||||
<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 <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] <= 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>
|
||||
|
||||
<p>In a single operation, you can select any <span data-keyword="subarray">subarray</span> of <code>nums</code> and increment or decrement each element within that subarray by 1.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
|
||||
- Increment <code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br />
|
||||
- Increment <code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
|
||||
- Increment <code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br />
|
||||
- Decrement <code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br />
|
||||
- Decrement <code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br />
|
||||
- Increment <code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br />
|
||||
- Increment <code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == target.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], target[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p>
|
||||
|
||||
<p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p>
|
||||
|
||||
<p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br />
|
||||
We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
<code>n</code> and <code>k</code> are already equal, so no changes are needed.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
It is not possible to make <code>n</code> equal to <code>k</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
82
leetcode/problem/odd-and-even-transactions.html
Normal file
82
leetcode/problem/odd-and-even-transactions.html
Normal file
@@ -0,0 +1,82 @@
|
||||
<p>Table: <code>transactions</code></p>
|
||||
|
||||
<pre>
|
||||
+------------------+------+
|
||||
| Column Name | Type |
|
||||
+------------------+------+
|
||||
| transaction_id | int |
|
||||
| amount | int |
|
||||
| transaction_date | date |
|
||||
+------------------+------+
|
||||
The transactions_id column uniquely identifies each row in this table.
|
||||
Each row of this table contains the transaction id, amount and transaction date.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to find the <strong>sum of amounts</strong> for <strong>odd</strong> and <strong>even</strong> transactions for each day. If there are no odd or even transactions for a specific date, display as <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the result table ordered by</em> <code>transaction_date</code> <em>in <strong>ascending</strong> order</em>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong></p>
|
||||
|
||||
<p><code>transactions</code> table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+----------------+--------+------------------+
|
||||
| transaction_id | amount | transaction_date |
|
||||
+----------------+--------+------------------+
|
||||
| 1 | 150 | 2024-07-01 |
|
||||
| 2 | 200 | 2024-07-01 |
|
||||
| 3 | 75 | 2024-07-01 |
|
||||
| 4 | 300 | 2024-07-02 |
|
||||
| 5 | 50 | 2024-07-02 |
|
||||
| 6 | 120 | 2024-07-03 |
|
||||
+----------------+--------+------------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------------+---------+----------+
|
||||
| transaction_date | odd_sum | even_sum |
|
||||
+------------------+---------+----------+
|
||||
| 2024-07-01 | 75 | 350 |
|
||||
| 2024-07-02 | 0 | 350 |
|
||||
| 2024-07-03 | 0 | 120 |
|
||||
+------------------+---------+----------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For transaction dates:
|
||||
<ul>
|
||||
<li>2024-07-01:
|
||||
<ul>
|
||||
<li>Sum of amounts for odd transactions: 75</li>
|
||||
<li>Sum of amounts for even transactions: 150 + 200 = 350</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>2024-07-02:
|
||||
<ul>
|
||||
<li>Sum of amounts for odd transactions: 0</li>
|
||||
<li>Sum of amounts for even transactions: 300 + 50 = 350</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>2024-07-03:
|
||||
<ul>
|
||||
<li>Sum of amounts for odd transactions: 0</li>
|
||||
<li>Sum of amounts for even transactions: 120</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note:</strong> The output table is ordered by <code>transaction_date</code> in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 <= i < n - 1</code>.</p>
|
||||
|
||||
<p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p>
|
||||
|
||||
<p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
|
||||
|
||||
<p><strong>Explanation: </strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image8.jpg" style="width: 350px; height: 60px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image9.jpg" style="width: 350px; height: 60px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image10.jpg" style="width: 350px; height: 96px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image11.jpg" style="width: 300px; height: 70px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image12.jpg" style="width: 300px; height: 70px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 500</code></li>
|
||||
<li><code>1 <= queries.length <= 500</code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= queries[i][0] < queries[i][1] < n</code></li>
|
||||
<li><code>1 < queries[i][1] - queries[i][0]</code></li>
|
||||
<li>There are no repeated roads among the queries.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 <= i < n - 1</code>.</p>
|
||||
|
||||
<p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p>
|
||||
|
||||
<p>There are no two queries such that <code>queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]</code>.</p>
|
||||
|
||||
<p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
|
||||
|
||||
<p><strong>Explanation: </strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image8.jpg" style="width: 350px; height: 60px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image9.jpg" style="width: 350px; height: 60px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image10.jpg" style="width: 350px; height: 96px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image11.jpg" style="width: 300px; height: 70px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image12.jpg" style="width: 300px; height: 70px;" /></p>
|
||||
|
||||
<p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= queries[i][0] < queries[i][1] < n</code></li>
|
||||
<li><code>1 < queries[i][1] - queries[i][0]</code></li>
|
||||
<li>There are no repeated roads among the queries.</li>
|
||||
<li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]</code>.</li>
|
||||
</ul>
|
93
leetcode/problem/time-taken-to-mark-all-nodes.html
Normal file
93
leetcode/problem/time-taken-to-mark-all-nodes.html
Normal file
@@ -0,0 +1,93 @@
|
||||
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Initially, <strong>all</strong> nodes are <strong>unmarked</strong>. For each node <code>i</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>i</code> is odd, the node will get marked at time <code>x</code> if there is <strong>at least</strong> one node <em>adjacent</em> to it which was marked at time <code>x - 1</code>.</li>
|
||||
<li>If <code>i</code> is even, the node will get marked at time <code>x</code> if there is <strong>at least</strong> one node <em>adjacent</em> to it which was marked at time <code>x - 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array <code>times</code> where <code>times[i]</code> is the time when all nodes get marked in the tree, if you mark node <code>i</code> at time <code>t = 0</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the answer for each <code>times[i]</code> is <strong>independent</strong>, i.e. when you mark node <code>i</code> all other nodes are <em>unmarked</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> [2,4,3]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/01/screenshot-2024-06-02-122236.png" style="width: 500px; height: 241px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>:
|
||||
|
||||
<ul>
|
||||
<li>Node 1 is marked at <code>t = 1</code>, and Node 2 at <code>t = 2</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For <code>i = 1</code>:
|
||||
<ul>
|
||||
<li>Node 0 is marked at <code>t = 2</code>, and Node 2 at <code>t = 4</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For <code>i = 2</code>:
|
||||
<ul>
|
||||
<li>Node 0 is marked at <code>t = 2</code>, and Node 1 at <code>t = 3</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> [1,2]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/01/screenshot-2024-06-02-122249.png" style="width: 500px; height: 257px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>:
|
||||
|
||||
<ul>
|
||||
<li>Node 1 is marked at <code>t = 1</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For <code>i = 1</code>:
|
||||
<ul>
|
||||
<li>Node 0 is marked at <code>t = 2</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = </span>[[2,4],[0,1],[2,3],[0,2]]</p>
|
||||
|
||||
<p><strong>Output:</strong> [4,6,3,5,5]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-210550.png" style="height: 266px; width: 500px;" /></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
52
leetcode/problem/vowels-game-in-a-string.html
Normal file
52
leetcode/problem/vowels-game-in-a-string.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<p>Alice and Bob are playing a game on a string.</p>
|
||||
|
||||
<p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>On Alice's turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li>
|
||||
<li>On Bob's turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li>
|
||||
</ul>
|
||||
|
||||
<p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p>
|
||||
|
||||
<p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "leetcoder"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
Alice can win the game as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Alice plays first, she can delete the underlined substring in <code>s = "<u><strong>leetco</strong></u>der"</code> which contains 3 vowels. The resulting string is <code>s = "der"</code>.</li>
|
||||
<li>Bob plays second, he can delete the underlined substring in <code>s = "<u><strong>d</strong></u>er"</code> which contains 0 vowels. The resulting string is <code>s = "er"</code>.</li>
|
||||
<li>Alice plays third, she can delete the whole string <code>s = "<strong><u>er</u></strong>"</code> which contains 1 vowel.</li>
|
||||
<li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "bbcd"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong><br />
|
||||
There is no valid play for Alice in her first turn, so Alice loses the game.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user