mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-01 12:53:27 +08:00
update
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2024.03.28**
|
||||
> 最后更新日期: **2024.04.07**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3409",
|
||||
"questionFrontendId": "3103",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 2719939,
|
||||
"title": "Find Trending Hashtags II ",
|
||||
"titleSlug": "find-trending-hashtags-ii",
|
||||
"content": null,
|
||||
"translatedTitle": "查找热门话题标签 II",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": "数据库",
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"10\", \"totalSubmission\": \"60\", \"totalAcceptedRaw\": 10, \"totalSubmissionRaw\": 60, \"acRate\": \"16.7%\"}",
|
||||
"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 #MorningVibes\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good vibes! #FeelGood\",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife #ProductiveDay\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife #Innovation\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay #Thankful\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife #FutureTech\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature #Peaceful\",\"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);\\n\"],\"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 #MorningVibes', '2024-02-01')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('136', '14', 'Another #HappyDay with good vibes! #FeelGood', '2024-02-03')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('137', '15', 'Productivity peaks! #WorkLife #ProductiveDay', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('138', '16', 'Exploring new tech frontiers. #TechLife #Innovation', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('139', '17', 'Gratitude for today's moments. #HappyDay #Thankful', '2024-02-05')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('140', '18', 'Innovation drives us. #TechLife #FutureTech', '2024-02-07')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('141', '19', 'Connecting with nature's serenity. #Nature #Peaceful', '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 #MorningVibes\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good vibes! #FeelGood\",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife #ProductiveDay\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife #Innovation\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay #Thankful\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife #FutureTech\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature #Peaceful\",\"2024-02-09\"]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
178
leetcode-cn/originData/count-alternating-subarrays.json
Normal file
178
leetcode-cn/originData/count-alternating-subarrays.json
Normal file
File diff suppressed because one or more lines are too long
185
leetcode-cn/originData/find-the-sum-of-subsequence-powers.json
Normal file
185
leetcode-cn/originData/find-the-sum-of-subsequence-powers.json
Normal file
File diff suppressed because one or more lines are too long
170
leetcode-cn/originData/harshad-number.json
Normal file
170
leetcode-cn/originData/harshad-number.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
178
leetcode-cn/originData/minimize-manhattan-distances.json
Normal file
178
leetcode-cn/originData/minimize-manhattan-distances.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/minimum-cost-walk-in-weighted-graph.json
Normal file
164
leetcode-cn/originData/minimum-cost-walk-in-weighted-graph.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/minimum-levels-to-gain-more-points.json
Normal file
177
leetcode-cn/originData/minimum-levels-to-gain-more-points.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
176
leetcode-cn/originData/water-bottles-ii.json
Normal file
176
leetcode-cn/originData/water-bottles-ii.json
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,42 @@
|
||||
<p>给你一个<span data-keyword="binary-array">二进制数组 </span><code>nums</code> 。</p>
|
||||
|
||||
<p>如果一个<span data-keyword="subarray-nonempty">子数组</span>中 <strong>不存在 </strong>两个 <strong>相邻 </strong>元素的值 <strong>相同</strong> 的情况,我们称这样的子数组为 <strong>交替子数组 </strong>。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 中交替子数组的数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">nums = [0,1,1,1]</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
<!-- 解释示例1的交替子数组 -->
|
||||
|
||||
<p>以下子数组是交替子数组:<code>[0]</code> 、<code>[1]</code> 、<code>[1]</code> 、<code>[1]</code> 以及 <code>[0,1]</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">nums = [1,0,1,0]</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
<!-- 解释示例2的交替子数组 -->
|
||||
|
||||
<p>数组的每个子数组都是交替子数组。可以统计在内的子数组共有 10 个。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> 不是 <code>0</code> 就是 <code>1</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个 <strong>非负</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以选择任一下标 <code>i</code> ,然后将 <code>nums[i]</code> 加 <code>1</code> 或者减 <code>1</code> 。</p>
|
||||
|
||||
<p>请你返回将 <code>nums</code> <strong>中位数</strong> 变为 <code>k</code> 所需要的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p>一个数组的 <strong>中位数</strong> 指的是数组按 <strong>非递减</strong> 顺序排序后最中间的元素。如果数组长度为偶数,我们选择中间两个数的较大值为中位数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,5,6,8,5], k = 4</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>2</span></p>
|
||||
|
||||
<p><b>解释:</b>我们将 <code>nums[1]</code> 和 <code>nums[4]</code> 减 <code>1</code> 得到 <code>[2, 4, 6, 8, 4]</code> 。现在数组的中位数等于 <code>k</code> 。所以答案为 2 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,5,6,8,5], k = 7</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>3</span></p>
|
||||
|
||||
<p><b>解释:</b>我们将 <code>nums[1]</code> 增加 1 两次,并且将 <code>nums[2]</code> 增加 1 一次,得到 <code>[2, 7, 7, 8, 5]</code> 。结果数组的中位数等于 <code>k</code> 。所以答案为 3 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4,5,6], k = 4</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>0</span></p>
|
||||
|
||||
<p><b>解释:</b>数组中位数已经等于 <code>k</code> 了,所以不需要进行任何操作。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= 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>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/哈沙德数 [harshad-number].html
Normal file
35
leetcode-cn/problem (Chinese)/哈沙德数 [harshad-number].html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>如果一个整数能够被其各个数位上的数字之和整除,则称之为<strong> 哈沙德数</strong>(Harshad number)。给你一个整数 <code>x</code> 。如果 <code>x</code> 是 <strong>哈沙德数 </strong>,则返回<em> </em><code>x</code> 各个数位上的数字之和,否则,返回<em> </em><code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">x = 18</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">9</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>x</code> 各个数位上的数字之和为 <code>9</code> 。<code>18</code> 能被 <code>9</code> 整除。因此 <code>18</code> 是哈沙德数,答案是 <code>9</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">x = 23</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>x</code> 各个数位上的数字之和为 <code>5</code> 。<code>23</code> 不能被 <code>5</code> 整除。因此 <code>23</code> 不是哈沙德数,答案是 <code>-1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>给你一个 <code>n</code> 个节点的带权无向图,节点编号为 <code>0</code> 到 <code>n - 1</code> 。</p>
|
||||
|
||||
<p>给你一个整数 <code>n</code> 和一个数组 <code>edges</code> ,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> 表示节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 之间有一条权值为 <code>w<sub>i</sub></code> 的无向边。</p>
|
||||
|
||||
<p>在图中,一趟旅途包含一系列节点和边。旅途开始和结束点都是图中的节点,且图中存在连接旅途中相邻节点的边。注意,一趟旅途可能访问同一条边或者同一个节点多次。</p>
|
||||
|
||||
<p>如果旅途开始于节点 <code>u</code> ,结束于节点 <code>v</code> ,我们定义这一趟旅途的 <strong>代价</strong> 是经过的边权按位与 <code>AND</code> 的结果。换句话说,如果经过的边对应的边权为 <code>w<sub>0</sub>, w<sub>1</sub>, w<sub>2</sub>, ..., w<sub>k</sub></code> ,那么代价为<code>w<sub>0</sub> & w<sub>1</sub> & w<sub>2</sub> & ... & w<sub>k</sub></code> ,其中 <code>&</code> 表示按位与 <code>AND</code> 操作。</p>
|
||||
|
||||
<p>给你一个二维数组 <code>query</code> ,其中 <code>query[i] = [s<sub>i</sub>, t<sub>i</sub>]</code> 。对于每一个查询,你需要找出从节点开始 <code>s<sub>i</sub></code> ,在节点 <code>t<sub>i</sub></code> 处结束的旅途的最小代价。如果不存在这样的旅途,答案为 <code>-1</code> 。</p>
|
||||
|
||||
<p>返回数组<em> </em><code>answer</code> ,其中<em> </em><code>answer[i]</code><em> </em>表示对于查询 <code>i</code> 的 <strong>最小</strong> 旅途代价。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[1,-1]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/31/q4_example1-1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 351px; height: 141px;" /></p>
|
||||
|
||||
<p>第一个查询想要得到代价为 1 的旅途,我们依次访问:<code>0->1</code>(边权为 7 )<code>1->2</code> (边权为 1 )<code>2->1</code>(边权为 1 )<code>1->3</code> (边权为 7 )。</p>
|
||||
|
||||
<p>第二个查询中,无法从节点 3 到节点 4 ,所以答案为 -1 。</p>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
</div>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[0]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/31/q4_example2e.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 211px; height: 181px;" /></p>
|
||||
|
||||
<p>第一个查询想要得到代价为 0 的旅途,我们依次访问:<code>1->2</code>(边权为 1 ),<code>2->1</code>(边权 为 6 ),<code>1->2</code>(边权为 1 )。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>0 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= query.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>query[i].length == 2</code></li>
|
||||
<li><code>0 <= s<sub>i</sub>, t<sub>i</sub> <= n - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,75 @@
|
||||
<p>给你一个长度为 <code>n</code> 的二进制数组 <code>possible</code> 。</p>
|
||||
|
||||
<p>莉叩酱和冬坂五百里正在玩一个有 <code>n</code> 个关卡的游戏,游戏中有一些关卡是 <strong>困难</strong> 模式,其他的关卡是 <strong>简单</strong> 模式。如果 <code>possible[i] == 0</code> ,那么第 <code>i</code> 个关卡是 <strong>困难</strong> 模式。一个玩家通过一个简单模式的关卡可以获得 <code>1</code> 分,通过困难模式的关卡将失去 <code>1</code> 分。</p>
|
||||
|
||||
<p>游戏的一开始,莉叩酱将从第 <code>0</code> 级开始 <strong>按顺序</strong> 完成一些关卡,然后冬坂五百里会完成剩下的所有关卡。</p>
|
||||
|
||||
<p>假设两名玩家都采取最优策略,目的是 <strong>最大化</strong> 自己的得分,莉叩酱想知道自己 <strong>最少</strong> 需要完成多少个关卡,才能获得比冬坂五百里更多的分数。</p>
|
||||
|
||||
<p>请你返回莉叩酱获得比冬坂五百里更多的分数所需要完成的 <strong>最少</strong> 关卡数目,如果 <strong>无法</strong> 达成,那么返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>注意</strong>,每个玩家都至少需要完成 <code>1</code> 个关卡。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>possible = [1,0,1,0]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>我们来看一下莉叩酱可以完成的关卡数目:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果莉叩酱只完成关卡 0 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 1 分,冬坂五百里获得 -1 + 1 - 1 = -1 分。</li>
|
||||
<li>如果莉叩酱完成到关卡 1 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 1 - 1 = 0 分,冬坂五百里获得 1 - 1 = 0 分。</li>
|
||||
<li>如果莉叩酱完成到关卡 2 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 1 - 1 + 1 = 1 分,冬坂五百里获得 -1 分。</li>
|
||||
</ul>
|
||||
|
||||
<p>莉叩酱需要完成至少一个关卡获得更多的分数。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>possible = [1,1,1,1,1]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>3</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>我们来看一下莉叩酱可以完成的关卡数目:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果莉叩酱只完成关卡 0 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 1 分,冬坂五百里获得 4 分。</li>
|
||||
<li>如果莉叩酱完成到关卡 1 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 2 分,冬坂五百里获得 3 分。</li>
|
||||
<li>如果莉叩酱完成到关卡 2 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 3 分,冬坂五百里获得 2 分。</li>
|
||||
<li>如果莉叩酱完成到关卡 3 ,冬坂五百里完成剩下的所有关卡,那么莉叩酱获得 4 分,冬坂五百里获得 1 分。</li>
|
||||
</ul>
|
||||
|
||||
<p>莉叩酱需要完成至少三个关卡获得更多的分数。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>possible = [0,0]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>-1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>两名玩家只能各完成 1 个关卡,莉叩酱完成关卡 0 得到 -1 分,冬坂五百里完成关卡 1 得到 -1 分。两名玩家得分相同,所以莉叩酱无法得到更多分数。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == possible.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>possible[i]</code> 要么是 <code>0</code> 要么是 <code>1</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个 <strong>非负</strong> 整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果一个数组中所有元素的按位或运算 <code>OR</code> 的值 <strong>至少</strong> 为 <code>k</code> ,那么我们称这个数组是 <strong>特别的</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>最短特别非空</strong> <span data-keyword="subarray-nonempty">子数组</span>的长度,如果特别子数组不存在,那么返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2,3], k = 2</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>子数组 <code>[3]</code> 的按位 <code>OR</code> 值为 <code>3</code> ,所以我们返回 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,1,8], k = 10</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>3</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>子数组 <code>[2,1,8]</code> 的按位 <code>OR</code> 值为 <code>11</code> ,所以我们返回 <code>3</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2], k = 0</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><b>解释:</b></p>
|
||||
|
||||
<p>子数组 <code>[1]</code> 的按位 <code>OR</code> 值为 <code>1</code> ,所以我们返回 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] <= 50</code></li>
|
||||
<li><code>0 <= k < 64</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个 <strong>非负</strong> 整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果一个数组中所有元素的按位或运算 <code>OR</code> 的值 <strong>至少</strong> 为 <code>k</code> ,那么我们称这个数组是 <strong>特别的</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>最短特别非空</strong> <span data-keyword="subarray-nonempty">子数组</span>的长度,如果特别子数组不存在,那么返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2,3], k = 2</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>子数组 <code>[3]</code> 的按位 <code>OR</code> 值为 <code>3</code> ,所以我们返回 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,1,8], k = 10</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>3</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>子数组 <code>[2,1,8]</code> 的按位 <code>OR</code> 值为 <code>11</code> ,所以我们返回 <code>3</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2], k = 0</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><b>解释:</b></p>
|
||||
|
||||
<p>子数组 <code>[1]</code> 的按位 <code>OR</code> 值为 <code>1</code> ,所以我们返回 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup><font size="1">9</font></sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你两个整数 <code>numBottles</code> 和 <code>numExchange</code> 。</p>
|
||||
|
||||
<p><code>numBottles</code> 代表你最初拥有的满水瓶数量。在一次操作中,你可以执行以下操作之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>喝掉任意数量的满水瓶,使它们变成空水瓶。</li>
|
||||
<li>用 <code>numExchange</code> 个空水瓶交换一个满水瓶。然后,将 <code>numExchange</code> 的值增加 1 。</li>
|
||||
</ul>
|
||||
|
||||
<p>注意,你不能使用相同的 <code>numExchange</code> 值交换多批空水瓶。例如,如果 <code>numBottles == 3</code> 并且 <code>numExchange == 1</code> ,则不能用 <code>3</code> 个空水瓶交换成 <code>3</code> 个满水瓶。</p>
|
||||
|
||||
<p>返回你 <strong>最多</strong> 可以喝到多少瓶水。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png" style="width: 948px; height: 482px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>numBottles = 13, numExchange = 6
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>上表显示了满水瓶的数量、空水瓶的数量、numExchange 的值,以及累计喝掉的水瓶数量。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/example231.png" style="width: 990px; height: 642px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>numBottles = 10, numExchange = 3
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>上表显示了满水瓶的数量、空水瓶的数量、numExchange 的值,以及累计喝掉的水瓶数量。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= numBottles <= 100 </code></li>
|
||||
<li><code>1 <= numExchange <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>points</code> ,它表示二维平面上一些点的整数坐标,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>两点之间的距离定义为它们的<span data-keyword="manhattan-distance">曼哈顿距离</span>。</p>
|
||||
|
||||
<p>请你恰好移除一个点,返回移除后任意两点之间的 <strong>最大 </strong>距离可能的 <strong>最小 </strong>值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[3,10],[5,15],[10,2],[4,4]]
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>移除每个点后的最大距离如下所示:
|
||||
- 移除第 0 个点后,最大距离在点 (5, 15) 和 (10, 2) 之间,为 |5 - 10| + |15 - 2| = 18 。
|
||||
- 移除第 1 个点后,最大距离在点 (3, 10) 和 (10, 2) 之间,为 |3 - 10| + |10 - 2| = 15 。
|
||||
- 移除第 2 个点后,最大距离在点 (5, 15) 和 (4, 4) 之间,为 |5 - 4| + |15 - 4| = 12 。
|
||||
- 移除第 3 个点后,最大距离在点 (5, 15) 和 (10, 2) 之间的,为 |5 - 10| + |15 - 2| = 18 。
|
||||
在恰好移除一个点后,任意两点之间的最大距离可能的最小值是 12 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,1],[1,1],[1,1]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>移除任一点后,任意两点之间的最大距离都是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>1 <= points[i][0], points[i][1] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 中<strong> 严格递增 </strong>或 <strong>严格递减 </strong>的最长非空子数组的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">nums = [1,4,3,3,2]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 中严格递增的子数组有<code>[1]</code>、<code>[2]</code>、<code>[3]</code>、<code>[3]</code>、<code>[4]</code> 以及 <code>[1,4]</code> 。</p>
|
||||
|
||||
<p><code>nums</code> 中严格递减的子数组有<code>[1]</code>、<code>[2]</code>、<code>[3]</code>、<code>[3]</code>、<code>[4]</code>、<code>[3,2]</code> 以及 <code>[4,3]</code> 。</p>
|
||||
|
||||
<p>因此,返回 <code>2</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">nums = [3,3,3,3]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 中严格递增的子数组有 <code>[3]</code>、<code>[3]</code>、<code>[3]</code> 以及 <code>[3]</code> 。</p>
|
||||
|
||||
<p><code>nums</code> 中严格递减的子数组有 <code>[3]</code>、<code>[3]</code>、<code>[3]</code> 以及 <code>[3]</code> 。</p>
|
||||
|
||||
<p>因此,返回 <code>1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">nums = [3,2,1]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 中严格递增的子数组有 <code>[3]</code>、<code>[2]</code> 以及 <code>[1]</code> 。</p>
|
||||
|
||||
<p><code>nums</code> 中严格递减的子数组有 <code>[3]</code>、<code>[2]</code>、<code>[1]</code>、<code>[3,2]</code>、<code>[2,1]</code> 以及 <code>[3,2,1]</code> 。</p>
|
||||
|
||||
<p>因此,返回 <code>3</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一个子序列的 <strong>能量</strong> 定义为子序列中 <strong>任意</strong> 两个元素的差值绝对值的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中长度 <strong>等于</strong> <code>k</code> 的 <strong>所有</strong> 子序列的 <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">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4], k = 3</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>4</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 中总共有 4 个长度为 3 的子序列:<code>[1,2,3]</code> ,<code>[1,3,4]</code> ,<code>[1,2,4]</code> 和 <code>[2,3,4]</code> 。能量和为 <code>|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,2], k = 2</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>0</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 中唯一一个长度为 2 的子序列是 <code>[2,2]</code> 。能量和为 <code>|2 - 2| = 0</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">nums = [4,3,-1], k = 2</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>10</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><code>nums</code> 总共有 3 个长度为 2 的子序列:<code>[4,3]</code> ,<code>[4,-1]</code> 和 <code>[3,-1]</code> 。能量和为 <code>|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 50</code></li>
|
||||
<li><code>-10<sup>8</sup> <= nums[i] <= 10<sup>8</sup> </code></li>
|
||||
<li><code>2 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>定义函数 <code>distance(s<sub>1</sub>, s<sub>2</sub>)</code> ,用于衡量两个长度为 <code>n</code> 的字符串 <code>s<sub>1</sub></code> 和 <code>s<sub>2</sub></code> 之间的距离,即:</p>
|
||||
|
||||
<ul>
|
||||
<li>字符 <code>'a'</code> 到 <code>'z'</code> 按 <strong>循环 </strong>顺序排列,对于区间 <code>[0, n - 1]</code> 中的 <code>i</code> ,计算所有「 <code>s<sub>1</sub>[i]</code> 和 <code>s<sub>2</sub>[i]</code> 之间<strong> 最小距离</strong>」的 <strong>和 </strong>。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,<code>distance("ab", "cd") == 4</code> ,且 <code>distance("a", "z") == 1</code> 。</p>
|
||||
|
||||
<p>你可以对字符串 <code>s</code> 执行<strong> 任意次 </strong>操作。在每次操作中,可以将 <code>s</code> 中的一个字母 <strong>改变 </strong>为<strong> 任意 </strong>其他小写英文字母。</p>
|
||||
|
||||
<p>返回一个字符串,表示在执行一些操作后你可以得到的 <strong>字典序最小</strong> 的字符串 <code>t</code> ,且满足 <code>distance(s, t) <= k</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "zbbz", k = 3
|
||||
<strong>输出:</strong>"aaaz"
|
||||
<strong>解释:</strong>在这个例子中,可以执行以下操作:
|
||||
将 s[0] 改为 'a' ,s 变为 "abbz" 。
|
||||
将 s[1] 改为 'a' ,s 变为 "aabz" 。
|
||||
将 s[2] 改为 'a' ,s 变为 "aaaz" 。
|
||||
"zbbz" 和 "aaaz" 之间的距离等于 k = 3 。
|
||||
可以证明 "aaaz" 是在任意次操作后能够得到的字典序最小的字符串。
|
||||
因此,答案是 "aaaz" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "xaxcd", k = 4
|
||||
<strong>输出:</strong>"aawcd"
|
||||
<strong>解释:</strong>在这个例子中,可以执行以下操作:
|
||||
将 s[0] 改为 'a' ,s 变为 "aaxcd" 。
|
||||
将 s[2] 改为 'w' ,s 变为 "aawcd" 。
|
||||
"xaxcd" 和 "aawcd" 之间的距离等于 k = 4 。
|
||||
可以证明 "aawcd" 是在任意次操作后能够得到的字典序最小的字符串。
|
||||
因此,答案是 "aawcd" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "lol", k = 0
|
||||
<strong>输出:</strong>"lol"
|
||||
<strong>解释:</strong>在这个例子中,k = 0,更改任何字符都会使得距离大于 0 。
|
||||
因此,答案是 "lol" 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>0 <= k <= 2000</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p>
|
||||
|
||||
<p>We call a <span data-keyword="subarray-nonempty">subarray</span> <strong>alternating</strong> if <strong>no</strong> two <strong>adjacent</strong> elements in the subarray have the <strong>same</strong> value.</p>
|
||||
|
||||
<p>Return <em>the number of alternating subarrays in </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The following subarrays are alternating: <code>[0]</code>, <code>[1]</code>, <code>[1]</code>, <code>[1]</code>, and <code>[0,1]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. In one operation, you can increase or decrease any element by 1.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations needed to make the <strong><span data-keyword="median-array">median</span></strong> of <code>nums</code> <em>equal</em> to <code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,6,8,5], k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can subtract one from <code>nums[1]</code> and <code>nums[4]</code> to obtain <code>[2, 4, 6, 8, 4]</code>. The median of the resulting array is equal to <code>k</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,6,8,5], k = 7</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can add one to <code>nums[1]</code> twice and add one to <code>nums[2]</code> once to obtain <code>[2, 7, 7, 8, 5]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,6], k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The median of the array is already equal to <code>k</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= 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>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>An integer divisible by the <strong>sum</strong> of its digits is said to be a <strong>Harshad</strong> number. You are given an integer <code>x</code>. Return<em> the sum of the digits </em>of<em> </em><code>x</code><em> </em>if<em> </em><code>x</code><em> </em>is a <strong>Harshad</strong> number, otherwise, return<em> </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 18</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">9</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of digits of <code>x</code> is <code>9</code>. <code>18</code> is divisible by <code>9</code>. So <code>18</code> is a Harshad number and the answer is <code>9</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 23</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of digits of <code>x</code> is <code>5</code>. <code>23</code> is not divisible by <code>5</code>. So <code>23</code> is not a Harshad number and the answer is <code>-1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>There is an undirected weighted graph with <code>n</code> vertices labeled from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given the integer <code>n</code> and an array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between vertices <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with a weight of <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.</p>
|
||||
|
||||
<p>The <strong>cost</strong> of a walk starting at node <code>u</code> and ending at node <code>v</code> is defined as the bitwise <code>AND</code> of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is <code>w<sub>0</sub>, w<sub>1</sub>, w<sub>2</sub>, ..., w<sub>k</sub></code>, then the cost is calculated as <code>w<sub>0</sub> & w<sub>1</sub> & w<sub>2</sub> & ... & w<sub>k</sub></code>, where <code>&</code> denotes the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p>You are also given a 2D array <code>query</code>, where <code>query[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>. For each query, you need to find the minimum cost of the walk starting at vertex <code>s<sub>i</sub></code> and ending at vertex <code>t<sub>i</sub></code>. If there exists no such walk, the answer is <code>-1</code>.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> denotes the <strong>minimum</strong> cost of a walk for query </em><code>i</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,-1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/31/q4_example1-1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 351px; height: 141px;" />
|
||||
<p>To achieve the cost of 1 in the first query, we need to move on the following edges: <code>0->1</code> (weight 7), <code>1->2</code> (weight 1), <code>2->1</code> (weight 1), <code>1->3</code> (weight 7).</p>
|
||||
|
||||
<p>In the second query, there is no walk between nodes 3 and 4, so the answer is -1.</p>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
</div>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/31/q4_example2e.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 211px; height: 181px;" />
|
||||
<p>To achieve the cost of 0 in the first query, we need to move on the following edges: <code>1->2</code> (weight 1), <code>2->1</code> (weight 6), <code>1->2</code> (weight 1).</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>0 <= w<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= query.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>query[i].length == 2</code></li>
|
||||
<li><code>0 <= s<sub>i</sub>, t<sub>i</sub> <= n - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,73 @@
|
||||
<p>You are given a binary array <code>possible</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>Danielchandg and Bob are playing a game that consists of <code>n</code> levels. Some of the levels in the game are <strong>impossible</strong> to clear while others can <strong>always</strong> be cleared. In particular, if <code>possible[i] == 0</code>, then the <code>i<sup>th</sup></code> level is <strong>impossible</strong> to clear for <strong>both</strong> the players. A player gains <code>1</code> point on clearing a level and loses <code>1</code> point if the player fails to clear it.</p>
|
||||
|
||||
<p>At the start of the game, Danielchandg will play some levels in the <strong>given order</strong> starting from the <code>0<sup>th</sup></code> level, after which Bob will play for the rest of the levels.</p>
|
||||
|
||||
<p>Danielchandg wants to know the <strong>minimum</strong> number of levels he should play to gain more points than Bob, if both players play optimally to <strong>maximize</strong> their points.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of levels danielchandg should play to gain more points</em>. <em>If this is <strong>not</strong> possible, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each player must play at least <code>1</code> level.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">possible = [1,0,1,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at all the levels that Danielchandg can play up to:</p>
|
||||
|
||||
<ul>
|
||||
<li>If Danielchandg plays only level 0 and Bob plays the rest of the levels, Danielchandg has 1 point, while Bob has -1 + 1 - 1 = -1 point.</li>
|
||||
<li>If Danielchandg plays till level 1 and Bob plays the rest of the levels, Danielchandg has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.</li>
|
||||
<li>If Danielchandg plays till level 2 and Bob plays the rest of the levels, Danielchandg has 1 - 1 + 1 = 1 point, while Bob has -1 point.</li>
|
||||
</ul>
|
||||
|
||||
<p>Danielchandg must play a minimum of 1 level to gain more points.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">possible = [1,1,1,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at all the levels that Danielchandg can play up to:</p>
|
||||
|
||||
<ul>
|
||||
<li>If Danielchandg plays only level 0 and Bob plays the rest of the levels, Danielchandg has 1 point, while Bob has 4 points.</li>
|
||||
<li>If Danielchandg plays till level 1 and Bob plays the rest of the levels, Danielchandg has 2 points, while Bob has 3 points.</li>
|
||||
<li>If Danielchandg plays till level 2 and Bob plays the rest of the levels, Danielchandg has 3 points, while Bob has 2 points.</li>
|
||||
<li>If Danielchandg plays till level 3 and Bob plays the rest of the levels, Danielchandg has 4 points, while Bob has 1 point.</li>
|
||||
</ul>
|
||||
|
||||
<p>Danielchandg must play a minimum of 3 levels to gain more points.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">possible = [0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only possible way is for both players to play 1 level each. Danielchandg plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Danielchandg can't gain more points than Bob.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == possible.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>possible[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given an array <code>nums</code> of <strong>non-negative</strong> integers and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An array is called <strong>special</strong> if the bitwise <code>OR</code> of all of its elements is <strong>at least</strong> <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>shortest</strong> <strong>special</strong> <strong>non-empty</strong> <span data-keyword="subarray-nonempty">subarray</span> of</em> <code>nums</code>, <em>or return</em> <code>-1</code> <em>if no special subarray exists</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[3]</code> has <code>OR</code> value of <code>3</code>. Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,8], k = 10</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[2,1,8]</code> has <code>OR</code> value of <code>11</code>. Hence, we return <code>3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2], k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[1]</code> has <code>OR</code> value of <code>1</code>. Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] <= 50</code></li>
|
||||
<li><code>0 <= k < 64</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given an array <code>nums</code> of <strong>non-negative</strong> integers and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An array is called <strong>special</strong> if the bitwise <code>OR</code> of all of its elements is <strong>at least</strong> <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>shortest</strong> <strong>special</strong> <strong>non-empty</strong> <span data-keyword="subarray-nonempty">subarray</span> of</em> <code>nums</code>, <em>or return</em> <code>-1</code> <em>if no special subarray exists</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[3]</code> has <code>OR</code> value of <code>3</code>. Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,8], k = 10</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[2,1,8]</code> has <code>OR</code> value of <code>11</code>. Hence, we return <code>3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2], k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[1]</code> has <code>OR</code> value of <code>1</code>. Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given two integers <code>numBottles</code> and <code>numExchange</code>.</p>
|
||||
|
||||
<p><code>numBottles</code> represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Drink any number of full water bottles turning them into empty bottles.</li>
|
||||
<li>Exchange <code>numExchange</code> empty bottles with one full water bottle. Then, increase <code>numExchange</code> by one.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that you cannot exchange multiple batches of empty bottles for the same value of <code>numExchange</code>. For example, if <code>numBottles == 3</code> and <code>numExchange == 1</code>, you cannot exchange <code>3</code> empty water bottles for <code>3</code> full bottles.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of water bottles you can drink</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png" style="width: 948px; height: 482px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> numBottles = 13, numExchange = 6
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/example231.png" style="width: 990px; height: 642px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> numBottles = 10, numExchange = 3
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= numBottles <= 100 </code></li>
|
||||
<li><code>1 <= numExchange <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a array <code>points</code> representing integer coordinates of some points on a 2D plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>The distance between two points is defined as their <span data-keyword="manhattan-distance">Manhattan distance</span>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible value for <strong>maximum</strong> distance between any two points by removing exactly one point</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[3,10],[5,15],[10,2],[4,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">12</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum distance after removing each point is the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>After removing the 0<sup>th</sup> point the maximum distance is between points (5, 15) and (10, 2), which is <code>|5 - 10| + |15 - 2| = 18</code>.</li>
|
||||
<li>After removing the 1<sup>st</sup> point the maximum distance is between points (3, 10) and (10, 2), which is <code>|3 - 10| + |10 - 2| = 15</code>.</li>
|
||||
<li>After removing the 2<sup>nd</sup> point the maximum distance is between points (5, 15) and (4, 4), which is <code>|5 - 4| + |15 - 4| = 12</code>.</li>
|
||||
<li>After removing the 3<sup>rd</sup> point the maximum distance is between points (5, 15) and (10, 2), which is <code>|5 - 10| + |15 - 2| = 18</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>12 is the minimum possible maximum distance between any two points after removing exactly one point.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[1,1],[1,1],[1,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Removing any of the points results in the maximum distance between any two points of 0.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>1 <= points[i][0], points[i][1] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>You are given an array of integers <code>nums</code>. Return <em>the length of the <strong>longest</strong> <span data-keyword="subarray-nonempty">subarray</span> of </em><code>nums</code><em> which is either <strong><span data-keyword="strictly-increasing-array">strictly increasing</span></strong> or <strong><span data-keyword="strictly-decreasing-array">strictly decreasing</span></strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, and <code>[1,4]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, <code>[3,2]</code>, and <code>[4,3]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,3,3,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, and <code>[1]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, <code>[1]</code>, <code>[3,2]</code>, <code>[2,1]</code>, and <code>[3,2,1]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<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 a <span data-keyword="subsequence-array">subsequence</span> is defined as the <strong>minimum</strong> absolute difference between <strong>any</strong> two elements in the subsequence.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum</strong> of <strong>powers</strong> of <strong>all</strong> subsequences of </em><code>nums</code><em> which have length</em> <strong><em>equal to</em></strong> <code>k</code>.</p>
|
||||
|
||||
<p>Since the answer may be 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">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 4 subsequences in <code>nums</code> which have length 3: <code>[1,2,3]</code>, <code>[1,3,4]</code>, <code>[1,2,4]</code>, and <code>[2,3,4]</code>. The sum of powers is <code>|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only subsequence in <code>nums</code> which has length 2 is <code>[2,2]</code>. The sum of powers is <code>|2 - 2| = 0</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,-1], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 3 subsequences in <code>nums</code> which have length 2: <code>[4,3]</code>, <code>[4,-1]</code>, and <code>[3,-1]</code>. The sum of powers is <code>|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 50</code></li>
|
||||
<li><code>-10<sup>8</sup> <= nums[i] <= 10<sup>8</sup> </code></li>
|
||||
<li><code>2 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Define a function <code>distance(s<sub>1</sub>, s<sub>2</sub>)</code> between two strings <code>s<sub>1</sub></code> and <code>s<sub>2</sub></code> of the same length <code>n</code> as:</p>
|
||||
|
||||
<ul>
|
||||
<li>The<strong> sum</strong> of the <strong>minimum distance</strong> between <code>s<sub>1</sub>[i]</code> and <code>s<sub>2</sub>[i]</code> when the characters from <code>'a'</code> to <code>'z'</code> are placed in a <strong>cyclic</strong> order, for all <code>i</code> in the range <code>[0, n - 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>distance("ab", "cd") == 4</code>, and <code>distance("a", "z") == 1</code>.</p>
|
||||
|
||||
<p>You can <strong>change</strong> any letter of <code>s</code> to <strong>any</strong> other lowercase English letter, <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return a string denoting the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> string <code>t</code> you can get after some changes, such that <code>distance(s, t) <= k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "zbbz", k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"aaaz"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Change <code>s</code> to <code>"aaaz"</code>. The distance between <code>"zbbz"</code> and <code>"aaaz"</code> is equal to <code>k = 3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "xaxcd", k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"aawcd"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The distance between "xaxcd" and "aawcd" is equal to k = 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "lol", k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"lol"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It's impossible to change any character as <code>k = 0</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>0 <= k <= 2000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
59
leetcode/originData/[no content]find-bursty-behavior.json
Normal file
59
leetcode/originData/[no content]find-bursty-behavior.json
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3400",
|
||||
"questionFrontendId": "3089",
|
||||
"boundTopicId": null,
|
||||
"title": "Find Bursty Behavior",
|
||||
"titleSlug": "find-bursty-behavior",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 3,
|
||||
"dislikes": 8,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\": {\"Posts\": [\"post_id\", \"user_id\", \"post_date\"]}, \"rows\": {\"Posts\": [[1, 1, \"2024-02-27\"], [2, 5, \"2024-02-06\"], [3, 3, \"2024-02-25\"], [4, 3, \"2024-02-14\"], [5, 3, \"2024-02-06\"], [6, 2, \"2024-02-25\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"252\", \"totalSubmission\": \"647\", \"totalAcceptedRaw\": 252, \"totalSubmissionRaw\": 647, \"acRate\": \"38.9%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Posts\": [\"post_id\", \"user_id\", \"post_date\"]}, \"rows\": {\"Posts\": [[1, 1, \"2024-02-27\"], [2, 5, \"2024-02-06\"], [3, 3, \"2024-02-25\"], [4, 3, \"2024-02-14\"], [5, 3, \"2024-02-06\"], [6, 2, \"2024-02-25\"]]}}",
|
||||
"metaData": "{\"mysql\": [\"Create table If Not Exists Posts (post_id int, user_id int, post_date date)\"], \"mssql\": [\"Create table Posts (post_id int, user_id int, post_date date)\"], \"oraclesql\": [\"Create table Posts (post_id int, user_id int, post_date date)\", \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"], \"database\": true, \"name\": \"find_bursty_behavior\", \"pythondata\": [\"Posts = pd.DataFrame([], columns=['post_id', 'user_id', 'post_date']).astype({'post_id':'Int64', 'user_id':'Int64', 'post_date':'datetime64[ns]'})\\n\"], \"postgresql\": [\"CREATE TABLE Posts (\\n post_id int,\\n user_id int,\\n post_date date\\n);\\n\", \"SELECT TO_CHAR(post_date, 'YYYY-MM-DD') AS formatted_post_date FROM Posts;\\n\"], \"database_schema\": {\"Posts\": {\"post_id\": \"INT\", \"user_id\": \"INT\", \"post_date\": \"DATE\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Posts (post_id int, user_id int, post_date date)",
|
||||
"Truncate table Posts",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('1', '1', '2024-02-27')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('2', '5', '2024-02-06')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('3', '3', '2024-02-25')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('4', '3', '2024-02-14')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('5', '3', '2024-02-06')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('6', '2', '2024-02-25')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"], \"pythondata\": [\"Pandas\", \"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0</p>\"], \"postgresql\": [\"PostgreSQL\", \"<p>PostgreSQL 16</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3409",
|
||||
"questionFrontendId": "3103",
|
||||
"boundTopicId": null,
|
||||
"title": "Find Trending Hashtags II ",
|
||||
"titleSlug": "find-trending-hashtags-ii",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 2,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\":{\"Tweets\":[\"user_id\",\"tweet_id\",\"tweet\",\"tweet_date\"]},\"rows\":{\"Tweets\":[[135,13,\"Enjoying a great start to the day. #HappyDay #MorningVibes\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good vibes! #FeelGood\",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife #ProductiveDay\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife #Innovation\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay #Thankful\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife #FutureTech\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature #Peaceful\",\"2024-02-09\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"127\", \"totalSubmission\": \"176\", \"totalAcceptedRaw\": 127, \"totalSubmissionRaw\": 176, \"acRate\": \"72.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 #MorningVibes\",\"2024-02-01\"],[136,14,\"Another #HappyDay with good vibes! #FeelGood\",\"2024-02-03\"],[137,15,\"Productivity peaks! #WorkLife #ProductiveDay\",\"2024-02-04\"],[138,16,\"Exploring new tech frontiers. #TechLife #Innovation\",\"2024-02-04\"],[139,17,\"Gratitude for today's moments. #HappyDay #Thankful\",\"2024-02-05\"],[140,18,\"Innovation drives us. #TechLife #FutureTech\",\"2024-02-07\"],[141,19,\"Connecting with nature's serenity. #Nature #Peaceful\",\"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);\\n\"], \"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 #MorningVibes', '2024-02-01')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('136', '14', 'Another #HappyDay with good vibes! #FeelGood', '2024-02-03')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('137', '15', 'Productivity peaks! #WorkLife #ProductiveDay', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('138', '16', 'Exploring new tech frontiers. #TechLife #Innovation', '2024-02-04')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('139', '17', 'Gratitude for today's moments. #HappyDay #Thankful', '2024-02-05')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('140', '18', 'Innovation drives us. #TechLife #FutureTech', '2024-02-07')",
|
||||
"insert into Tweets (user_id, tweet_id, tweet, tweet_date) values ('141', '19', 'Connecting with nature's serenity. #Nature #Peaceful', '2024-02-09')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"], \"pythondata\": [\"Pandas\", \"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0</p>\"], \"postgresql\": [\"PostgreSQL\", \"<p>PostgreSQL 16</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/[no content]find-trending-hashtags.json
Normal file
60
leetcode/originData/[no content]find-trending-hashtags.json
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3392",
|
||||
"questionFrontendId": "3087",
|
||||
"boundTopicId": null,
|
||||
"title": "Find Trending Hashtags",
|
||||
"titleSlug": "find-trending-hashtags",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 2,
|
||||
"dislikes": 5,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"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\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"323\", \"totalSubmission\": \"476\", \"totalAcceptedRaw\": 323, \"totalSubmissionRaw\": 476, \"acRate\": \"67.9%\"}",
|
||||
"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,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"], \"pythondata\": [\"Pandas\", \"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0</p>\"], \"postgresql\": [\"PostgreSQL\", \"<p>PostgreSQL 16</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/apple-redistribution-into-boxes.json
Normal file
180
leetcode/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
175
leetcode/originData/count-alternating-subarrays.json
Normal file
175
leetcode/originData/count-alternating-subarrays.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
186
leetcode/originData/distribute-elements-into-two-arrays-ii.json
Normal file
186
leetcode/originData/distribute-elements-into-two-arrays-ii.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
200
leetcode/originData/find-the-maximum-sum-of-node-values.json
Normal file
200
leetcode/originData/find-the-maximum-sum-of-node-values.json
Normal file
File diff suppressed because one or more lines are too long
173
leetcode/originData/find-the-sum-of-encrypted-integers.json
Normal file
173
leetcode/originData/find-the-sum-of-encrypted-integers.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/find-the-sum-of-subsequence-powers.json
Normal file
182
leetcode/originData/find-the-sum-of-subsequence-powers.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
167
leetcode/originData/harshad-number.json
Normal file
167
leetcode/originData/harshad-number.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/longest-common-suffix-queries.json
Normal file
180
leetcode/originData/longest-common-suffix-queries.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
181
leetcode/originData/maximize-happiness-of-selected-children.json
Normal file
181
leetcode/originData/maximize-happiness-of-selected-children.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
175
leetcode/originData/minimize-manhattan-distances.json
Normal file
175
leetcode/originData/minimize-manhattan-distances.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/minimum-cost-walk-in-weighted-graph.json
Normal file
161
leetcode/originData/minimum-cost-walk-in-weighted-graph.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
174
leetcode/originData/minimum-levels-to-gain-more-points.json
Normal file
174
leetcode/originData/minimum-levels-to-gain-more-points.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/minimum-moves-to-pick-k-ones.json
Normal file
187
leetcode/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
187
leetcode/originData/most-frequent-ids.json
Normal file
187
leetcode/originData/most-frequent-ids.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
179
leetcode/originData/shortest-subarray-with-or-at-least-k-i.json
Normal file
179
leetcode/originData/shortest-subarray-with-or-at-least-k-i.json
Normal file
File diff suppressed because one or more lines are too long
181
leetcode/originData/shortest-subarray-with-or-at-least-k-ii.json
Normal file
181
leetcode/originData/shortest-subarray-with-or-at-least-k-ii.json
Normal file
File diff suppressed because one or more lines are too long
186
leetcode/originData/shortest-uncommon-substring-in-an-array.json
Normal file
186
leetcode/originData/shortest-uncommon-substring-in-an-array.json
Normal file
File diff suppressed because one or more lines are too long
173
leetcode/originData/water-bottles-ii.json
Normal file
173
leetcode/originData/water-bottles-ii.json
Normal file
File diff suppressed because one or more lines are too long
35
leetcode/problem/apple-redistribution-into-boxes.html
Normal file
35
leetcode/problem/apple-redistribution-into-boxes.html
Normal file
@@ -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>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>. Initially, you have an array <code>nums = [1]</code>.</p>
|
||||
|
||||
<p>You can perform <strong>any</strong> of the following operations on the array <strong>any</strong> number of times (<strong>possibly zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any element in the array and <strong>increase</strong> its value by <code>1</code>.</li>
|
||||
<li>Duplicate any element in the array and add it to the end of the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make the <strong>sum</strong> of elements of the final array greater than or equal to </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 11</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can do the following operations on the array <code>nums = [1]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase the element by <code>1</code> three times. The resulting array is <code>nums = [4]</code>.</li>
|
||||
<li>Duplicate the element two times. The resulting array is <code>nums = [4,4,4]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The sum of the final array is <code>4 + 4 + 4 = 12</code> which is greater than or equal to <code>k = 11</code>.<br />
|
||||
The total number of operations performed is <code>3 + 2 = 5</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of the original array is already greater than or equal to <code>1</code>, so no operations are needed.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
38
leetcode/problem/count-alternating-subarrays.html
Normal file
38
leetcode/problem/count-alternating-subarrays.html
Normal file
@@ -0,0 +1,38 @@
|
||||
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p>
|
||||
|
||||
<p>We call a <span data-keyword="subarray-nonempty">subarray</span> <strong>alternating</strong> if <strong>no</strong> two <strong>adjacent</strong> elements in the subarray have the <strong>same</strong> value.</p>
|
||||
|
||||
<p>Return <em>the number of alternating subarrays in </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The following subarrays are alternating: <code>[0]</code>, <code>[1]</code>, <code>[1]</code>, <code>[1]</code>, and <code>[0,1]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> is either <code>0</code> or <code>1</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,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,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>
|
59
leetcode/problem/distribute-elements-into-two-arrays-ii.html
Normal file
59
leetcode/problem/distribute-elements-into-two-arrays-ii.html
Normal file
@@ -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,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>
|
59
leetcode/problem/find-the-maximum-sum-of-node-values.html
Normal file
59
leetcode/problem/find-the-maximum-sum-of-node-values.html
Normal file
@@ -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>
|
32
leetcode/problem/find-the-sum-of-encrypted-integers.html
Normal file
32
leetcode/problem/find-the-sum-of-encrypted-integers.html
Normal file
@@ -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>
|
53
leetcode/problem/find-the-sum-of-subsequence-powers.html
Normal file
53
leetcode/problem/find-the-sum-of-subsequence-powers.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<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 a <span data-keyword="subsequence-array">subsequence</span> is defined as the <strong>minimum</strong> absolute difference between <strong>any</strong> two elements in the subsequence.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum</strong> of <strong>powers</strong> of <strong>all</strong> subsequences of </em><code>nums</code><em> which have length</em> <strong><em>equal to</em></strong> <code>k</code>.</p>
|
||||
|
||||
<p>Since the answer may be 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">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 4 subsequences in <code>nums</code> which have length 3: <code>[1,2,3]</code>, <code>[1,3,4]</code>, <code>[1,2,4]</code>, and <code>[2,3,4]</code>. The sum of powers is <code>|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only subsequence in <code>nums</code> which has length 2 is <code>[2,2]</code>. The sum of powers is <code>|2 - 2| = 0</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,-1], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 3 subsequences in <code>nums</code> which have length 2: <code>[4,3]</code>, <code>[4,-1]</code>, and <code>[3,-1]</code>. The sum of powers is <code>|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 50</code></li>
|
||||
<li><code>-10<sup>8</sup> <= nums[i] <= 10<sup>8</sup> </code></li>
|
||||
<li><code>2 <= k <= n</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>
|
33
leetcode/problem/harshad-number.html
Normal file
33
leetcode/problem/harshad-number.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<p>An integer divisible by the <strong>sum</strong> of its digits is said to be a <strong>Harshad</strong> number. You are given an integer <code>x</code>. Return<em> the sum of the digits </em>of<em> </em><code>x</code><em> </em>if<em> </em><code>x</code><em> </em>is a <strong>Harshad</strong> number, otherwise, return<em> </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 18</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">9</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of digits of <code>x</code> is <code>9</code>. <code>18</code> is divisible by <code>9</code>. So <code>18</code> is a Harshad number and the answer is <code>9</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = 23</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of digits of <code>x</code> is <code>5</code>. <code>23</code> is not divisible by <code>5</code>. So <code>23</code> is not a Harshad number and the answer is <code>-1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Define a function <code>distance(s<sub>1</sub>, s<sub>2</sub>)</code> between two strings <code>s<sub>1</sub></code> and <code>s<sub>2</sub></code> of the same length <code>n</code> as:</p>
|
||||
|
||||
<ul>
|
||||
<li>The<strong> sum</strong> of the <strong>minimum distance</strong> between <code>s<sub>1</sub>[i]</code> and <code>s<sub>2</sub>[i]</code> when the characters from <code>'a'</code> to <code>'z'</code> are placed in a <strong>cyclic</strong> order, for all <code>i</code> in the range <code>[0, n - 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>distance("ab", "cd") == 4</code>, and <code>distance("a", "z") == 1</code>.</p>
|
||||
|
||||
<p>You can <strong>change</strong> any letter of <code>s</code> to <strong>any</strong> other lowercase English letter, <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return a string denoting the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> string <code>t</code> you can get after some changes, such that <code>distance(s, t) <= k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "zbbz", k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"aaaz"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Change <code>s</code> to <code>"aaaz"</code>. The distance between <code>"zbbz"</code> and <code>"aaaz"</code> is equal to <code>k = 3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "xaxcd", k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"aawcd"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The distance between "xaxcd" and "aawcd" is equal to k = 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "lol", k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"lol"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It's impossible to change any character as <code>k = 0</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>0 <= k <= 2000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
55
leetcode/problem/longest-common-suffix-queries.html
Normal file
55
leetcode/problem/longest-common-suffix-queries.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<p>You are given two arrays of strings <code>wordsContainer</code> and <code>wordsQuery</code>.</p>
|
||||
|
||||
<p>For each <code>wordsQuery[i]</code>, you need to find a string from <code>wordsContainer</code> that has the <strong>longest common suffix</strong> with <code>wordsQuery[i]</code>. If there are two or more strings in <code>wordsContainer</code> that share the longest common suffix, find the string that is the <strong>smallest</strong> in length. If there are two or more such strings that have the <strong>same</strong> smallest length, find the one that occurred <strong>earlier</strong> in <code>wordsContainer</code>.</p>
|
||||
|
||||
<p>Return <em>an array of integers </em><code>ans</code><em>, where </em><code>ans[i]</code><em> is the index of the string in </em><code>wordsContainer</code><em> that has the <strong>longest common suffix</strong> with </em><code>wordsQuery[i]</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "cd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"cd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[1] = "bcd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"bcd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[2] = "xyz"</code>, there is no string from <code>wordsContainer</code> that shares a common suffix. Hence the longest common suffix is <code>""</code>, that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "gh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
<li>For <code>wordsQuery[1] = "acbfgh"</code>, only the string at index 0 shares the longest common suffix <code>"fgh"</code>. Hence it is the answer, even though the string at index 2 is shorter.</li>
|
||||
<li>For <code>wordsQuery[2] = "acbfegh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordsContainer.length, wordsQuery.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= wordsContainer[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= wordsQuery[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>wordsContainer[i]</code> consists only of lowercase English letters.</li>
|
||||
<li><code>wordsQuery[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>Sum of <code>wordsContainer[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
<li>Sum of <code>wordsQuery[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>You are given an array of integers <code>nums</code>. Return <em>the length of the <strong>longest</strong> <span data-keyword="subarray-nonempty">subarray</span> of </em><code>nums</code><em> which is either <strong><span data-keyword="strictly-increasing-array">strictly increasing</span></strong> or <strong><span data-keyword="strictly-decreasing-array">strictly decreasing</span></strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, and <code>[1,4]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[3]</code>, <code>[4]</code>, <code>[3,2]</code>, and <code>[4,3]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,3,3,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[3]</code>, <code>[3]</code>, and <code>[3]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The strictly increasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, and <code>[1]</code>.</p>
|
||||
|
||||
<p>The strictly decreasing subarrays of <code>nums</code> are <code>[3]</code>, <code>[2]</code>, <code>[1]</code>, <code>[3,2]</code>, <code>[2,1]</code>, and <code>[3,2,1]</code>.</p>
|
||||
|
||||
<p>Hence, we return <code>3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user