mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
e31313baa5
commit
ed5c92e00e
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,56 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3392",
|
||||
"questionFrontendId": "3087",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 2693470,
|
||||
"title": "Find Trending Hashtags",
|
||||
"titleSlug": "find-trending-hashtags",
|
||||
"content": null,
|
||||
"translatedTitle": "查找热门话题标签",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"31\", \"totalSubmission\": \"63\", \"totalAcceptedRaw\": 31, \"totalSubmissionRaw\": 63, \"acRate\": \"49.2%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Tweets\":[\"user_id\",\"tweet_id\",\"tweet\",\"tweet_date\"]},\"rows\":{\"Tweets\":[[135,13,\"Enjoying a great start to the day. #HappyDay\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good \",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature\",\"2024-02-09\"]]}}",
|
||||
"metaData": "{\"mysql\":[\"Create table If Not Exists Tweets (user_id int, tweet_id int, tweet_date date, tweet varchar(100))\"],\"mssql\":[\"Create table Tweets (user_id int, tweet_id int, tweet_date date, tweet varchar(100))\"],\"oraclesql\":[\"Create table Tweets (user_id int, tweet_id int, tweet_date date, tweet varchar(100))\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_trending_hashtags\",\"postgresql\":[\"CREATE TABLE Tweets (\\n user_id INT,\\n tweet_id INT,\\n tweet_date DATE,\\n tweet VARCHAR(100)\\n);\",\"SET DateStyle = 'ISO, MDY';\"],\"pythondata\":[\"Tweets = pd.DataFrame([], columns=['user_id', 'tweet_id', 'tweet_date', 'tweet']).astype({'user_id':'Int64', 'tweet_id':'Int64', 'tweet_date':'datetime64[ns]', 'tweet':'object'})\\n\"],\"database_schema\":{\"Tweets\":{\"user_id\":\"INT\",\"tweet_id\":\"INT\",\"tweet_date\":\"DATE\",\"tweet\":\"VARCHAR(100)\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Tweets (user_id int, tweet_id int, tweet_date date, tweet varchar(100))",
|
||||
"Truncate table Tweets",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('135', '13', 'Enjoying a great start to the day. #HappyDay', '2024-02-01')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('136', '14', 'Another #HappyDay with good ', '2024-02-03')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('137', '15', 'Productivity peaks! #WorkLife', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('138', '16', 'Exploring new tech frontiers. #TechLife', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('139', '17', 'Gratitude for today's moments. #HappyDay', '2024-02-05')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('140', '18', 'Innovation drives us. #TechLife', '2024-02-07')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('141', '19', 'Connecting with nature's serenity. #Nature', '2024-02-09')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"headers\":{\"Tweets\":[\"user_id\",\"tweet_id\",\"tweet\",\"tweet_date\"]},\"rows\":{\"Tweets\":[[135,13,\"Enjoying a great start to the day. #HappyDay\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good \",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature\",\"2024-02-09\"]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
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
183
leetcode-cn/originData/apple-redistribution-into-boxes.json
Normal file
183
leetcode-cn/originData/apple-redistribution-into-boxes.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
203
leetcode-cn/originData/find-the-maximum-sum-of-node-values.json
Normal file
203
leetcode-cn/originData/find-the-maximum-sum-of-node-values.json
Normal file
File diff suppressed because one or more lines are too long
176
leetcode-cn/originData/find-the-sum-of-encrypted-integers.json
Normal file
176
leetcode-cn/originData/find-the-sum-of-encrypted-integers.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
190
leetcode-cn/originData/minimum-moves-to-pick-k-ones.json
Normal file
190
leetcode-cn/originData/minimum-moves-to-pick-k-ones.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
@ -0,0 +1,47 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个 <strong>正奇数</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p><code>x</code> 个子数组的能量值定义为 <code>strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1</code> ,其中 <code>sum[i]</code> 是第 <code>i</code> 个子数组的和。更正式的,能量值是满足 <code>1 <= i <= x</code> 的所有 <code>i</code> 对应的 <code>(-1)<sup>i+1</sup> * sum[i] * (x - i + 1)</code> 之和。</p>
|
||||
|
||||
<p>你需要在 <code>nums</code> 中选择 <code>k</code> 个 <strong>不相交</strong><strong>子数组</strong> ,使得 <strong>能量值最大</strong> 。</p>
|
||||
|
||||
<p>请你返回可以得到的 <strong>最大</strong><strong>能量值</strong> 。</p>
|
||||
|
||||
<p><strong>注意</strong>,选出来的所有子数组 <strong>不</strong> 需要覆盖整个数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,-1,2], k = 3
|
||||
<b>输出:</b>22
|
||||
<b>解释:</b>选择 3 个子数组的最好方式是选择:nums[0..2] ,nums[3..3] 和 nums[4..4] 。能量值为 (1 + 2 + 3) * 3 - (-1) * 2 + 2 * 1 = 22 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [12,-2,-2,-2,-2], k = 5
|
||||
<b>输出:</b>64
|
||||
<b>解释:</b>唯一一种选 5 个不相交子数组的方案是:nums[0..0] ,nums[1..1] ,nums[2..2] ,nums[3..3] 和 nums[4..4] 。能量值为 12 * 5 - (-2) * 4 + (-2) * 3 - (-2) * 2 + (-2) * 1 = 64 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-1,-2,-3], k = 1
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>选择 1 个子数组的最优方案是:nums[0..0] 。能量值为 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
<li><code>1 <= n * k <= 10<sup>6</sup></code></li>
|
||||
<li><code>k</code> 是奇数。</li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数矩阵 <code>grid</code> 和一个整数 <code>k</code>。</p>
|
||||
|
||||
<p>返回包含 <code>grid</code> 左上角元素、元素和小于或等于 <code>k</code> 的 <strong><span data-keyword="submatrix">子矩阵</span></strong>的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example1.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[7,6,3],[6,6,1]], k = 18
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>如上图所示,只有 4 个子矩阵满足:包含 grid 的左上角元素,并且元素和小于或等于 18 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example21.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>如上图所示,只有 6 个子矩阵满足:包含 grid 的左上角元素,并且元素和小于或等于 20 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length </code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= n, m <= 1000 </code></li>
|
||||
<li><code>0 <= grid[i][j] <= 1000</code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>给你一棵无根带权树,树中总共有 <code>n</code> 个节点,分别表示 <code>n</code> 个服务器,服务器从 <code>0</code> 到 <code>n - 1</code> 编号。同时给你一个数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条双向边,边的权值为 <code>weight<sub>i</sub></code> 。再给你一个整数 <code>signalSpeed</code> 。</p>
|
||||
|
||||
<p>如果两个服务器 <code>a</code> ,<code>b</code> 和 <code>c</code> 满足以下条件,那么我们称服务器 <code>a</code> 和 <code>b</code> 是通过服务器 <code>c</code> <strong>可连接的</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>a < b</code> ,<code>a != c</code> 且 <code>b != c</code> 。</li>
|
||||
<li>从 <code>c</code> 到 <code>a</code> 的距离是可以被 <code>signalSpeed</code> 整除的。</li>
|
||||
<li>从 <code>c</code> 到 <code>b</code> 的距离是可以被 <code>signalSpeed</code> 整除的。</li>
|
||||
<li>从 <code>c</code> 到 <code>b</code> 的路径与从 <code>c</code> 到 <code>a</code> 的路径没有任何公共边。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>n</code> 的整数数组 <code>count</code> ,其中 <code>count[i]</code> 表示通过服务器 <code>i</code> <strong>可连接</strong> 的服务器对的 <strong>数目</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example22.png" style="width: 438px; height: 243px; padding: 10px; background: #fff; border-radius: .5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1
|
||||
<b>输出:</b>[0,4,6,6,4,0]
|
||||
<b>解释:</b>由于 signalSpeed 等于 1 ,count[c] 等于所有从 c 开始且没有公共边的路径对数目。
|
||||
在输入图中,count[c] 等于服务器 c 左边服务器数目乘以右边服务器数目。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example11.png" style="width: 495px; height: 484px; padding: 10px; background: #fff; border-radius: .5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3
|
||||
<b>输出:</b>[2,0,0,0,0,0,2]
|
||||
<b>解释:</b>通过服务器 0 ,有 2 个可连接服务器对(4, 5) 和 (4, 6) 。
|
||||
通过服务器 6 ,有 2 个可连接服务器对 (4, 5) 和 (0, 5) 。
|
||||
所有服务器对都必须通过服务器 0 或 6 才可连接,所以其他服务器对应的可连接服务器对数目都为 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code><!-- notionvc: a2623897-1bb1-4c07-84b6-917ffdcd83ec --></li>
|
||||
<li><code>1 <= weight<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= signalSpeed <= 10<sup>6</sup></code></li>
|
||||
<li>输入保证 <code>edges</code> 构成一棵合法的树。</li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>n x n</code> 的矩阵 <code>grid</code> ,其中 <code>n</code> 为奇数,且 <code>grid[r][c]</code> 的值为 <code>0</code> 、<code>1</code> 或 <code>2</code> 。</p>
|
||||
|
||||
<p>如果一个单元格属于以下三条线中的任一一条,我们就认为它是字母 <strong>Y</strong> 的一部分:</p>
|
||||
|
||||
<ul>
|
||||
<li>从左上角单元格开始到矩阵中心单元格结束的对角线。</li>
|
||||
<li>从右上角单元格开始到矩阵中心单元格结束的对角线。</li>
|
||||
<li>从中心单元格开始到矩阵底部边界结束的垂直线。</li>
|
||||
</ul>
|
||||
|
||||
<p>当且仅当满足以下全部条件时,可以判定矩阵上写有字母 <strong>Y </strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>属于 Y 的所有单元格的值相等。</li>
|
||||
<li>不属于 Y 的所有单元格的值相等。</li>
|
||||
<li>属于 Y 的单元格的值与不属于Y的单元格的值不同。</li>
|
||||
</ul>
|
||||
|
||||
<p>每次操作你可以将任意单元格的值改变为 <code>0</code> 、<code>1</code> 或 <code>2</code> 。返回在矩阵上写出字母 Y 所需的 <strong>最少 </strong>操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y2.png" style="width: 461px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,2,2],[1,1,0],[0,1,0]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>将在矩阵上写出字母 Y 需要执行的操作用蓝色高亮显示。操作后,所有属于 Y 的单元格(加粗显示)的值都为 1 ,而不属于 Y 的单元格的值都为 0 。
|
||||
可以证明,写出 Y 至少需要进行 3 次操作。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y3.png" style="width: 701px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>将在矩阵上写出字母 Y 需要执行的操作用蓝色高亮显示。操作后,所有属于 Y 的单元格(加粗显示)的值都为 0 ,而不属于 Y 的单元格的值都为 2 。
|
||||
可以证明,写出 Y 至少需要进行 12 次操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 49</code></li>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 2</code></li>
|
||||
<li><code>n</code> 为奇数。</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串 <code>s</code> ,请你判断字符串 <code>s</code> 是否存在一个长度为 <code>2</code> 的子字符串,在其反转后的字符串中也出现。</p>
|
||||
|
||||
<p>如果存在这样的子字符串,返回 <code>true</code>;如果不存在,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "leetcode"</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">true</span></p>
|
||||
|
||||
<p><strong>解释:</strong>子字符串 <code>"ee"</code> 的长度为 <code>2</code>,它也出现在 <code>reverse(s) == "edocteel"</code> 中。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abcba"</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">true</span></p>
|
||||
|
||||
<p><strong>解释:</strong>所有长度为 <code>2</code> 的子字符串 <code>"ab"</code>、<code>"bc"</code>、<code>"cb"</code>、<code>"ba"</code> 也都出现在 <code>reverse(s) == "abcba"</code> 中。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abcd"</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">false</span></p>
|
||||
|
||||
<p><strong>解释:</strong>字符串 <code>s</code> 中不存在满足「在其反转后的字符串中也出现」且长度为 <code>2</code> 的子字符串。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li>字符串 <code>s</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个下标从 <strong>1</strong> 开始、包含<strong> 不同 </strong>整数的数组 <code>nums</code> ,数组长度为 <code>n</code> 。</p>
|
||||
|
||||
<p>你需要通过 <code>n</code> 次操作,将 <code>nums</code> 中的所有元素分配到两个数组 <code>arr1</code> 和 <code>arr2</code> 中。在第一次操作中,将 <code>nums[1]</code> 追加到 <code>arr1</code> 。在第二次操作中,将 <code>nums[2]</code> 追加到 <code>arr2</code> 。之后,在第 <code>i</code> 次操作中:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>arr1</code> 的最后一个元素 <strong>大于 </strong><code>arr2</code> 的最后一个元素,就将 <code>nums[i]</code> 追加到 <code>arr1</code> 。否则,将 <code>nums[i]</code> 追加到 <code>arr2</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>通过连接数组 <code>arr1</code> 和 <code>arr2</code> 形成数组 <code>result</code> 。例如,如果 <code>arr1 == [1,2,3]</code> 且 <code>arr2 == [4,5,6]</code> ,那么 <code>result = [1,2,3,4,5,6]</code> 。</p>
|
||||
|
||||
<p>返回数组 <code>result</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,1,3]
|
||||
<strong>输出:</strong>[2,3,1]
|
||||
<strong>解释:</strong>在前两次操作后,arr1 = [2] ,arr2 = [1] 。
|
||||
在第 3 次操作中,由于 arr1 的最后一个元素大于 arr2 的最后一个元素(2 > 1),将 nums[3] 追加到 arr1 。
|
||||
3 次操作后,arr1 = [2,3] ,arr2 = [1] 。
|
||||
因此,连接形成的数组 result 是 [2,3,1] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,4,3,8]
|
||||
<strong>输出:</strong>[5,3,4,8]
|
||||
<strong>解释:</strong>在前两次操作后,arr1 = [5] ,arr2 = [4] 。
|
||||
在第 3 次操作中,由于 arr1 的最后一个元素大于 arr2 的最后一个元素(5 > 4),将 nums[3] 追加到 arr1 ,因此 arr1 变为 [5,3] 。
|
||||
在第 4 次操作中,由于 arr2 的最后一个元素大于 arr1 的最后一个元素(4 > 3),将 nums[4] 追加到 arr2 ,因此 arr2 变为 [4,8] 。
|
||||
4 次操作后,arr1 = [5,3] ,arr2 = [4,8] 。
|
||||
因此,连接形成的数组 result 是 [5,3,4,8] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li><code>nums</code>中的所有元素都互不相同。</li>
|
||||
</ul>
|
@ -0,0 +1,61 @@
|
||||
<p>给你一个下标从 <strong>1</strong> 开始、长度为 <code>n</code> 的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>现定义函数 <code>greaterCount</code> ,使得 <code>greaterCount(arr, val)</code> 返回数组 <code>arr</code> 中<strong> 严格大于</strong> <code>val</code> 的元素数量。</p>
|
||||
|
||||
<p>你需要使用 <code>n</code> 次操作,将 <code>nums</code> 的所有元素分配到两个数组 <code>arr1</code> 和 <code>arr2</code> 中。在第一次操作中,将 <code>nums[1]</code> 追加到 <code>arr1</code> 。在第二次操作中,将 <code>nums[2]</code> 追加到 <code>arr2</code> 。之后,在第 <code>i</code> 次操作中:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])</code> ,将 <code>nums[i]</code> 追加到 <code>arr1</code> 。</li>
|
||||
<li>如果 <code>greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])</code> ,将 <code>nums[i]</code> 追加到 <code>arr2</code> 。</li>
|
||||
<li>如果 <code>greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])</code> ,将 <code>nums[i]</code> 追加到元素数量较少的数组中。</li>
|
||||
<li>如果仍然相等,那么将 <code>nums[i]</code> 追加到 <code>arr1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>连接数组 <code>arr1</code> 和 <code>arr2</code> 形成数组 <code>result</code> 。例如,如果 <code>arr1 == [1,2,3]</code> 且 <code>arr2 == [4,5,6]</code> ,那么 <code>result = [1,2,3,4,5,6]</code> 。</p>
|
||||
|
||||
<p>返回整数数组 <code>result</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,1,3,3]
|
||||
<strong>输出:</strong>[2,3,1,3]
|
||||
<strong>解释:</strong>在前两次操作后,arr1 = [2] ,arr2 = [1] 。
|
||||
在第 3 次操作中,两个数组中大于 3 的元素数量都是零,并且长度相等,因此,将 nums[3] 追加到 arr1 。
|
||||
在第 4 次操作中,两个数组中大于 3 的元素数量都是零,但 arr2 的长度较小,因此,将 nums[4] 追加到 arr2 。
|
||||
在 4 次操作后,arr1 = [2,3] ,arr2 = [1,3] 。
|
||||
因此,连接形成的数组 result 是 [2,3,1,3] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,14,3,1,2]
|
||||
<strong>输出:</strong>[5,3,1,2,14]
|
||||
<strong>解释:</strong>在前两次操作后,arr1 = [5] ,arr2 = [14] 。
|
||||
在第 3 次操作中,两个数组中大于 3 的元素数量都是一,并且长度相等,因此,将 nums[3] 追加到 arr1 。
|
||||
在第 4 次操作中,arr1 中大于 1 的元素数量大于 arr2 中的数量(2 > 1),因此,将 nums[4] 追加到 arr1 。
|
||||
在第 5 次操作中,arr1 中大于 2 的元素数量大于 arr2 中的数量(2 > 1),因此,将 nums[5] 追加到 arr1 。
|
||||
在 5 次操作后,arr1 = [5,3,1,2] ,arr2 = [14] 。
|
||||
因此,连接形成的数组 result 是 [5,3,1,2,14] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,3,3,3]
|
||||
<strong>输出:</strong>[3,3,3,3]
|
||||
<strong>解释:</strong>在 4 次操作后,arr1 = [3,3] ,arr2 = [3,3] 。
|
||||
因此,连接形成的数组 result 是 [3,3,3,3] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个长度为 <code>n</code> 的数组 <code>happiness</code> ,以及一个<strong> 正整数 </strong><code>k</code> 。</p>
|
||||
|
||||
<p><code>n</code> 个孩子站成一队,其中第 <code>i</code> 个孩子的 <strong>幸福值</strong> 是<strong> </strong><code>happiness[i]</code> 。你计划组织 <code>k</code> 轮筛选从这 <code>n</code> 个孩子中选出 <code>k</code> 个孩子。</p>
|
||||
|
||||
<p>在每一轮选择一个孩子时,所有<strong> 尚未 </strong>被选中的孩子的 <strong>幸福值 </strong>将减少 <code>1</code> 。注意,幸福值<strong> 不能 </strong>变成负数,且只有在它是正数的情况下才会减少。</p>
|
||||
|
||||
<p>选择 <code>k</code> 个孩子,并使你选中的孩子幸福值之和最大,返回你能够得到的<strong> </strong><strong>最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>happiness = [1,2,3], k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>按以下方式选择 2 个孩子:
|
||||
- 选择幸福值为 3 的孩子。剩余孩子的幸福值变为 [0,1] 。
|
||||
- 选择幸福值为 1 的孩子。剩余孩子的幸福值变为 [0] 。注意幸福值不能小于 0 。
|
||||
所选孩子的幸福值之和为 3 + 1 = 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>happiness = [1,1,1,1], k = 2
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>按以下方式选择 2 个孩子:
|
||||
- 选择幸福值为 1 的任意一个孩子。剩余孩子的幸福值变为 [0,0,0] 。
|
||||
- 选择幸福值为 0 的孩子。剩余孩子的幸福值变为 [0,0] 。
|
||||
所选孩子的幸福值之和为 1 + 0 = 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>happiness = [2,3,4,5], k = 1
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>按以下方式选择 1 个孩子:
|
||||
- 选择幸福值为 5 的孩子。剩余孩子的幸福值变为 [1,2,3] 。
|
||||
所选孩子的幸福值之和为 5 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>给你一个字符串 <code>word</code> 和一个整数 <code>k</code>。</p>
|
||||
|
||||
<p>如果 <code>|freq(word[i]) - freq(word[j])| <= k</code> 对于字符串中所有下标 <code>i</code> 和 <code>j</code> 都成立,则认为 <code>word</code> 是 <strong>k 特殊字符串</strong>。</p>
|
||||
|
||||
<p>此处,<code>freq(x)</code> 表示字符 <code>x</code> 在 <code>word</code> 中的<span data-keyword="frequency-letter">出现频率</span>,而 <code>|y|</code> 表示 <code>y</code> 的绝对值。</p>
|
||||
|
||||
<p>返回使 <code>word</code> 成为 <strong>k 特殊字符串</strong> 需要删除的字符的最小数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "aabcaba", k = 0</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">3</span></p>
|
||||
|
||||
<p><strong>解释:</strong>可以删除 <code>2</code> 个 <code>"a"</code> 和 <code>1</code> 个 <code>"c"</code> 使 <code>word</code> 成为 <code>0</code> 特殊字符串。<code>word</code> 变为 <code>"baba"</code>,此时 <code>freq('a') == freq('b') == 2</code>。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "dabdcbdcdcd", k = 2</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">2</span></p>
|
||||
|
||||
<p><strong>解释:</strong>可以删除 <code>1</code> 个 <code>"a"</code> 和 <code>1</code> 个 <code>"d"</code> 使 <code>word</code> 成为 <code>2</code> 特殊字符串。<code>word</code> 变为 <code>"bdcbdcdcd"</code>,此时 <code>freq('b') == 2</code>,<code>freq('c') == 3</code>,<code>freq('d') == 4</code>。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "aaabaaa", k = 2</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">1</span></p>
|
||||
|
||||
<p><strong>解释:</strong>可以删除<strong> </strong>1 个 <code>"b"</code> 使 <code>word</code> 成为 <code>2</code>特殊字符串。因此,<code>word</code> 变为 <code>"aaaaaa"</code>,此时每个字母的频率都是 <code>6</code>。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>word</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,57 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的正整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>同时给你一个长度为 <code>m</code> 的二维操作数组 <code>queries</code> ,其中 <code>queries[i] = [index<sub>i</sub>, k<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>一开始,数组中的所有元素都 <strong>未标记</strong> 。</p>
|
||||
|
||||
<p>你需要依次对数组执行 <code>m</code> 次操作,第 <code>i</code> 次操作中,你需要执行:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果下标 <code>index<sub>i</sub></code> 对应的元素还没标记,那么标记这个元素。</li>
|
||||
<li>然后标记 <code>k<sub>i</sub></code> 个数组中还没有标记的 <strong>最小</strong> 元素。如果有元素的值相等,那么优先标记它们中下标较小的。如果少于 <code>k<sub>i</sub></code> 个未标记元素存在,那么将它们全部标记。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>m</code> 的数组 <code>answer</code> ,其中<em> </em><code>answer[i]</code>是第 <code>i</code> 次操作后数组中还没标记元素的 <strong>和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[8,3,0]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>我们依次对数组做以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>标记下标为 <code>1</code> 的元素,同时标记 <code>2</code> 个未标记的最小元素。标记完后数组为 <code>nums = [<em><strong>1</strong></em>,<em><strong>2</strong></em>,2,<em><strong>1</strong></em>,2,3,1]</code> 。未标记元素的和为 <code>2 + 2 + 3 + 1 = 8</code> 。</li>
|
||||
<li>标记下标为 <code>3</code> 的元素,由于它已经被标记过了,所以我们忽略这次标记,同时标记最靠前的 <code>3</code> 个未标记的最小元素。标记完后数组为 <code>nums = [<em><strong>1</strong></em>,<em><strong>2</strong></em>,<em><strong>2</strong></em>,<em><strong>1</strong></em>,<em><strong>2</strong></em>,3,<em><strong>1</strong></em>]</code> 。未标记元素的和为 <code>3</code> 。</li>
|
||||
<li>标记下标为 <code>4</code> 的元素,由于它已经被标记过了,所以我们忽略这次标记,同时标记最靠前的 <code>2</code> 个未标记的最小元素。标记完后数组为 <code>nums = [<em><strong>1</strong></em>,<em><strong>2</strong></em>,<em><strong>2</strong></em>,<em><strong>1</strong></em>,<em><strong>2</strong></em>,<em><strong>3</strong></em>,<em><strong>1</strong></em>]</code> 。未标记元素的和为 <code>0</code> 。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,4,2,3], queries = [[0,1]]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[7]</span></p>
|
||||
|
||||
<p><strong>解释:</strong>我们执行一次操作,将下标为 <code>0</code> 处的元素标记,并且标记最靠前的 <code>1</code> 个未标记的最小元素。标记完后数组为 <code>nums = [<em><strong>1</strong></em>,4,<em><strong>2</strong></em>,3]</code> 。未标记元素的和为 <code>4 + 3 = 7</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= index<sub>i</sub>, k<sub>i</sub> <= n - 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,60 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二进制数组 <code>nums</code>,其长度为 <code>n</code> ;另给你一个 <strong>正整数 </strong><code>k</code> 以及一个 <strong>非负整数 </strong><code>maxChanges</code> 。</p>
|
||||
|
||||
<p>Alice 在玩一个游戏,游戏的目标是让 Alice 使用 <strong>最少 </strong>数量的 <strong>行动 </strong>次数从 <code>nums</code> 中拾起 <code>k</code> 个 1 。游戏开始时,Alice 可以选择数组 <code>[0, n - 1]</code> 范围内的任何索引 <code>aliceIndex</code> 站立。如果 <code>nums[aliceIndex] == 1</code> ,Alice 会拾起一个 1 ,并且 <code>nums[aliceIndex]</code> 变成<code>0</code>(这<strong> 不算 </strong>作一次行动)。之后,Alice 可以执行 <strong>任意数量</strong> 的 <strong>行动</strong>(<strong>包括</strong><strong>零次</strong>),在每次行动中 Alice 必须 <strong>恰好 </strong>执行以下动作之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择任意一个下标 <code>j != aliceIndex</code> 且满足 <code>nums[j] == 0</code> ,然后将 <code>nums[j]</code> 设置为 <code>1</code> 。这个动作最多可以执行 <code>maxChanges</code> 次。</li>
|
||||
<li>选择任意两个相邻的下标 <code>x</code> 和 <code>y</code>(<code>|x - y| == 1</code>)且满足 <code>nums[x] == 1</code>, <code>nums[y] == 0</code> ,然后交换它们的值(将 <code>nums[y] = 1</code> 和 <code>nums[x] = 0</code>)。如果 <code>y == aliceIndex</code>,在这次行动后 Alice 拾起一个 1 ,并且 <code>nums[y]</code> 变成 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 Alice 拾起 <strong>恰好 </strong><code>k</code> 个 1 所需的 <strong>最少 </strong>行动次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">3</span></p>
|
||||
|
||||
<p><strong>解释:</strong>如果游戏开始时 Alice 在 <code>aliceIndex == 1</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>3</code> 次行动拾取 <code>3</code> 个 1 :</p>
|
||||
|
||||
<ul>
|
||||
<li>游戏开始时 Alice 拾取了一个 1 ,<code>nums[1]</code> 变成了 <code>0</code>。此时 <code>nums</code> 变为 <code>[1,<strong><u>1</u></strong>,1,0,0,1,1,0,0,1]</code> 。</li>
|
||||
<li>选择 <code>j == 2</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[1,<strong><u>0</u></strong>,1,0,0,1,1,0,0,1]</code></li>
|
||||
<li>选择 <code>x == 2</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[1,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾取了一个 1 ,<code>nums</code> 变为 <code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
|
||||
<li>选择 <code>x == 0</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[0,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾取了一个 1 ,<code>nums</code> 变为 <code>[0,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请注意,Alice 也可能执行其他的 <code>3</code> 次行动序列达成拾取 <code>3</code> 个 1 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;"><!-- 以下是示例内容的中文翻译,同时保留了原有的HTML格式和注释 -->
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [0,0,0,0], k = 2, maxChanges = 3</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">4</span></p>
|
||||
|
||||
<p><strong>解释:</strong>如果游戏开始时 Alice 在 <code>aliceIndex == 0</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>4</code> 次行动拾取 <code>2</code> 个 1 :</p>
|
||||
|
||||
<ul>
|
||||
<li>选择 <code>j == 1</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,1,0,0]</code> 。</li>
|
||||
<li>选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
|
||||
<li>再次选择 <code>j == 1</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,1,0,0]</code> 。</li>
|
||||
<li>再次选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于<code>y == aliceIndex</code>,Alice 拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 1</code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= maxChanges <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxChanges + sum(nums) >= k</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个数组 <code>arr</code> ,数组中有 <code>n</code> 个 <b>非空</b> 字符串。</p>
|
||||
|
||||
<p>请你求出一个长度为 <code>n</code> 的字符串 <code>answer</code> ,满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[i]</code> 是 <code>arr[i]</code> <strong>最短</strong> 的子字符串,且它不是 <code>arr</code> 中其他任何字符串的子字符串。如果有多个这样的子字符串存在,<code>answer[i]</code> 应该是它们中字典序最小的一个。如果不存在这样的子字符串,<code>answer[i]</code> 为空字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回数组<em> </em><code>answer</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = ["cab","ad","bad","c"]
|
||||
<b>输出:</b>["ab","","ba",""]
|
||||
<b>解释:</b>求解过程如下:
|
||||
- 对于字符串 "cab" ,最短没有在其他字符串中出现过的子字符串是 "ca" 或者 "ab" ,我们选择字典序更小的子字符串,也就是 "ab" 。
|
||||
- 对于字符串 "ad" ,不存在没有在其他字符串中出现过的子字符串。
|
||||
- 对于字符串 "bad" ,最短没有在其他字符串中出现过的子字符串是 "ba" 。
|
||||
- 对于字符串 "c" ,不存在没有在其他字符串中出现过的子字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = ["abc","bcd","abcd"]
|
||||
<b>输出:</b>["","","abcd"]
|
||||
<b>解释:</b>求解过程如下:
|
||||
- 对于字符串 "abc" ,不存在没有在其他字符串中出现过的子字符串。
|
||||
- 对于字符串 "bcd" ,不存在没有在其他字符串中出现过的子字符串。
|
||||
- 对于字符串 "abcd" ,最短没有在其他字符串中出现过的子字符串是 "abcd" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == arr.length</code></li>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>1 <= arr[i].length <= 20</code></li>
|
||||
<li><code>arr[i]</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,61 @@
|
||||
<p>给你一个字符串 <code>s</code> 。<code>s[i]</code> 要么是小写英文字母,要么是问号 <code>'?'</code> 。</p>
|
||||
|
||||
<p>对于长度为 <code>m</code> 且 <strong>只</strong> 含有小写英文字母的字符串 <code>t</code> ,我们定义函数 <code>cost(i)</code> 为下标 <code>i</code> 之前(也就是范围 <code>[0, i - 1]</code> 中)出现过与 <code>t[i]</code> <strong>相同</strong> 字符出现的次数。</p>
|
||||
|
||||
<p>字符串 <code>t</code> 的 <strong>分数</strong> 为所有下标 <code>i</code> 的 <code>cost(i)</code> 之 <strong>和</strong> 。</p>
|
||||
|
||||
<p>比方说,字符串 <code>t = "aab"</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>cost(0) = 0</code></li>
|
||||
<li><code>cost(1) = 1</code></li>
|
||||
<li><code>cost(2) = 0</code></li>
|
||||
<li>所以,字符串 <code>"aab"</code> 的分数为 <code>0 + 1 + 0 = 1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>你的任务是用小写英文字母 <strong>替换</strong> <code>s</code> 中 <strong>所有</strong> 问号,使 <code>s</code> 的 <strong>分数</strong><strong>最小 </strong>。</p>
|
||||
|
||||
<p>请你返回替换所有问号<em> </em><code>'?'</code> 之后且分数最小的字符串。如果有多个字符串的 <strong>分数最小</strong> ,那么返回字典序最小的一个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "???" </span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">"abc" </span></p>
|
||||
|
||||
<p><strong>解释:</strong>这个例子中,我们将 <code>s</code> 中的问号 <code>'?'</code> 替换得到 <code>"abc"</code> 。</p>
|
||||
|
||||
<p>对于字符串 <code>"abc"</code> ,<code>cost(0) = 0</code> ,<code>cost(1) = 0</code> 和 <code>cost(2) = 0</code> 。</p>
|
||||
|
||||
<p><code>"abc"</code> 的分数为 <code>0</code> 。</p>
|
||||
|
||||
<p>其他修改 <code>s</code> 得到分数 <code>0</code> 的字符串为 <code>"cba"</code> ,<code>"abz"</code> 和 <code>"hey"</code> 。</p>
|
||||
|
||||
<p>这些字符串中,我们返回字典序最小的。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "a?a?"</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">"abac"</span></p>
|
||||
|
||||
<p><strong>解释:</strong>这个例子中,我们将 <code>s</code> 中的问号 <code>'?'</code> 替换得到 <code>"abac"</code> 。</p>
|
||||
|
||||
<p>对于字符串 <code>"abac"</code> ,<code>cost(0) = 0</code> ,<code>cost(1) = 0</code> ,<code>cost(2) = 1</code> 和 <code>cost(3) = 0</code> 。</p>
|
||||
|
||||
<p><code>"abac"</code> 的分数为 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 要么是小写英文字母,要么是 <code>'?'</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,67 @@
|
||||
<p>给你一棵 <code>n</code> 个节点的 <strong>无向</strong> 树,节点从 <code>0</code> 到 <code>n - 1</code> 编号。树以长度为 <code>n - 1</code> 下标从 <strong>0</strong> 开始的二维整数数组 <code>edges</code> 的形式给你,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示树中节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 之间有一条边。同时给你一个 <strong>正</strong> 整数 <code>k</code> 和一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的 <strong>非负</strong> 整数数组 <code>nums</code> ,其中 <code>nums[i]</code> 表示节点 <code>i</code> 的 <strong>价值</strong> 。</p>
|
||||
|
||||
<p>Alice 想 <strong>最大化</strong> 树中所有节点价值之和。为了实现这一目标,Alice 可以执行以下操作 <strong>任意</strong> 次(<strong>包括</strong><strong> 0 次</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>选择连接节点 <code>u</code> 和 <code>v</code> 的边 <code>[u, v]</code> ,并将它们的值更新为:
|
||||
|
||||
<ul>
|
||||
<li><code>nums[u] = nums[u] XOR k</code></li>
|
||||
<li><code>nums[v] = nums[v] XOR k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 Alice 通过执行以上操作 <strong>任意次</strong> 后,可以得到所有节点 <strong>价值之和</strong> 的 <strong>最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>Alice 可以通过一次操作得到最大价值和 6 :
|
||||
- 选择边 [0,2] 。nums[0] 和 nums[2] 都变为:1 XOR 3 = 2 ,数组 nums 变为:[1,2,1] -> [2,2,2] 。
|
||||
所有节点价值之和为 2 + 2 + 2 = 6 。
|
||||
6 是可以得到最大的价值之和。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,3], k = 7, edges = [[0,1]]
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>Alice 可以通过一次操作得到最大和 9 :
|
||||
- 选择边 [0,1] 。nums[0] 变为:2 XOR 7 = 5 ,nums[1] 变为:3 XOR 7 = 4 ,数组 nums 变为:[2,3] -> [5,4] 。
|
||||
所有节点价值之和为 5 + 4 = 9 。
|
||||
9 是可以得到最大的价值之和。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
|
||||
<b>输出:</b>42
|
||||
<b>解释:</b>Alice 不需要执行任何操作,就可以得到最大价值之和 42 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</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>输入保证 <code>edges</code> 构成一棵合法的树。</li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,数组中的元素都是 <strong>正</strong> 整数。定义一个加密函数 <code>encrypt</code> ,<code>encrypt(x)</code> 将一个整数 <code>x</code> 中 <strong>每一个</strong> 数位都用 <code>x</code> 中的 <strong>最大</strong> 数位替换。比方说 <code>encrypt(523) = 555</code> 且 <code>encrypt(213) = 333</code> 。</p>
|
||||
|
||||
<p>请你返回数组中所有元素加密后的 <strong>和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><b>解释:</b>加密后的元素位 <code>[1,2,3]</code> 。加密元素的和为 <code>1 + 2 + 3 == 6</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [10,21,31]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">66</span></p>
|
||||
|
||||
<p><b>解释:</b>加密后的元素为 <code>[11,22,33]</code> 。加密元素的和为 <code>11 + 22 + 33 == 66</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,71 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一个整数数组的 <strong>能量</strong> 定义为和 <strong>等于</strong> <code>k</code> 的子序列的数目。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中所有子序列的 <strong>能量和</strong> 。</p>
|
||||
|
||||
<p>由于答案可能很大,请你将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 3 </span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 6 </span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>总共有 <code>5</code> 个能量不为 0 的子序列:</p>
|
||||
|
||||
<ul>
|
||||
<li>子序列 <code>[<em><strong>1</strong></em>,<em><strong>2</strong></em>,<em><strong>3</strong></em>]</code> 有 <code>2</code> 个和为 <code>3</code> 的子序列:<code>[1,2,<strong><em>3</em></strong>]</code> 和 <code>[<strong><em>1</em></strong>,<strong><em>2</em></strong>,3]</code> 。</li>
|
||||
<li>子序列 <code>[<em><strong>1</strong></em>,2,<em><strong>3</strong></em>]</code> 有 <code>1</code> 个和为 <code>3</code> 的子序列:<code>[1,2,<strong><em>3</em></strong>]</code> 。</li>
|
||||
<li>子序列 <code>[1,<em><strong>2</strong></em>,<em><strong>3</strong></em>]</code> 有 <code>1</code> 个和为 <code>3</code> 的子序列:<code>[1,2,<strong><em>3</em></strong>]</code> 。</li>
|
||||
<li>子序列 <code>[<em><strong>1</strong></em>,<em><strong>2</strong></em>,3]</code> 有 <code>1</code> 个和为 <code>3</code> 的子序列:<code>[<strong><em>1</em></strong>,<strong><em>2</em></strong>,3]</code> 。</li>
|
||||
<li>子序列 <code>[1,2,<em><strong>3</strong></em>]</code> 有 <code>1</code> 个和为 <code>3</code> 的子序列:<code>[1,2,<strong><em>3</em></strong>]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>所以答案为 <code>2 + 1 + 1 + 1 + 1 = 6</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [2,3,3], k = 5 </span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 4 </span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>总共有 <code>3</code> 个能量不为 0 的子序列:</p>
|
||||
|
||||
<ul>
|
||||
<li>子序列 <code>[<em><strong>2</strong></em>,<em><strong>3</strong></em>,<em><strong>3</strong></em>]</code> 有 2 个子序列和为 <code>5</code> :<code>[<strong><em>2</em></strong>,3,<strong><em>3</em></strong>]</code> 和 <code>[<strong><em>2</em></strong>,<strong><em>3</em></strong>,3]</code> 。</li>
|
||||
<li>子序列 <code>[<em><strong>2</strong></em>,3,<em><strong>3</strong></em>]</code> 有 1 个子序列和为 <code>5</code> :<code>[<strong><em>2</em></strong>,3,<strong><em>3</em></strong>]</code> 。</li>
|
||||
<li>子序列 <code>[<em><strong>2</strong></em>,<em><strong>3</strong></em>,3]</code> 有 1 个子序列和为 <code>5</code> :<code>[<strong><em>2</em></strong>,<strong><em>3</em></strong>,3]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>所以答案为 <code>2 + 1 + 1 = 4</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 7 </span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 0 </span></p>
|
||||
|
||||
<p><strong>解释:</strong>不存在和为 <code>7</code> 的子序列,所以 <code>nums</code> 的能量和为 <code>0</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个字符 <code>c </code>。返回在字符串 <code>s</code> 中并且以 <code>c</code> 字符开头和结尾的<span data-keyword="substring-nonempty">非空子字符串</span>的总数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abada", c = "a"</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><strong>解释:</strong>以 <code>"a"</code> 开头和结尾的子字符串有: <code>"<strong><u>a</u></strong>bada"</code>、<code>"<u><strong>aba</strong></u>da"</code>、<code>"<u><strong>abada</strong></u>"</code>、<code>"ab<u><strong>a</strong></u>da"</code>、<code>"ab<u><strong>ada</strong></u>"</code>、<code>"abad<u><strong>a</strong></u>"</code>。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "zzz", c = "z"</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><strong>解释:</strong>字符串 <code>s</code> 中总共有 <code>6</code> 个子字符串,并且它们都以 <code>"z"</code> 开头和结尾。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 和 <code>c</code> 均由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个下标从 <b>0</b> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以删除 <code>nums</code> 中的最小元素。</p>
|
||||
|
||||
<p>你需要使数组中的所有元素都大于或等于 <code>k</code> ,请你返回需要的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,11,10,1,3], k = 10
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>第一次操作后,nums 变为 [2, 11, 10, 3] 。
|
||||
第二次操作后,nums 变为 [11, 10, 3] 。
|
||||
第三次操作后,nums 变为 [11, 10] 。
|
||||
此时,数组中的所有元素都大于等于 10 ,所以我们停止操作。
|
||||
使数组中所有元素都大于等于 10 需要的最少操作次数为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,1,2,4,9], k = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>数组中的所有元素都大于等于 1 ,所以不需要对 nums 做任何操作。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,1,2,4,9], k = 9
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>nums 中只有一个元素大于等于 9 ,所以需要执行 4 次操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li>输入保证至少有一个满足 <code>nums[i] >= k</code> 的下标 <code>i</code> 存在。</li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一次操作中,你将执行:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择 <code>nums</code> 中最小的两个整数 <code>x</code> 和 <code>y</code> 。</li>
|
||||
<li>将 <code>x</code> 和 <code>y</code> 从 <code>nums</code> 中删除。</li>
|
||||
<li>将 <code>min(x, y) * 2 + max(x, y)</code> 添加到数组中的任意位置。</li>
|
||||
</ul>
|
||||
|
||||
<p><b>注意,</b>只有当 <code>nums</code> 至少包含两个元素时,你才可以执行以上操作。</p>
|
||||
|
||||
<p>你需要使数组中的所有元素都大于或等于 <code>k</code> ,请你返回需要的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,11,10,1,3], k = 10
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>第一次操作中,我们删除元素 1 和 2 ,然后添加 1 * 2 + 2 到 nums 中,nums 变为 [4, 11, 10, 3] 。
|
||||
第二次操作中,我们删除元素 3 和 4 ,然后添加 3 * 2 + 4 到 nums 中,nums 变为 [10, 11, 10] 。
|
||||
此时,数组中的所有元素都大于等于 10 ,所以我们停止操作。
|
||||
使数组中所有元素都大于等于 10 需要的最少操作次数为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,1,2,4,9], k = 20
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>第一次操作后,nums 变为 [2, 4, 9, 3] 。
|
||||
第二次操作后,nums 变为 [7, 4, 9] 。
|
||||
第三次操作后,nums 变为 [15, 9] 。
|
||||
第四次操作后,nums 变为 [33] 。
|
||||
此时,数组中的所有元素都大于等于 20 ,所以我们停止操作。
|
||||
使数组中所有元素都大于等于 20 需要的最少操作次数为 4 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li>输入保证答案一定存在,也就是说一定存在一个操作序列使数组中所有元素都大于等于 <code>k</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>给你一个长度为 <code>n</code> 的数组 <code>apple</code> 和另一个长度为 <code>m</code> 的数组 <code>capacity</code> 。</p>
|
||||
|
||||
<p>一共有 <code>n</code> 个包裹,其中第 <code>i</code> 个包裹中装着 <code>apple[i]</code> 个苹果。同时,还有 <code>m</code> 个箱子,第 <code>i</code> 个箱子的容量为 <code>capacity[i]</code> 个苹果。</p>
|
||||
|
||||
<p>请你选择一些箱子来将这 <code>n</code> 个包裹中的苹果重新分装到箱子中,返回你需要选择的箱子的<strong> 最小</strong> 数量。</p>
|
||||
|
||||
<p><strong>注意</strong>,同一个包裹中的苹果可以分装到不同的箱子中。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>apple = [1,3,2], capacity = [4,3,1,5,2]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>使用容量为 4 和 5 的箱子。
|
||||
总容量大于或等于苹果的总数,所以可以完成重新分装。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>apple = [5,5,5], capacity = [2,4,2,7]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>需要使用所有箱子。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == apple.length <= 50</code></li>
|
||||
<li><code>1 <= m == capacity.length <= 50</code></li>
|
||||
<li><code>1 <= apple[i], capacity[i] <= 50</code></li>
|
||||
<li>输入数据保证可以将包裹中的苹果重新分装到箱子中。</li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and a <strong>positive</strong> <strong>odd</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>The strength of <code>x</code> subarrays is defined as <code>strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1</code> where <code>sum[i]</code> is the sum of the elements in the <code>i<sup>th</sup></code> subarray. Formally, strength is sum of <code>(-1)<sup>i+1</sup> * sum[i] * (x - i + 1)</code> over all <code>i</code>'s such that <code>1 <= i <= x</code>.</p>
|
||||
|
||||
<p>You need to select <code>k</code> <strong>disjoint <span data-keyword="subarray-nonempty">subarrays</span></strong> from <code>nums</code>, such that their <strong>strength</strong> is <strong>maximum</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible <strong>strength</strong> that can be obtained</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the selected subarrays <strong>don't</strong> need to cover the entire array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,-1,2], k = 3
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is (1 + 2 + 3) * 3 - (-1) * 2 + 2 * 1 = 22.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [12,-2,-2,-2,-2], k = 5
|
||||
<strong>Output:</strong> 64
|
||||
<strong>Explanation:</strong> The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is 12 * 5 - (-2) * 4 + (-2) * 3 - (-2) * 2 + (-2) * 1 = 64.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The best possible way to select 1 subarray is: nums[0..0]. The strength is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
<li><code>1 <= n * k <= 10<sup>6</sup></code></li>
|
||||
<li><code>k</code> is odd.</li>
|
||||
</ul>
|
@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer matrix <code>grid</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>number</strong> of <span data-keyword="submatrix">submatrices</span> that contain the top-left element of the</em> <code>grid</code>, <em>and have a sum less than or equal to </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example1.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[7,6,3],[6,6,1]], k = 18
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example21.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.
|
||||
</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 <= n, m <= 1000 </code></li>
|
||||
<li><code>0 <= grid[i][j] <= 1000</code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an unrooted weighted tree with <code>n</code> vertices representing servers numbered from <code>0</code> to <code>n - 1</code>, an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> of weight <code>weight<sub>i</sub></code>. You are also given an integer <code>signalSpeed</code>.</p>
|
||||
|
||||
<p>Two servers <code>a</code> and <code>b</code> are <strong>connectable</strong> through a server <code>c</code> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>a < b</code>, <code>a != c</code> and <code>b != c</code>.</li>
|
||||
<li>The distance from <code>c</code> to <code>a</code> is divisible by <code>signalSpeed</code>.</li>
|
||||
<li>The distance from <code>c</code> to <code>b</code> is divisible by <code>signalSpeed</code>.</li>
|
||||
<li>The path from <code>c</code> to <code>b</code> and the path from <code>c</code> to <code>a</code> do not share any edges.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array</em> <code>count</code> <em>of length</em> <code>n</code> <em>where</em> <code>count[i]</code> <em>is the <strong>number</strong> of server pairs that are <strong>connectable</strong> through</em> <em>the server</em> <code>i</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example22.png" style="width: 438px; height: 243px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1
|
||||
<strong>Output:</strong> [0,4,6,6,4,0]
|
||||
<strong>Explanation:</strong> Since signalSpeed is 1, count[c] is equal to the number of pairs of paths that start at c and do not share any edges.
|
||||
In the case of the given path graph, count[c] is equal to the number of servers to the left of c multiplied by the servers to the right of c.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example11.png" style="width: 495px; height: 484px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3
|
||||
<strong>Output:</strong> [2,0,0,0,0,0,2]
|
||||
<strong>Explanation:</strong> Through server 0, there are 2 pairs of connectable servers: (4, 5) and (4, 6).
|
||||
Through server 6, there are 2 pairs of connectable servers: (4, 5) and (0, 5).
|
||||
It can be shown that no two servers are connectable through servers other than 0 and 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code><!-- notionvc: a2623897-1bb1-4c07-84b6-917ffdcd83ec --></li>
|
||||
<li><code>1 <= weight<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= signalSpeed <= 10<sup>6</sup></code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>n x n</code> grid where <code>n</code> is odd, and <code>grid[r][c]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</p>
|
||||
|
||||
<p>We say that a cell belongs to the Letter <strong>Y</strong> if it belongs to one of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>The diagonal starting at the top-left cell and ending at the center cell of the grid.</li>
|
||||
<li>The diagonal starting at the top-right cell and ending at the center cell of the grid.</li>
|
||||
<li>The vertical line starting at the center cell and ending at the bottom border of the grid.</li>
|
||||
</ul>
|
||||
|
||||
<p>The Letter <strong>Y</strong> is written on the grid if and only if:</p>
|
||||
|
||||
<ul>
|
||||
<li>All values at cells belonging to the Y are equal.</li>
|
||||
<li>All values at cells not belonging to the Y are equal.</li>
|
||||
<li>The values at cells belonging to the Y are different from the values at cells not belonging to the Y.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to</em> <code>0</code><em>,</em> <code>1</code><em>,</em> <em>or</em> <code>2</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y2.png" style="width: 461px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,2],[1,1,0],[0,1,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0.
|
||||
It can be shown that 3 is the minimum number of operations needed to write Y on the grid.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y3.png" style="width: 701px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2.
|
||||
It can be shown that 12 is the minimum number of operations needed to write Y on the grid.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 49 </code></li>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 2</code></li>
|
||||
<li><code>n</code> is odd.</li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>Given a<strong> </strong>string <code>s</code>, find any <span data-keyword="substring">substring</span> of length <code>2</code> which is also present in the reverse of <code>s</code>.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if such a substring exists, and </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "leetcode"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Substring <code>"ee"</code> is of length <code>2</code> which is also present in <code>reverse(s) == "edocteel"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abcba"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> All of the substrings of length <code>2</code> <code>"ab"</code>, <code>"bc"</code>, <code>"cb"</code>, <code>"ba"</code> are also present in <code>reverse(s) == "abcba"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abcd"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> There is no substring of length <code>2</code> in <code>s</code>, which is also present in the reverse of <code>s</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, append <code>nums[1]</code> to <code>arr1</code>. In the second operation, append <code>nums[2]</code> to <code>arr2</code>. Afterwards, in the <code>i<sup>th</sup></code> operation:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the last element of <code>arr1</code> is<strong> greater</strong> than the last element of <code>arr2</code>, append <code>nums[i]</code> to <code>arr1</code>. Otherwise, append <code>nums[i]</code> to <code>arr2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The array <code>result</code> is formed by concatenating the arrays <code>arr1</code> and <code>arr2</code>. For example, if <code>arr1 == [1,2,3]</code> and <code>arr2 == [4,5,6]</code>, then <code>result = [1,2,3,4,5,6]</code>.</p>
|
||||
|
||||
<p>Return <em>the array</em> <code>result</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3]
|
||||
<strong>Output:</strong> [2,3,1]
|
||||
<strong>Explanation:</strong> After the first 2 operations, arr1 = [2] and arr2 = [1].
|
||||
In the 3<sup>rd</sup> operation, as the last element of arr1 is greater than the last element of arr2 (2 > 1), append nums[3] to arr1.
|
||||
After 3 operations, arr1 = [2,3] and arr2 = [1].
|
||||
Hence, the array result formed by concatenation is [2,3,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,3,8]
|
||||
<strong>Output:</strong> [5,3,4,8]
|
||||
<strong>Explanation:</strong> After the first 2 operations, arr1 = [5] and arr2 = [4].
|
||||
In the 3<sup>rd</sup> operation, as the last element of arr1 is greater than the last element of arr2 (5 > 4), append nums[3] to arr1, hence arr1 becomes [5,3].
|
||||
In the 4<sup>th</sup> operation, as the last element of arr2 is greater than the last element of arr1 (4 > 3), append nums[4] to arr2, hence arr2 becomes [4,8].
|
||||
After 4 operations, arr1 = [5,3] and arr2 = [4,8].
|
||||
Hence, the array result formed by concatenation is [5,3,4,8].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li>All elements in <code>nums</code> are distinct.</li>
|
||||
</ul>
|
@ -0,0 +1,59 @@
|
||||
<p>You are given a <strong>1-indexed</strong> array of integers <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>We define a function <code>greaterCount</code> such that <code>greaterCount(arr, val)</code> returns the number of elements in <code>arr</code> that are <strong>strictly greater</strong> than <code>val</code>.</p>
|
||||
|
||||
<p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, append <code>nums[1]</code> to <code>arr1</code>. In the second operation, append <code>nums[2]</code> to <code>arr2</code>. Afterwards, in the <code>i<sup>th</sup></code> operation:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to <code>arr1</code>.</li>
|
||||
<li>If <code>greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to <code>arr2</code>.</li>
|
||||
<li>If <code>greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to the array with a <strong>lesser</strong> number of elements.</li>
|
||||
<li>If there is still a tie, append <code>nums[i]</code> to <code>arr1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The array <code>result</code> is formed by concatenating the arrays <code>arr1</code> and <code>arr2</code>. For example, if <code>arr1 == [1,2,3]</code> and <code>arr2 == [4,5,6]</code>, then <code>result = [1,2,3,4,5,6]</code>.</p>
|
||||
|
||||
<p>Return <em>the integer array</em> <code>result</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,3]
|
||||
<strong>Output:</strong> [2,3,1,3]
|
||||
<strong>Explanation:</strong> After the first 2 operations, arr1 = [2] and arr2 = [1].
|
||||
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is zero in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
|
||||
In the 4<sup>th</sup> operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.
|
||||
After 4 operations, arr1 = [2,3] and arr2 = [1,3].
|
||||
Hence, the array result formed by concatenation is [2,3,1,3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,14,3,1,2]
|
||||
<strong>Output:</strong> [5,3,1,2,14]
|
||||
<strong>Explanation:</strong> After the first 2 operations, arr1 = [5] and arr2 = [14].
|
||||
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
|
||||
In the 4<sup>th</sup> operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1.
|
||||
In the 5<sup>th</sup> operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1.
|
||||
After 5 operations, arr1 = [5,3,1,2] and arr2 = [14].
|
||||
Hence, the array result formed by concatenation is [5,3,1,2,14].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3,3]
|
||||
<strong>Output:</strong> [3,3,3,3]
|
||||
<strong>Explanation:</strong> At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3].
|
||||
Hence, the array result formed by concatenation is [3,3,3,3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
|
||||
|
||||
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> happiness = [1,2,3], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can pick 2 children in the following way:
|
||||
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
|
||||
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
|
||||
The sum of the happiness values of the selected children is 3 + 1 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can pick 2 children in the following way:
|
||||
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
|
||||
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
|
||||
The sum of the happiness values of the selected children is 1 + 0 = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We can pick 1 child in the following way:
|
||||
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
|
||||
The sum of the happiness values of the selected children is 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>We consider <code>word</code> to be <strong>k-special</strong> if <code>|freq(word[i]) - freq(word[j])| <= k</code> for all indices <code>i</code> and <code>j</code> in the string.</p>
|
||||
|
||||
<p>Here, <code>freq(x)</code> denotes the <span data-keyword="frequency-letter">frequency</span> of the character <code>x</code> in <code>word</code>, and <code>|y|</code> denotes the absolute value of <code>y</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of characters you need to delete to make</em> <code>word</code> <strong><em>k-special</em></strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "aabcaba", k = 0</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> We can make <code>word</code> <code>0</code>-special by deleting <code>2</code> occurrences of <code>"a"</code> and <code>1</code> occurrence of <code>"c"</code>. Therefore, <code>word</code> becomes equal to <code>"baba"</code> where <code>freq('a') == freq('b') == 2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "dabdcbdcdcd", k = 2</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> We can make <code>word</code> <code>2</code>-special by deleting <code>1</code> occurrence of <code>"a"</code> and <code>1</code> occurrence of <code>"d"</code>. Therefore, <code>word</code> becomes equal to "bdcbdcdcd" where <code>freq('b') == 2</code>, <code>freq('c') == 3</code>, and <code>freq('d') == 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">word = "aaabaaa", k = 2</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> We can make <code>word</code> <code>2</code>-special by deleting <code>1</code> occurrence of <code>"b"</code>. Therefore, <code>word</code> becomes equal to <code>"aaaaaa"</code> where each letter's frequency is now uniformly <code>6</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,55 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You are also given a 2D array <code>queries</code> of size <code>m</code> where <code>queries[i] = [index<sub>i</sub>, k<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>Initially all elements of the array are <strong>unmarked</strong>.</p>
|
||||
|
||||
<p>You need to apply <code>m</code> queries on the array in order, where on the <code>i<sup>th</sup></code> query you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Mark the element at index <code>index<sub>i</sub></code> if it is not already marked.</li>
|
||||
<li>Then mark <code>k<sub>i</sub></code> unmarked elements in the array with the <strong>smallest</strong> values. If multiple such elements exist, mark the ones with the smallest indices. And if less than <code>k<sub>i</sub></code> unmarked elements exist, then mark all of them.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array answer of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>sum</strong> of unmarked elements in the array after the </em><code>i<sup>th</sup></code><em> query</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]]</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[8,3,0]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We do the following queries on the array:</p>
|
||||
|
||||
<ul>
|
||||
<li>Mark the element at index <code>1</code>, and <code>2</code> of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = [<strong><u>1</u></strong>,<u><strong>2</strong></u>,2,<u><strong>1</strong></u>,2,3,1]</code>. The sum of unmarked elements is <code>2 + 2 + 3 + 1 = 8</code>.</li>
|
||||
<li>Mark the element at index <code>3</code>, since it is already marked we skip it. Then we mark <code>3</code> of the smallest unmarked elements with the smallest indices, the marked elements now are <code>nums = [<strong><u>1</u></strong>,<u><strong>2</strong></u>,<u><strong>2</strong></u>,<u><strong>1</strong></u>,<u><strong>2</strong></u>,3,<strong><u>1</u></strong>]</code>. The sum of unmarked elements is <code>3</code>.</li>
|
||||
<li>Mark the element at index <code>4</code>, since it is already marked we skip it. Then we mark <code>2</code> of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = [<strong><u>1</u></strong>,<u><strong>2</strong></u>,<u><strong>2</strong></u>,<u><strong>1</strong></u>,<u><strong>2</strong></u>,<strong><u>3</u></strong>,<u><strong>1</strong></u>]</code>. The sum of unmarked elements is <code>0</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,4,2,3], queries = [[0,1]]</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[7]</span></p>
|
||||
|
||||
<p><strong>Explanation: </strong> We do one query which is mark the element at index <code>0</code> and mark the smallest element among unmarked elements. The marked elements will be <code>nums = [<strong><u>1</u></strong>,4,<u><strong>2</strong></u>,3]</code>, and the sum of unmarked elements is <code>4 + 3 = 7</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= index<sub>i</sub>, k<sub>i</sub> <= n - 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,58 @@
|
||||
<p>You are given a binary array <code>nums</code> of length <code>n</code>, a <strong>positive</strong> integer <code>k</code> and a <strong>non-negative</strong> integer <code>maxChanges</code>.</p>
|
||||
|
||||
<p>Alice plays a game, where the goal is for Alice to pick up <code>k</code> ones from <code>nums</code> using the <strong>minimum</strong> number of <strong>moves</strong>. When the game starts, Alice picks up any index <code>aliceIndex</code> in the range <code>[0, n - 1]</code> and stands there. If <code>nums[aliceIndex] == 1</code> , Alice picks up the one and <code>nums[aliceIndex]</code> becomes <code>0</code>(this <strong>does not</strong> count as a move). After this, Alice can make <strong>any</strong> number of <strong>moves</strong> (<strong>including</strong> <strong>zero</strong>) where in each move Alice must perform <strong>exactly</strong> one of the following actions:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select any index <code>j != aliceIndex</code> such that <code>nums[j] == 0</code> and set <code>nums[j] = 1</code>. This action can be performed <strong>at</strong> <strong>most</strong> <code>maxChanges</code> times.</li>
|
||||
<li>Select any two adjacent indices <code>x</code> and <code>y</code> (<code>|x - y| == 1</code>) such that <code>nums[x] == 1</code>, <code>nums[y] == 0</code>, then swap their values (set <code>nums[y] = 1</code> and <code>nums[x] = 0</code>). If <code>y == aliceIndex</code>, Alice picks up the one after this move and <code>nums[y]</code> becomes <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of moves required by Alice to pick <strong>exactly </strong></em><code>k</code> <em>ones</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Alice can pick up <code>3</code> ones in <code>3</code> moves, if Alice performs the following actions in each move when standing at <code>aliceIndex == 1</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li> At the start of the game Alice picks up the one and <code>nums[1]</code> becomes <code>0</code>. <code>nums</code> becomes <code>[1,<strong><u>1</u></strong>,1,0,0,1,1,0,0,1]</code>.</li>
|
||||
<li>Select <code>j == 2</code> and perform an action of the first type. <code>nums</code> becomes <code>[1,<strong><u>0</u></strong>,1,0,0,1,1,0,0,1]</code></li>
|
||||
<li>Select <code>x == 2</code> and <code>y == 1</code>, and perform an action of the second type. <code>nums</code> becomes <code>[1,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes <code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code>.</li>
|
||||
<li>Select <code>x == 0</code> and <code>y == 1</code>, and perform an action of the second type. <code>nums</code> becomes <code>[0,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes <code>[0,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that it may be possible for Alice to pick up <code>3</code> ones using some other sequence of <code>3</code> moves.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [0,0,0,0], k = 2, maxChanges = 3</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Alice can pick up <code>2</code> ones in <code>4</code> moves, if Alice performs the following actions in each move when standing at <code>aliceIndex == 0</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select <code>j == 1</code> and perform an action of the first type. <code>nums</code> becomes <code>[<strong><u>0</u></strong>,1,0,0]</code>.</li>
|
||||
<li>Select <code>x == 1</code> and <code>y == 0</code>, and perform an action of the second type. <code>nums</code> becomes <code>[<strong><u>1</u></strong>,0,0,0]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes <code>[<strong><u>0</u></strong>,0,0,0]</code>.</li>
|
||||
<li>Select <code>j == 1</code> again and perform an action of the first type. <code>nums</code> becomes <code>[<strong><u>0</u></strong>,1,0,0]</code>.</li>
|
||||
<li>Select <code>x == 1</code> and <code>y == 0</code> again, and perform an action of the second type. <code>nums</code> becomes <code>[<strong><u>1</u></strong>,0,0,0]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes <code>[<strong><u>0</u></strong>,0,0,0]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 1</code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= maxChanges <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxChanges + sum(nums) >= k</code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given an array <code>arr</code> of size <code>n</code> consisting of <strong>non-empty</strong> strings.</p>
|
||||
|
||||
<p>Find a string array <code>answer</code> of size <code>n</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[i]</code> is the <strong>shortest</strong> <span data-keyword="substring">substring</span> of <code>arr[i]</code> that does <strong>not</strong> occur as a substring in any other string in <code>arr</code>. If multiple such substrings exist, <code>answer[i]</code> should be the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span>. And if no such substring exists, <code>answer[i]</code> should be an empty string.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array </em><code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = ["cab","ad","bad","c"]
|
||||
<strong>Output:</strong> ["ab","","ba",""]
|
||||
<strong>Explanation:</strong> We have the following:
|
||||
- For the string "cab", the shortest substring that does not occur in any other string is either "ca" or "ab", we choose the lexicographically smaller substring, which is "ab".
|
||||
- For the string "ad", there is no substring that does not occur in any other string.
|
||||
- For the string "bad", the shortest substring that does not occur in any other string is "ba".
|
||||
- For the string "c", there is no substring that does not occur in any other string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = ["abc","bcd","abcd"]
|
||||
<strong>Output:</strong> ["","","abcd"]
|
||||
<strong>Explanation:</strong> We have the following:
|
||||
- For the string "abc", there is no substring that does not occur in any other string.
|
||||
- For the string "bcd", there is no substring that does not occur in any other string.
|
||||
- For the string "abcd", the shortest substring that does not occur in any other string is "abcd".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == arr.length</code></li>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>1 <= arr[i].length <= 20</code></li>
|
||||
<li><code>arr[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,59 @@
|
||||
<p>You are given a string <code>s</code>. <code>s[i]</code> is either a lowercase English letter or <code>'?'</code>.</p>
|
||||
|
||||
<p>For a string <code>t</code> having length <code>m</code> containing <strong>only</strong> lowercase English letters, we define the function <code>cost(i)</code> for an index <code>i</code> as the number of characters <strong>equal</strong> to <code>t[i]</code> that appeared before it, i.e. in the range <code>[0, i - 1]</code>.</p>
|
||||
|
||||
<p>The <strong>value</strong> of <code>t</code> is the <strong>sum</strong> of <code>cost(i)</code> for all indices <code>i</code>.</p>
|
||||
|
||||
<p>For example, for the string <code>t = "aab"</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>cost(0) = 0</code></li>
|
||||
<li><code>cost(1) = 1</code></li>
|
||||
<li><code>cost(2) = 0</code></li>
|
||||
<li>Hence, the value of <code>"aab"</code> is <code>0 + 1 + 0 = 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Your task is to <strong>replace all</strong> occurrences of <code>'?'</code> in <code>s</code> with any lowercase English letter so that the <strong>value</strong> of <code>s</code> is <strong>minimized</strong>.</p>
|
||||
|
||||
<p>Return <em>a string denoting the modified string with replaced occurrences of </em><code>'?'</code><em>. If there are multiple strings resulting in the <strong>minimum value</strong>, return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> one.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> s = "???" </span></p>
|
||||
|
||||
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> "abc" </span></p>
|
||||
|
||||
<p><strong>Explanation: </strong> In this example, we can replace the occurrences of <code>'?'</code> to make <code>s</code> equal to <code>"abc"</code>.</p>
|
||||
|
||||
<p>For <code>"abc"</code>, <code>cost(0) = 0</code>, <code>cost(1) = 0</code>, and <code>cost(2) = 0</code>.</p>
|
||||
|
||||
<p>The value of <code>"abc"</code> is <code>0</code>.</p>
|
||||
|
||||
<p>Some other modifications of <code>s</code> that have a value of <code>0</code> are <code>"cba"</code>, <code>"abz"</code>, and, <code>"hey"</code>.</p>
|
||||
|
||||
<p>Among all of them, we choose the lexicographically smallest.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "a?a?"</span></p>
|
||||
|
||||
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">"abac"</span></p>
|
||||
|
||||
<p><strong>Explanation: </strong> In this example, the occurrences of <code>'?'</code> can be replaced to make <code>s</code> equal to <code>"abac"</code>.</p>
|
||||
|
||||
<p>For <code>"abac"</code>, <code>cost(0) = 0</code>, <code>cost(1) = 0</code>, <code>cost(2) = 1</code>, and <code>cost(3) = 0</code>.</p>
|
||||
|
||||
<p>The value of <code>"abac"</code> is <code>1</code>.</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 a lowercase English letter or <code>'?'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,59 @@
|
||||
<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 <strong>0-indexed</strong> 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. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p>
|
||||
|
||||
<p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows:
|
||||
|
||||
<ul>
|
||||
<li><code>nums[u] = nums[u] XOR k</code></li>
|
||||
<li><code>nums[v] = nums[v] XOR k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> Alice can achieve the maximum sum of 6 using a single operation:
|
||||
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
|
||||
The total sum of values is 2 + 2 + 2 = 6.
|
||||
It can be shown that 6 is the maximum achievable sum of values.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Alice can achieve the maximum sum of 9 using a single operation:
|
||||
- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
|
||||
The total sum of values is 5 + 4 = 9.
|
||||
It can be shown that 9 is the maximum achievable sum of values.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</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> represent a valid tree.</li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given an integer array <code>nums</code> containing <strong>positive</strong> integers. We define a function <code>encrypt</code> such that <code>encrypt(x)</code> replaces <strong>every</strong> digit in <code>x</code> with the <strong>largest</strong> digit in <code>x</code>. For example, <code>encrypt(523) = 555</code> and <code>encrypt(213) = 333</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum </strong>of encrypted elements</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> The encrypted elements are <code>[1,2,3]</code>. The sum of encrypted elements is <code>1 + 2 + 3 == 6</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [10,21,31]</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">66</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> The encrypted elements are <code>[11,22,33]</code>. The sum of encrypted elements is <code>11 + 22 + 33 == 66</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,69 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>power</strong> of an array of integers is defined as the number of <span data-keyword="subsequence-array">subsequences</span> with their sum <strong>equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum</strong> of <strong>power</strong> of all subsequences of</em> <code>nums</code><em>.</em></p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 3 </span></p>
|
||||
|
||||
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 6 </span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are <code>5</code> subsequences of nums with non-zero power:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subsequence <code>[<u><strong>1</strong></u>,<u><strong>2</strong></u>,<u><strong>3</strong></u>]</code> has <code>2</code> subsequences with <code>sum == 3</code>: <code>[1,2,<u>3</u>]</code> and <code>[<u>1</u>,<u>2</u>,3]</code>.</li>
|
||||
<li>The subsequence <code>[<u><strong>1</strong></u>,2,<u><strong>3</strong></u>]</code> has <code>1</code> subsequence with <code>sum == 3</code>: <code>[1,2,<u>3</u>]</code>.</li>
|
||||
<li>The subsequence <code>[1,<u><strong>2</strong></u>,<u><strong>3</strong></u>]</code> has <code>1</code> subsequence with <code>sum == 3</code>: <code>[1,2,<u>3</u>]</code>.</li>
|
||||
<li>The subsequence <code>[<u><strong>1</strong></u>,<u><strong>2</strong></u>,3]</code> has <code>1</code> subsequence with <code>sum == 3</code>: <code>[<u>1</u>,<u>2</u>,3]</code>.</li>
|
||||
<li>The subsequence <code>[1,2,<u><strong>3</strong></u>]</code> has <code>1</code> subsequence with <code>sum == 3</code>: <code>[1,2,<u>3</u>]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Hence the answer is <code>2 + 1 + 1 + 1 + 1 = 6</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [2,3,3], k = 5 </span></p>
|
||||
|
||||
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 4 </span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are <code>3</code> subsequences of nums with non-zero power:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subsequence <code>[<u><strong>2</strong></u>,<u><strong>3</strong></u>,<u><strong>3</strong></u>]</code> has 2 subsequences with <code>sum == 5</code>: <code>[<u>2</u>,3,<u>3</u>]</code> and <code>[<u>2</u>,<u>3</u>,3]</code>.</li>
|
||||
<li>The subsequence <code>[<u><strong>2</strong></u>,3,<u><strong>3</strong></u>]</code> has 1 subsequence with <code>sum == 5</code>: <code>[<u>2</u>,3,<u>3</u>]</code>.</li>
|
||||
<li>The subsequence <code>[<u><strong>2</strong></u>,<u><strong>3</strong></u>,3]</code> has 1 subsequence with <code>sum == 5</code>: <code>[<u>2</u>,<u>3</u>,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Hence the answer is <code>2 + 1 + 1 = 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 7 </span></p>
|
||||
|
||||
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 0 </span></p>
|
||||
|
||||
<p><strong>Explanation: </strong>There exists no subsequence with sum <code>7</code>. Hence all subsequences of nums have <code>power = 0</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,30 @@
|
||||
<p>You are given a string <code>s</code> and a character <code>c</code>. Return <em>the total number of <span data-keyword="substring-nonempty">substrings</span> of </em><code>s</code><em> that start and end with </em><code>c</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abada", c = "a"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Substrings starting and ending with <code>"a"</code> are: <code>"<strong><u>a</u></strong>bada"</code>, <code>"<u><strong>aba</strong></u>da"</code>, <code>"<u><strong>abada</strong></u>"</code>, <code>"ab<u><strong>a</strong></u>da"</code>, <code>"ab<u><strong>ada</strong></u>"</code>, <code>"abad<u><strong>a</strong></u>"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "zzz", c = "z"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> There are a total of <code>6</code> substrings in <code>s</code> and all start and end with <code>"z"</code>.</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> and <code>c</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you can remove one occurrence of the smallest element of <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations needed so that all elements of the array are greater than or equal to</em> <code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,11,10,1,3], k = 10
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> After one operation, nums becomes equal to [2, 11, 10, 3].
|
||||
After two operations, nums becomes equal to [11, 10, 3].
|
||||
After three operations, nums becomes equal to [11, 10].
|
||||
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
|
||||
It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,9], k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,9], k = 9
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li>The input is generated such that there is at least one index <code>i</code> such that <code>nums[i] >= k</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you will:</p>
|
||||
|
||||
<ul>
|
||||
<li>Take the two smallest integers <code>x</code> and <code>y</code> in <code>nums</code>.</li>
|
||||
<li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li>
|
||||
<li>Add <code>min(x, y) * 2 + max(x, y)</code> anywhere in the array.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you can only apply the described operation if <code>nums</code> contains at least two elements.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations needed so that all elements of the array are greater than or equal to</em> <code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,11,10,1,3], k = 10
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
|
||||
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
|
||||
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
|
||||
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,9], k = 20
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> After one operation, nums becomes equal to [2, 4, 9, 3].
|
||||
After two operations, nums becomes equal to [7, 4, 9].
|
||||
After three operations, nums becomes equal to [15, 9].
|
||||
After four operations, nums becomes equal to [33].
|
||||
At this stage, all the elements of nums are greater than 20 so we can stop.
|
||||
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li>The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to <code>k</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>You are given an array <code>apple</code> of size <code>n</code> and an array <code>capacity</code> of size <code>m</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> packs where the <code>i<sup>th</sup></code> pack contains <code>apple[i]</code> apples. There are <code>m</code> boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of <code>capacity[i]</code> apples.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of boxes you need to select to redistribute these </em><code>n</code><em> packs of apples into boxes</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that, apples from the same pack can be distributed into different boxes.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> apple = [1,3,2], capacity = [4,3,1,5,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We will use boxes with capacities 4 and 5.
|
||||
It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> apple = [5,5,5], capacity = [2,4,2,7]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We will need to use all the boxes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == apple.length <= 50</code></li>
|
||||
<li><code>1 <= m == capacity.length <= 50</code></li>
|
||||
<li><code>1 <= apple[i], capacity[i] <= 50</code></li>
|
||||
<li>The input is generated such that it's possible to redistribute packs of apples into boxes.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user