mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-04-18 08:39:41 +08:00
update
This commit is contained in:
parent
6bfbd3556c
commit
5028bd771c
README.md
leetcode-cn
origin-data.json
originData
[no content]find-pattern-in-infinite-stream-ii.jsonapply-operations-to-make-string-empty.jsoncount-prefix-and-suffix-pairs-i.jsoncount-prefix-and-suffix-pairs-ii.jsonfind-the-length-of-the-longest-common-prefix.jsonmaximize-consecutive-elements-in-an-array-after-modification.jsonmaximum-number-of-operations-with-the-same-score-i.jsonmaximum-number-of-operations-with-the-same-score-ii.jsonmaximum-palindromes-after-operations.jsonmodify-the-matrix.jsonmost-frequent-prime.jsonnumber-of-subarrays-that-match-a-pattern-i.jsonnumber-of-subarrays-that-match-a-pattern-ii.jsontype-of-triangle.json
problem (Chinese)
三角形类型 [type-of-triangle].html修改数组后最大化数组中的连续元素数目 [maximize-consecutive-elements-in-an-array-after-modification].html修改矩阵 [modify-the-matrix].html出现频率最高的质数 [most-frequent-prime].html匹配模式数组的子数组数目 I [number-of-subarrays-that-match-a-pattern-i].html匹配模式数组的子数组数目 II [number-of-subarrays-that-match-a-pattern-ii].html回文字符串的最大数量 [maximum-palindromes-after-operations].html最长公共前缀的长度 [find-the-length-of-the-longest-common-prefix].html相同分数的最大操作数目 I [maximum-number-of-operations-with-the-same-score-i].html相同分数的最大操作数目 II [maximum-number-of-operations-with-the-same-score-ii].html统计前后缀下标对 I [count-prefix-and-suffix-pairs-i].html统计前后缀下标对 II [count-prefix-and-suffix-pairs-ii].html进行操作使字符串为空 [apply-operations-to-make-string-empty].html
problem (English)
三角形类型(English) [type-of-triangle].html修改数组后最大化数组中的连续元素数目(English) [maximize-consecutive-elements-in-an-array-after-modification].html修改矩阵(English) [modify-the-matrix].html出现频率最高的质数(English) [most-frequent-prime].html匹配模式数组的子数组数目 I(English) [number-of-subarrays-that-match-a-pattern-i].html匹配模式数组的子数组数目 II(English) [number-of-subarrays-that-match-a-pattern-ii].html回文字符串的最大数量(English) [maximum-palindromes-after-operations].html最长公共前缀的长度(English) [find-the-length-of-the-longest-common-prefix].html相同分数的最大操作数目 I(English) [maximum-number-of-operations-with-the-same-score-i].html相同分数的最大操作数目 II(English) [maximum-number-of-operations-with-the-same-score-ii].html统计前后缀下标对 I(English) [count-prefix-and-suffix-pairs-i].html统计前后缀下标对 II(English) [count-prefix-and-suffix-pairs-ii].html进行操作使字符串为空(English) [apply-operations-to-make-string-empty].html
leetcode
origin-data.json
originData
[no content]find-pattern-in-infinite-stream-ii.jsonapply-operations-to-make-string-empty.jsoncount-prefix-and-suffix-pairs-i.jsoncount-prefix-and-suffix-pairs-ii.jsonfind-the-length-of-the-longest-common-prefix.jsonmaximize-consecutive-elements-in-an-array-after-modification.jsonmaximum-number-of-operations-with-the-same-score-i.jsonmaximum-number-of-operations-with-the-same-score-ii.jsonmaximum-palindromes-after-operations.jsonmodify-the-matrix.jsonmost-frequent-prime.jsonnumber-of-subarrays-that-match-a-pattern-i.jsonnumber-of-subarrays-that-match-a-pattern-ii.jsontype-of-triangle.json
problem
apply-operations-to-make-string-empty.htmlcount-prefix-and-suffix-pairs-i.htmlcount-prefix-and-suffix-pairs-ii.htmlfind-the-length-of-the-longest-common-prefix.htmlmaximize-consecutive-elements-in-an-array-after-modification.htmlmaximum-number-of-operations-with-the-same-score-i.htmlmaximum-number-of-operations-with-the-same-score-ii.htmlmaximum-palindromes-after-operations.htmlmodify-the-matrix.htmlmost-frequent-prime.htmlnumber-of-subarrays-that-match-a-pattern-i.htmlnumber-of-subarrays-that-match-a-pattern-ii.htmltype-of-triangle.html
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2024.02.09**
|
||||
> 最后更新日期: **2024.02.19**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/count-prefix-and-suffix-pairs-i.json
Normal file
164
leetcode-cn/originData/count-prefix-and-suffix-pairs-i.json
Normal file
File diff suppressed because one or more lines are too long
166
leetcode-cn/originData/count-prefix-and-suffix-pairs-ii.json
Normal file
166
leetcode-cn/originData/count-prefix-and-suffix-pairs-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
164
leetcode-cn/originData/maximize-consecutive-elements-in-an-array-after-modification.json
Normal file
164
leetcode-cn/originData/maximize-consecutive-elements-in-an-array-after-modification.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
205
leetcode-cn/originData/maximum-palindromes-after-operations.json
Normal file
205
leetcode-cn/originData/maximum-palindromes-after-operations.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode-cn/originData/modify-the-matrix.json
Normal file
174
leetcode-cn/originData/modify-the-matrix.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/most-frequent-prime.json
Normal file
163
leetcode-cn/originData/most-frequent-prime.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
183
leetcode-cn/originData/type-of-triangle.json
Normal file
183
leetcode-cn/originData/type-of-triangle.json
Normal file
File diff suppressed because one or more lines are too long
38
leetcode-cn/problem (Chinese)/三角形类型 [type-of-triangle].html
Normal file
38
leetcode-cn/problem (Chinese)/三角形类型 [type-of-triangle].html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>3</code> 的整数数组 <code>nums</code> ,需要用它们来构造三角形。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果一个三角形的所有边长度相等,那么这个三角形称为 <strong>equilateral</strong> 。</li>
|
||||
<li>如果一个三角形恰好有两条边长度相等,那么这个三角形称为 <strong>isosceles</strong> 。</li>
|
||||
<li>如果一个三角形三条边的长度互不相同,那么这个三角形称为 <strong>scalene</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果这个数组无法构成一个三角形,请你返回字符串 <code>"none"</code> ,否则返回一个字符串表示这个三角形的类型。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,3,3]
|
||||
<b>输出:</b>"equilateral"
|
||||
<b>解释:</b>由于三条边长度相等,所以可以构成一个等边三角形,返回 "equilateral" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,4,5]
|
||||
<b>输出:</b>"scalene"
|
||||
<b>解释:</b>
|
||||
nums[0] + nums[1] = 3 + 4 = 7 ,大于 nums[2] = 5 <span style="text-wrap: wrap;">。</span>
|
||||
nums[0] + nums[2] = 3 + 5 = 8 ,大于 nums[1] = 4 。
|
||||
nums[1] + nums[2] = 4 + 5 = 9 ,大于 nums[0] = 3 。
|
||||
由于任意两边之和都大于第三边,所以可以构成一个三角形,因为三条边的长度互不相等,所以返回 "scalene"。
|
||||
</pre>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/修改数组后最大化数组中的连续元素数目 [maximize-consecutive-elements-in-an-array-after-modification].html
Normal file
35
leetcode-cn/problem (Chinese)/修改数组后最大化数组中的连续元素数目 [maximize-consecutive-elements-in-an-array-after-modification].html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始只包含 <strong>正</strong> 整数的数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>一开始,你可以将数组中 <strong>任意数量</strong> 元素增加 <strong>至多</strong> <code>1</code> 。</p>
|
||||
|
||||
<p>修改后,你可以从最终数组中选择 <strong>一个或者更多</strong> 元素,并确保这些元素升序排序后是 <strong>连续</strong> 的。比方说,<code>[3, 4, 5]</code> 是连续的,但是 <code>[3, 4, 6]</code> 和 <code>[1, 1, 2, 3]</code> 不是连续的。<!-- notionvc: 312f8c5d-40d0-4cd1-96cc-9e96a846735b --></p>
|
||||
|
||||
<p>请你返回 <strong>最多</strong> 可以选出的元素数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,1,5,1,1]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>我们将下标 0 和 3 处的元素增加 1 ,得到结果数组 nums = [3,1,5,2,1] 。
|
||||
我们选择元素 [<em><strong>3</strong></em>,<em><strong>1</strong></em>,5,<em><strong>2</strong></em>,1] 并将它们排序得到 [1,2,3] ,是连续元素。
|
||||
最多可以得到 3 个连续元素。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,4,7,10]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们可以选择的最多元素数目是 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/修改矩阵 [modify-the-matrix].html
Normal file
35
leetcode-cn/problem (Chinese)/修改矩阵 [modify-the-matrix].html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的整数矩阵 <code>matrix</code> ,新建一个下标从 <strong>0</strong> 开始、名为 <code>answer</code> 的矩阵。使 <code>answer</code> 与 <code>matrix</code> 相等,接着将其中每个值为 <code>-1</code> 的元素替换为所在列的 <strong>最大</strong> 元素。</p>
|
||||
|
||||
<p>返回矩阵 <code>answer</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix1.png" style="width: 491px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
|
||||
<strong>输出:</strong>[[1,2,9],[4,8,6],[7,8,9]]
|
||||
<strong>解释:</strong>上图显示了发生替换的元素(蓝色区域)。
|
||||
- 将单元格 [1][1] 中的值替换为列 1 中的最大值 8 。
|
||||
- 将单元格 [0][2] 中的值替换为列 2 中的最大值 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix2.png" style="width: 411px; height: 111px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[3,-1],[5,2]]
|
||||
<strong>输出:</strong>[[3,2],[5,2]]
|
||||
<strong>解释:</strong>上图显示了发生替换的元素(蓝色区域)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == matrix.length</code></li>
|
||||
<li><code>n == matrix[i].length</code></li>
|
||||
<li><code>2 <= m, n <= 50</code></li>
|
||||
<li><code>-1 <= matrix[i][j] <= 100</code></li>
|
||||
<li>测试用例中生成的输入满足每列至少包含一个非负整数。</li>
|
||||
</ul>
|
@ -0,0 +1,66 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 、下标从 <strong>0</strong> 开始的二维矩阵 <code>mat</code> 。在每个单元格,你可以按以下方式生成数字:</p>
|
||||
|
||||
<ul>
|
||||
<li>最多有 <code>8</code> 条路径可以选择:东,东南,南,西南,西,西北,北,东北。</li>
|
||||
<li>选择其中一条路径,沿着这个方向移动,并且将路径上的数字添加到正在形成的数字后面。</li>
|
||||
<li>注意,每一步都会生成数字,例如,如果路径上的数字是 <code>1, 9, 1</code>,那么在这个方向上会生成三个数字:<code>1, 19, 191</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在遍历矩阵所创建的所有数字中,出现频率最高的、<strong>大于</strong> <code>10</code>的<span data-keyword="prime-number">质数</span>;如果不存在这样的质数,则返回 <code>-1</code><em> </em>。如果存在多个出现频率最高的质数,那么返回其中最大的那个。</p>
|
||||
|
||||
<p><strong>注意:</strong>移动过程中不允许改变方向。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2024/02/15/south" style="width: 641px; height: 291px;" /> </strong>
|
||||
|
||||
<pre>
|
||||
<strong>
|
||||
输入:</strong>mat = [[1,1],[9,9],[1,1]]
|
||||
<strong>输出:</strong>19
|
||||
<strong>解释:</strong>
|
||||
从单元格 (0,0) 出发,有 3 个可能的方向,这些方向上可以生成的大于 10 的数字有:
|
||||
东方向: [11], 东南方向: [19], 南方向: [19,191] 。
|
||||
从单元格 (0,1) 出发,所有可能方向上生成的大于 10 的数字有:[19,191,19,11] 。
|
||||
从单元格 (1,0) 出发,所有可能方向上生成的大于 10 的数字有:[99,91,91,91,91] 。
|
||||
从单元格 (1,1) 出发,所有可能方向上生成的大于 10 的数字有:[91,91,99,91,91] 。
|
||||
从单元格 (2,0) 出发,所有可能方向上生成的大于 10 的数字有:[11,19,191,19] 。
|
||||
从单元格 (2,1) 出发,所有可能方向上生成的大于 10 的数字有:[11,19,19,191] 。
|
||||
在所有生成的数字中,出现频率最高的质数是 19 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[7]]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>唯一可以生成的数字是 7 。它是一个质数,但不大于 10 ,所以返回 -1 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[9,7,8],[4,6,5],[2,8,6]]
|
||||
<strong>输出:</strong>97
|
||||
<strong>解释:</strong>
|
||||
从单元格 (0,0) 出发,所有可能方向上生成的大于 10 的数字有: [97,978,96,966,94,942] 。
|
||||
从单元格 (0,1) 出发,所有可能方向上生成的大于 10 的数字有: [78,75,76,768,74,79] 。
|
||||
从单元格 (0,2) 出发,所有可能方向上生成的大于 10 的数字有: [85,856,86,862,87,879] 。
|
||||
从单元格 (1,0) 出发,所有可能方向上生成的大于 10 的数字有: [46,465,48,42,49,47] 。
|
||||
从单元格 (1,1) 出发,所有可能方向上生成的大于 10 的数字有: [65,66,68,62,64,69,67,68] 。
|
||||
从单元格 (1,2) 出发,所有可能方向上生成的大于 10 的数字有: [56,58,56,564,57,58] 。
|
||||
从单元格 (2,0) 出发,所有可能方向上生成的大于 10 的数字有: [28,286,24,249,26,268] 。
|
||||
从单元格 (2,1) 出发,所有可能方向上生成的大于 10 的数字有: [86,82,84,86,867,85] 。
|
||||
从单元格 (2,2) 出发,所有可能方向上生成的大于 10 的数字有: [68,682,66,669,65,658] 。
|
||||
在所有生成的数字中,出现频率最高的质数是 97 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 6</code></li>
|
||||
<li><code>1 <= mat[i][j] <= 9</code></li>
|
||||
</ul>
|
42
leetcode-cn/problem (Chinese)/匹配模式数组的子数组数目 I [number-of-subarrays-that-match-a-pattern-i].html
Normal file
42
leetcode-cn/problem (Chinese)/匹配模式数组的子数组数目 I [number-of-subarrays-that-match-a-pattern-i].html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>nums</code> ,和一个下标从 <code>0</code> 开始长度为 <code>m</code> 的整数数组 <code>pattern</code> ,<code>pattern</code> 数组只包含整数 <code>-1</code> ,<code>0</code> 和 <code>1</code> 。</p>
|
||||
|
||||
<p>大小为 <code>m + 1</code> 的<span data-keyword="subarray">子数组</span> <code>nums[i..j]</code> 如果对于每个元素 <code>pattern[k]</code> 都满足以下条件,那么我们说这个子数组匹配模式数组 <code>pattern</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>pattern[k] == 1</code> ,那么 <code>nums[i + k + 1] > nums[i + k]</code></li>
|
||||
<li>如果 <code>pattern[k] == 0</code> ,那么 <code>nums[i + k + 1] == nums[i + k]</code></li>
|
||||
<li>如果 <code>pattern[k] == -1</code> ,那么 <code>nums[i + k + 1] < nums[i + k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回匹配 <code>pattern</code> 的 <code>nums</code> 子数组的 <strong>数目</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>模式 [1,1] 说明我们要找的子数组是长度为 3 且严格上升的。在数组 nums 中,子数组 [1,2,3] ,[2,3,4] ,[3,4,5] 和 [4,5,6] 都匹配这个模式。
|
||||
所以 nums 中总共有 4 个子数组匹配这个模式。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>这里,模式数组 [1,0,-1] 说明我们需要找的子数组中,第一个元素小于第二个元素,第二个元素等于第三个元素,第三个元素大于第四个元素。在 nums 中,子数组 [1,4,4,1] 和 [3,5,5,3] 都匹配这个模式。
|
||||
所以 nums 中总共有 2 个子数组匹配这个模式。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
42
leetcode-cn/problem (Chinese)/匹配模式数组的子数组数目 II [number-of-subarrays-that-match-a-pattern-ii].html
Normal file
42
leetcode-cn/problem (Chinese)/匹配模式数组的子数组数目 II [number-of-subarrays-that-match-a-pattern-ii].html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>nums</code> ,和一个下标从 <code>0</code> 开始长度为 <code>m</code> 的整数数组 <code>pattern</code> ,<code>pattern</code> 数组只包含整数 <code>-1</code> ,<code>0</code> 和 <code>1</code> 。</p>
|
||||
|
||||
<p>大小为 <code>m + 1</code> 的<span data-keyword="subarray">子数组</span> <code>nums[i..j]</code> 如果对于每个元素 <code>pattern[k]</code> 都满足以下条件,那么我们说这个子数组匹配模式数组 <code>pattern</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>pattern[k] == 1</code> ,那么 <code>nums[i + k + 1] > nums[i + k]</code></li>
|
||||
<li>如果 <code>pattern[k] == 0</code> ,那么 <code>nums[i + k + 1] == nums[i + k]</code></li>
|
||||
<li>如果 <code>pattern[k] == -1</code> ,那么 <code>nums[i + k + 1] < nums[i + k]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回匹配 <code>pattern</code> 的 <code>nums</code> 子数组的 <strong>数目</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>模式 [1,1] 说明我们要找的子数组是长度为 3 且严格上升的。在数组 nums 中,子数组 [1,2,3] ,[2,3,4] ,[3,4,5] 和 [4,5,6] 都匹配这个模式。
|
||||
所以 nums 中总共有 4 个子数组匹配这个模式。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>这里,模式数组 [1,0,-1] 说明我们需要找的子数组中,第一个元素小于第二个元素,第二个元素等于第三个元素,第三个元素大于第四个元素。在 nums 中,子数组 [1,4,4,1] 和 [3,5,5,3] 都匹配这个模式。
|
||||
所以 nums 中总共有 2 个子数组匹配这个模式。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,57 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> ,数组的长度为 <code>n</code> ,且包含下标从 <strong>0</strong> 开始的若干字符串。</p>
|
||||
|
||||
<p>你可以执行以下操作 <strong>任意 </strong>次数(<strong>包括零次</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>选择整数<code>i</code>、<code>j</code>、<code>x</code>和<code>y</code>,满足<code>0 <= i, j < n</code>,<code>0 <= x < words[i].length</code>,<code>0 <= y < words[j].length</code>,<strong>交换 </strong>字符 <code>words[i][x]</code> 和 <code>words[j][y]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回一个整数,表示在执行一些操作后,<code>words</code> 中可以包含的回文字符串的 <strong>最大 </strong>数量。</p>
|
||||
|
||||
<p><strong>注意:</strong>在操作过程中,<code>i</code> 和 <code>j</code> 可以相等。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["abbb","ba","aa"]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>在这个例子中,获得最多回文字符串的一种方式是:
|
||||
选择 i = 0, j = 1, x = 0, y = 0,交换 words[0][0] 和 words[1][0] 。words 变成了 ["bbbb","aa","aa"] 。
|
||||
words 中的所有字符串都是回文。
|
||||
因此,可实现的回文字符串的最大数量是 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["abc","ab"]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在这个例子中,获得最多回文字符串的一种方式是:
|
||||
选择 i = 0, j = 1, x = 1, y = 0,交换 words[0][1] 和 words[1][0] 。words 变成了 ["aac","bb"] 。
|
||||
选择 i = 0, j = 0, x = 1, y = 2,交换 words[0][1] 和 words[0][2] 。words 变成了 ["aca","bb"] 。
|
||||
两个字符串都是回文 。
|
||||
因此,可实现的回文字符串的最大数量是 2。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["cd","ef","a"]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>在这个例子中,没有必要执行任何操作。
|
||||
words 中有一个回文 "a" 。
|
||||
可以证明,在执行任何次数操作后,无法得到更多回文。
|
||||
因此,答案是 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
41
leetcode-cn/problem (Chinese)/最长公共前缀的长度 [find-the-length-of-the-longest-common-prefix].html
Normal file
41
leetcode-cn/problem (Chinese)/最长公共前缀的长度 [find-the-length-of-the-longest-common-prefix].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>给你两个 <strong>正整数 </strong>数组 <code>arr1</code> 和 <code>arr2</code> 。</p>
|
||||
|
||||
<p>正整数的 <strong>前缀 </strong>是其 <strong>最左边 </strong>的一位或多位数字组成的整数。例如,<code>123</code> 是整数 <code>12345</code> 的前缀,而 <code>234</code><strong> 不是 </strong>。</p>
|
||||
|
||||
<p>设若整数 <code>c</code> 是整数 <code>a</code> 和 <code>b</code> 的<strong> 公共前缀 </strong>,那么 <code>c</code> 需要同时是 <code>a</code> 和 <code>b</code> 的前缀。例如,<code>5655359</code> 和 <code>56554</code> 有公共前缀 <code>565</code> ,而 <code>1223</code> 和 <code>43456</code><strong> 没有 </strong>公共前缀。</p>
|
||||
|
||||
<p>你需要找出属于 <code>arr1</code> 的整数 <code>x</code> 和属于 <code>arr2</code> 的整数 <code>y</code> 组成的所有数对 <code>(x, y)</code> 之中最长的公共前缀的长度。</p>
|
||||
|
||||
<p>返回所有数对之中最长公共前缀的长度。如果它们之间不存在公共前缀,则返回 <code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr1 = [1,10,100], arr2 = [1000]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>存在 3 个数对 (arr1[i], arr2[j]) :
|
||||
- (1, 1000) 的最长公共前缀是 1 。
|
||||
- (10, 1000) 的最长公共前缀是 10 。
|
||||
- (100, 1000) 的最长公共前缀是 100 。
|
||||
最长的公共前缀是 100 ,长度为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr1 = [1,2,3], arr2 = [4,4,4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>任何数对 (arr1[i], arr2[j]) 之中都不存在公共前缀,因此返回 0 。
|
||||
请注意,同一个数组内元素之间的公共前缀不在考虑范围内。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr1[i], arr2[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
42
leetcode-cn/problem (Chinese)/相同分数的最大操作数目 I [maximum-number-of-operations-with-the-same-score-i].html
Normal file
42
leetcode-cn/problem (Chinese)/相同分数的最大操作数目 I [maximum-number-of-operations-with-the-same-score-i].html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,如果 <code>nums</code> <strong>至少</strong> 包含 <code>2</code> 个元素,你可以执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择 <code>nums</code> 中的前两个元素并将它们删除。</li>
|
||||
</ul>
|
||||
|
||||
<p>一次操作的 <strong>分数</strong> 是被删除元素的和。</p>
|
||||
|
||||
<p>在确保<strong> 所有操作分数相同</strong> 的前提下,请你求出 <strong>最多</strong> 能进行多少次操作。</p>
|
||||
|
||||
<p>请你返回按照上述要求 <strong>最多</strong> 可以进行的操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,1,4,5]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们执行以下操作:
|
||||
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [1,4,5] 。
|
||||
- 删除前两个元素,分数为 1 + 4 = 5 ,nums = [5] 。
|
||||
由于只剩下 1 个元素,我们无法继续进行任何操作。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,6,1,4]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们执行以下操作:
|
||||
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
|
||||
由于下一次操作的分数与前一次不相等,我们无法继续进行任何操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
47
leetcode-cn/problem (Chinese)/相同分数的最大操作数目 II [maximum-number-of-operations-with-the-same-score-ii].html
Normal file
47
leetcode-cn/problem (Chinese)/相同分数的最大操作数目 II [maximum-number-of-operations-with-the-same-score-ii].html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,如果 <code>nums</code> <strong>至少</strong> 包含 <code>2</code> 个元素,你可以执行以下操作中的 <strong>任意</strong> 一个:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择 <code>nums</code> 中最前面两个元素并且删除它们。</li>
|
||||
<li>选择 <code>nums</code> 中最后两个元素并且删除它们。</li>
|
||||
<li>选择 <code>nums</code> 中第一个和最后一个元素并且删除它们。</li>
|
||||
</ul>
|
||||
|
||||
<p>一次操作的 <strong>分数</strong> 是被删除元素的和。</p>
|
||||
|
||||
<p>在确保<strong> 所有操作分数相同</strong> 的前提下,请你求出 <strong>最多</strong> 能进行多少次操作。</p>
|
||||
|
||||
<p>请你返回按照上述要求 <strong>最多</strong> 可以进行的操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,1,2,3,4]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>我们执行以下操作:
|
||||
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [1,2,3,4] 。
|
||||
- 删除第一个元素和最后一个元素,分数为 1 + 4 = 5 ,nums = [2,3] 。
|
||||
- 删除第一个元素和最后一个元素,分数为 2 + 3 = 5 ,nums = [] 。
|
||||
由于 nums 为空,我们无法继续进行任何操作。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,6,1,4]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们执行以下操作:
|
||||
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
|
||||
- 删除最后两个元素,分数为 1 + 4 = 5 ,nums = [6] 。
|
||||
至多进行 2 次操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>
|
||||
|
||||
<p>定义一个 <strong>布尔 </strong>函数 <code>isPrefixAndSuffix</code> ,它接受两个字符串参数 <code>str1</code> 和 <code>str2</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>当 <code>str1</code> 同时是 <code>str2</code> 的前缀(<span data-keyword="string-prefix">prefix</span>)和后缀(<span data-keyword="string-suffix">suffix</span>)时,<code>isPrefixAndSuffix(str1, str2)</code> 返回 <code>true</code>,否则返回 <code>false</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,<code>isPrefixAndSuffix("aba", "ababa")</code> 返回 <code>true</code>,因为 <code>"aba"</code> 既是 <code>"ababa"</code> 的前缀,也是 <code>"ababa"</code> 的后缀,但是 <code>isPrefixAndSuffix("abc", "abcd")</code> 返回<code> false</code>。</p>
|
||||
|
||||
<p>以整数形式,返回满足 <code>i < j</code> 且 <code>isPrefixAndSuffix(words[i], words[j])</code> 为 <code>true</code> 的下标对 <code>(i, j)</code> 的<strong> 数量 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["a","aba","ababa","aa"]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>在本示例中,计数的下标对包括:
|
||||
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("a", "aba") 为 true 。
|
||||
i = 0 且 j = 2 ,因为 isPrefixAndSuffix("a", "ababa") 为 true 。
|
||||
i = 0 且 j = 3 ,因为 isPrefixAndSuffix("a", "aa") 为 true 。
|
||||
i = 1 且 j = 2 ,因为 isPrefixAndSuffix("aba", "ababa") 为 true 。
|
||||
因此,答案是 4 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["pa","papa","ma","mama"]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在本示例中,计数的下标对包括:
|
||||
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("pa", "papa") 为 true 。
|
||||
i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
|
||||
因此,答案是 2 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["abab","ab"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>在本示例中,唯一有效的下标对是 i = 0 且 j = 1 ,但是 isPrefixAndSuffix("abab", "ab") 为 false 。
|
||||
因此,答案是 0 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>
|
||||
|
||||
<p>定义一个 <strong>布尔 </strong>函数 <code>isPrefixAndSuffix</code> ,它接受两个字符串参数 <code>str1</code> 和 <code>str2</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>当 <code>str1</code> 同时是 <code>str2</code> 的前缀(<span data-keyword="string-prefix">prefix</span>)和后缀(<span data-keyword="string-suffix">suffix</span>)时,<code>isPrefixAndSuffix(str1, str2)</code> 返回 <code>true</code>,否则返回 <code>false</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,<code>isPrefixAndSuffix("aba", "ababa")</code> 返回 <code>true</code>,因为 <code>"aba"</code> 既是 <code>"ababa"</code> 的前缀,也是 <code>"ababa"</code> 的后缀,但是 <code>isPrefixAndSuffix("abc", "abcd")</code> 返回<code> false</code>。</p>
|
||||
|
||||
<p>以整数形式,返回满足 <code>i < j</code> 且 <code>isPrefixAndSuffix(words[i], words[j])</code> 为 <code>true</code> 的下标对 <code>(i, j)</code> 的<strong> 数量 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["a","aba","ababa","aa"]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>在本示例中,计数的下标对包括:
|
||||
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("a", "aba") 为 true 。
|
||||
i = 0 且 j = 2 ,因为 isPrefixAndSuffix("a", "ababa") 为 true 。
|
||||
i = 0 且 j = 3 ,因为 isPrefixAndSuffix("a", "aa") 为 true 。
|
||||
i = 1 且 j = 2 ,因为 isPrefixAndSuffix("aba", "ababa") 为 true 。
|
||||
因此,答案是 4 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["pa","papa","ma","mama"]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在本示例中,计数的下标对包括:
|
||||
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("pa", "papa") 为 true 。
|
||||
i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
|
||||
因此,答案是 2 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["abab","ab"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>在本示例中,唯一有效的下标对是 i = 0 且 j = 1 ,但是 isPrefixAndSuffix("abab", "ab") 为 false 。
|
||||
因此,答案是 0 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= words[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
|
||||
<li>所有 <code>words[i]</code> 的长度之和不超过 <code>5 * 10<sup>5</sup></code> 。</li>
|
||||
</ul>
|
42
leetcode-cn/problem (Chinese)/进行操作使字符串为空 [apply-operations-to-make-string-empty].html
Normal file
42
leetcode-cn/problem (Chinese)/进行操作使字符串为空 [apply-operations-to-make-string-empty].html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>给你一个字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>请你进行以下操作直到 <code>s</code> 为 <strong>空</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>每次操作 <strong>依次</strong> 遍历 <code>'a'</code> 到 <code>'z'</code>,如果当前字符出现在 <code>s</code> 中,那么删除出现位置 <strong>最早</strong> 的该字符。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回进行 <strong>最后</strong> 一次操作 <strong>之前</strong> 的字符串<em> </em><code>s</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aabcbbca"
|
||||
<b>输出:</b>"ba"
|
||||
<b>解释:</b>我们进行以下操作:
|
||||
- 删除 s = "<em><strong>a</strong></em>a<em><strong>bc</strong></em>bbca" 中加粗加斜字符,得到字符串 s = "abbca" 。
|
||||
- 删除 s = "<em><strong>ab</strong></em>b<em><strong>c</strong></em>a" 中加粗加斜字符,得到字符串 s = "ba" 。
|
||||
- 删除 s = "<em><strong>ba</strong></em>" 中加粗加斜字符,得到字符串 s = "" 。
|
||||
进行最后一次操作之前的字符串为 "ba" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcd"
|
||||
<b>输出:</b>"abcd"
|
||||
<b>解释:</b>我们进行以下操作:
|
||||
- 删除 s = "<em><strong>abcd</strong></em>" 中加粗加斜字符,得到字符串 s = "" 。
|
||||
进行最后一次操作之前的字符串为 "abcd" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>3</code> which can form the sides of a triangle.</p>
|
||||
|
||||
<ul>
|
||||
<li>A triangle is called <strong>equilateral</strong> if it has all sides of equal length.</li>
|
||||
<li>A triangle is called <strong>isosceles</strong> if it has exactly two sides of equal length.</li>
|
||||
<li>A triangle is called <strong>scalene</strong> if all its sides are of different lengths.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a string representing</em> <em>the type of triangle that can be formed </em><em>or </em><code>"none"</code><em> if it <strong>cannot</strong> form a triangle.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3]
|
||||
<strong>Output:</strong> "equilateral"
|
||||
<strong>Explanation:</strong> Since all the sides are of equal length, therefore, it will form an equilateral triangle.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5]
|
||||
<strong>Output:</strong> "scalene"
|
||||
<strong>Explanation:</strong>
|
||||
nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5.
|
||||
nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4.
|
||||
nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3.
|
||||
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
|
||||
As all the sides are of different lengths, it will form a scalene triangle.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
33
leetcode-cn/problem (English)/修改数组后最大化数组中的连续元素数目(English) [maximize-consecutive-elements-in-an-array-after-modification].html
Normal file
33
leetcode-cn/problem (English)/修改数组后最大化数组中的连续元素数目(English) [maximize-consecutive-elements-in-an-array-after-modification].html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>Initially, you can increase the value of <strong>any</strong> element in the array by <strong>at most</strong> <code>1</code>.</p>
|
||||
|
||||
<p>After that, you need to select <strong>one or more</strong> elements from the final array such that those elements are <strong>consecutive</strong> when sorted in increasing order. For example, the elements <code>[3, 4, 5]</code> are consecutive while <code>[3, 4, 6]</code> and <code>[1, 1, 2, 3]</code> are not.<!-- notionvc: 312f8c5d-40d0-4cd1-96cc-9e96a846735b --></p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of elements that you can select</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,5,1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
|
||||
We select the elements [<u><strong>3</strong></u>,<u><strong>1</strong></u>,5,<u><strong>2</strong></u>,1] and we sort them to obtain [1,2,3], which are consecutive.
|
||||
It can be shown that we cannot select more than 3 consecutive elements.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,7,10]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The maximum consecutive elements that we can select is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>matrix</code>, create a new <strong>0-indexed</strong> matrix called <code>answer</code>. Make <code>answer</code> equal to <code>matrix</code>, then replace each element with the value <code>-1</code> with the <strong>maximum</strong> element in its respective column.</p>
|
||||
|
||||
<p>Return <em>the matrix</em> <code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix1.png" style="width: 491px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
|
||||
<strong>Output:</strong> [[1,2,9],[4,8,6],[7,8,9]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
|
||||
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix2.png" style="width: 411px; height: 111px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[3,-1],[5,2]]
|
||||
<strong>Output:</strong> [[3,2],[5,2]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == matrix.length</code></li>
|
||||
<li><code>n == matrix[i].length</code></li>
|
||||
<li><code>2 <= m, n <= 50</code></li>
|
||||
<li><code>-1 <= matrix[i][j] <= 100</code></li>
|
||||
<li>The input is generated such that each column contains at least one non-negative integer.</li>
|
||||
</ul>
|
@ -0,0 +1,64 @@
|
||||
<p>You are given a <code>m x n</code> <strong>0-indexed </strong>2D<strong> </strong>matrix <code>mat</code>. From every cell, you can create numbers in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>There could be at most <code>8</code> paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east.</li>
|
||||
<li>Select a path from them and append digits in this path to the number being formed by traveling in this direction.</li>
|
||||
<li>Note that numbers are generated at every step, for example, if the digits along the path are <code>1, 9, 1</code>, then there will be three numbers generated along the way: <code>1, 19, 191</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the most frequent <span data-keyword="prime-number">prime number</span> <strong>greater</strong> than </em><code>10</code><em> out of all the numbers created by traversing the matrix or </em><code>-1</code><em> if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the <b>largest</b> among them.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> It is invalid to change the direction during the move.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2024/02/15/south" style="width: 641px; height: 291px;" /> </strong>
|
||||
|
||||
<pre>
|
||||
<strong>
|
||||
Input:</strong> mat = [[1,1],[9,9],[1,1]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong>
|
||||
From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are:
|
||||
East: [11], South-East: [19], South: [19,191].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191].
|
||||
The most frequent prime number among all the created numbers is 19.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[7]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[9,7,8],[4,6,5],[2,8,6]]
|
||||
<strong>Output:</strong> 97
|
||||
<strong>Explanation:</strong>
|
||||
Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79].
|
||||
Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68].
|
||||
Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85].
|
||||
Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658].
|
||||
The most frequent prime number among all the created numbers is 97.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 6</code></li>
|
||||
<li><code>1 <= mat[i][j] <= 9</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/匹配模式数组的子数组数目 I(English) [number-of-subarrays-that-match-a-pattern-i].html
Normal file
40
leetcode-cn/problem (English)/匹配模式数组的子数组数目 I(English) [number-of-subarrays-that-match-a-pattern-i].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/匹配模式数组的子数组数目 II(English) [number-of-subarrays-that-match-a-pattern-ii].html
Normal file
40
leetcode-cn/problem (English)/匹配模式数组的子数组数目 II(English) [number-of-subarrays-that-match-a-pattern-ii].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
53
leetcode-cn/problem (English)/回文字符串的最大数量(English) [maximum-palindromes-after-operations].html
Normal file
53
leetcode-cn/problem (English)/回文字符串的最大数量(English) [maximum-palindromes-after-operations].html
Normal file
@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code> having length <code>n</code> and containing <strong>0-indexed</strong> strings.</p>
|
||||
|
||||
<p>You are allowed to perform the following operation <strong>any</strong> number of times (<strong>including</strong> <strong>zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose integers <code>i</code>, <code>j</code>, <code>x</code>, and <code>y</code> such that <code>0 <= i, j < n</code>, <code>0 <= x < words[i].length</code>, <code>0 <= y < words[j].length</code>, and <strong>swap</strong> the characters <code>words[i][x]</code> and <code>words[j][y]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> number of <span data-keyword="palindrome-string">palindromes</span> </em><code>words</code><em> can contain, after performing some operations.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be equal during an operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abbb","ba","aa"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
|
||||
All strings in words are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 3.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","ab"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
|
||||
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
|
||||
Both strings are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cd","ef","a"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, there is no need to perform any operation.
|
||||
There is one palindrome in words "a".
|
||||
It can be shown that it is not possible to get more than one palindrome after any number of operations.
|
||||
Hence, the answer is 1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
39
leetcode-cn/problem (English)/最长公共前缀的长度(English) [find-the-length-of-the-longest-common-prefix].html
Normal file
39
leetcode-cn/problem (English)/最长公共前缀的长度(English) [find-the-length-of-the-longest-common-prefix].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p>
|
||||
|
||||
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p>
|
||||
|
||||
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have a common prefix <code>565</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p>
|
||||
|
||||
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]):
|
||||
- The longest common prefix of (1, 1000) is 1.
|
||||
- The longest common prefix of (10, 1000) is 10.
|
||||
- The longest common prefix of (100, 1000) is 100.
|
||||
The longest common prefix is 100 with a length of 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
|
||||
Note that common prefixes between elements of the same array do not count.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr1[i], arr2[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/相同分数的最大操作数目 I(English) [maximum-number-of-operations-with-the-same-score-i].html
Normal file
40
leetcode-cn/problem (English)/相同分数的最大操作数目 I(English) [maximum-number-of-operations-with-the-same-score-i].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
|
||||
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
|
||||
We are unable to perform any more operations as nums contain only 1 element.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
45
leetcode-cn/problem (English)/相同分数的最大操作数目 II(English) [maximum-number-of-operations-with-the-same-score-ii].html
Normal file
45
leetcode-cn/problem (English)/相同分数的最大操作数目 II(English) [maximum-number-of-operations-with-the-same-score-ii].html
Normal file
@ -0,0 +1,45 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform <strong>any</strong> of the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the last two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the first and the last elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
|
||||
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
|
||||
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
|
||||
We are unable to perform any more operations as nums is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
|
||||
It can be proven that we can perform at most 2 operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (English)/统计前后缀下标对 I(English) [count-prefix-and-suffix-pairs-i].html
Normal file
51
leetcode-cn/problem (English)/统计前后缀下标对 I(English) [count-prefix-and-suffix-pairs-i].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i, j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
52
leetcode-cn/problem (English)/统计前后缀下标对 II(English) [count-prefix-and-suffix-pairs-ii].html
Normal file
52
leetcode-cn/problem (English)/统计前后缀下标对 II(English) [count-prefix-and-suffix-pairs-ii].html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i<em>, </em>j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= words[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>The sum of the lengths of all <code>words[i]</code> does not exceed <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
44
leetcode-cn/problem (English)/进行操作使字符串为空(English) [apply-operations-to-make-string-empty].html
Normal file
44
leetcode-cn/problem (English)/进行操作使字符串为空(English) [apply-operations-to-make-string-empty].html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are given a string <code>s</code>.</p>
|
||||
|
||||
<p>Consider performing the following operation until <code>s</code> becomes <strong>empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <strong>every</strong> alphabet character from <code>'a'</code> to <code>'z'</code>, remove the <strong>first</strong> occurrence of that character in <code>s</code> (if it exists).</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, let initially <code>s = "aabcbbca"</code>. We do the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>a</strong></u>a<strong><u>bc</u></strong>bbca"</code>. The resulting string is <code>s = "abbca"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ab</strong></u>b<u><strong>c</strong></u>a"</code>. The resulting string is <code>s = "ba"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ba</strong></u>"</code>. The resulting string is <code>s = ""</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the value of the string </em><code>s</code><em> right <strong>before</strong> applying the <strong>last</strong> operation</em>. In the example above, answer is <code>"ba"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabcbbca"
|
||||
<strong>Output:</strong> "ba"
|
||||
<strong>Explanation:</strong> Explained in the statement.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd"
|
||||
<strong>Output:</strong> "abcd"
|
||||
<strong>Explanation:</strong> We do the following operation:
|
||||
- Remove the underlined characters s = "<u><strong>abcd</strong></u>". The resulting string is s = "".
|
||||
The string just before the last operation is "abcd".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
161
leetcode/originData/apply-operations-to-make-string-empty.json
Normal file
161
leetcode/originData/apply-operations-to-make-string-empty.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/count-prefix-and-suffix-pairs-i.json
Normal file
161
leetcode/originData/count-prefix-and-suffix-pairs-i.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/count-prefix-and-suffix-pairs-ii.json
Normal file
163
leetcode/originData/count-prefix-and-suffix-pairs-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
161
leetcode/originData/maximize-consecutive-elements-in-an-array-after-modification.json
Normal file
161
leetcode/originData/maximize-consecutive-elements-in-an-array-after-modification.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
202
leetcode/originData/maximum-palindromes-after-operations.json
Normal file
202
leetcode/originData/maximum-palindromes-after-operations.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode/originData/modify-the-matrix.json
Normal file
171
leetcode/originData/modify-the-matrix.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/most-frequent-prime.json
Normal file
160
leetcode/originData/most-frequent-prime.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
180
leetcode/originData/type-of-triangle.json
Normal file
180
leetcode/originData/type-of-triangle.json
Normal file
File diff suppressed because one or more lines are too long
44
leetcode/problem/apply-operations-to-make-string-empty.html
Normal file
44
leetcode/problem/apply-operations-to-make-string-empty.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are given a string <code>s</code>.</p>
|
||||
|
||||
<p>Consider performing the following operation until <code>s</code> becomes <strong>empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <strong>every</strong> alphabet character from <code>'a'</code> to <code>'z'</code>, remove the <strong>first</strong> occurrence of that character in <code>s</code> (if it exists).</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, let initially <code>s = "aabcbbca"</code>. We do the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>a</strong></u>a<strong><u>bc</u></strong>bbca"</code>. The resulting string is <code>s = "abbca"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ab</strong></u>b<u><strong>c</strong></u>a"</code>. The resulting string is <code>s = "ba"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ba</strong></u>"</code>. The resulting string is <code>s = ""</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the value of the string </em><code>s</code><em> right <strong>before</strong> applying the <strong>last</strong> operation</em>. In the example above, answer is <code>"ba"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabcbbca"
|
||||
<strong>Output:</strong> "ba"
|
||||
<strong>Explanation:</strong> Explained in the statement.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd"
|
||||
<strong>Output:</strong> "abcd"
|
||||
<strong>Explanation:</strong> We do the following operation:
|
||||
- Remove the underlined characters s = "<u><strong>abcd</strong></u>". The resulting string is s = "".
|
||||
The string just before the last operation is "abcd".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
51
leetcode/problem/count-prefix-and-suffix-pairs-i.html
Normal file
51
leetcode/problem/count-prefix-and-suffix-pairs-i.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i, j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
52
leetcode/problem/count-prefix-and-suffix-pairs-ii.html
Normal file
52
leetcode/problem/count-prefix-and-suffix-pairs-ii.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i<em>, </em>j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= words[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>The sum of the lengths of all <code>words[i]</code> does not exceed <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p>
|
||||
|
||||
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p>
|
||||
|
||||
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have a common prefix <code>565</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p>
|
||||
|
||||
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]):
|
||||
- The longest common prefix of (1, 1000) is 1.
|
||||
- The longest common prefix of (10, 1000) is 10.
|
||||
- The longest common prefix of (100, 1000) is 100.
|
||||
The longest common prefix is 100 with a length of 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
|
||||
Note that common prefixes between elements of the same array do not count.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr1[i], arr2[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>Initially, you can increase the value of <strong>any</strong> element in the array by <strong>at most</strong> <code>1</code>.</p>
|
||||
|
||||
<p>After that, you need to select <strong>one or more</strong> elements from the final array such that those elements are <strong>consecutive</strong> when sorted in increasing order. For example, the elements <code>[3, 4, 5]</code> are consecutive while <code>[3, 4, 6]</code> and <code>[1, 1, 2, 3]</code> are not.<!-- notionvc: 312f8c5d-40d0-4cd1-96cc-9e96a846735b --></p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of elements that you can select</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,5,1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
|
||||
We select the elements [<u><strong>3</strong></u>,<u><strong>1</strong></u>,5,<u><strong>2</strong></u>,1] and we sort them to obtain [1,2,3], which are consecutive.
|
||||
It can be shown that we cannot select more than 3 consecutive elements.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,7,10]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The maximum consecutive elements that we can select is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
|
||||
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
|
||||
We are unable to perform any more operations as nums contain only 1 element.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform <strong>any</strong> of the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the last two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the first and the last elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
|
||||
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
|
||||
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
|
||||
We are unable to perform any more operations as nums is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
|
||||
It can be proven that we can perform at most 2 operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
53
leetcode/problem/maximum-palindromes-after-operations.html
Normal file
53
leetcode/problem/maximum-palindromes-after-operations.html
Normal file
@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code> having length <code>n</code> and containing <strong>0-indexed</strong> strings.</p>
|
||||
|
||||
<p>You are allowed to perform the following operation <strong>any</strong> number of times (<strong>including</strong> <strong>zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose integers <code>i</code>, <code>j</code>, <code>x</code>, and <code>y</code> such that <code>0 <= i, j < n</code>, <code>0 <= x < words[i].length</code>, <code>0 <= y < words[j].length</code>, and <strong>swap</strong> the characters <code>words[i][x]</code> and <code>words[j][y]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> number of <span data-keyword="palindrome-string">palindromes</span> </em><code>words</code><em> can contain, after performing some operations.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be equal during an operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abbb","ba","aa"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
|
||||
All strings in words are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 3.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","ab"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
|
||||
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
|
||||
Both strings are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cd","ef","a"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, there is no need to perform any operation.
|
||||
There is one palindrome in words "a".
|
||||
It can be shown that it is not possible to get more than one palindrome after any number of operations.
|
||||
Hence, the answer is 1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
33
leetcode/problem/modify-the-matrix.html
Normal file
33
leetcode/problem/modify-the-matrix.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>matrix</code>, create a new <strong>0-indexed</strong> matrix called <code>answer</code>. Make <code>answer</code> equal to <code>matrix</code>, then replace each element with the value <code>-1</code> with the <strong>maximum</strong> element in its respective column.</p>
|
||||
|
||||
<p>Return <em>the matrix</em> <code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix1.png" style="width: 491px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
|
||||
<strong>Output:</strong> [[1,2,9],[4,8,6],[7,8,9]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
|
||||
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix2.png" style="width: 411px; height: 111px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[3,-1],[5,2]]
|
||||
<strong>Output:</strong> [[3,2],[5,2]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == matrix.length</code></li>
|
||||
<li><code>n == matrix[i].length</code></li>
|
||||
<li><code>2 <= m, n <= 50</code></li>
|
||||
<li><code>-1 <= matrix[i][j] <= 100</code></li>
|
||||
<li>The input is generated such that each column contains at least one non-negative integer.</li>
|
||||
</ul>
|
64
leetcode/problem/most-frequent-prime.html
Normal file
64
leetcode/problem/most-frequent-prime.html
Normal file
@ -0,0 +1,64 @@
|
||||
<p>You are given a <code>m x n</code> <strong>0-indexed </strong>2D<strong> </strong>matrix <code>mat</code>. From every cell, you can create numbers in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>There could be at most <code>8</code> paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east.</li>
|
||||
<li>Select a path from them and append digits in this path to the number being formed by traveling in this direction.</li>
|
||||
<li>Note that numbers are generated at every step, for example, if the digits along the path are <code>1, 9, 1</code>, then there will be three numbers generated along the way: <code>1, 19, 191</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the most frequent <span data-keyword="prime-number">prime number</span> <strong>greater</strong> than </em><code>10</code><em> out of all the numbers created by traversing the matrix or </em><code>-1</code><em> if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the <b>largest</b> among them.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> It is invalid to change the direction during the move.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2024/02/15/south" style="width: 641px; height: 291px;" /> </strong>
|
||||
|
||||
<pre>
|
||||
<strong>
|
||||
Input:</strong> mat = [[1,1],[9,9],[1,1]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong>
|
||||
From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are:
|
||||
East: [11], South-East: [19], South: [19,191].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191].
|
||||
The most frequent prime number among all the created numbers is 19.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[7]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[9,7,8],[4,6,5],[2,8,6]]
|
||||
<strong>Output:</strong> 97
|
||||
<strong>Explanation:</strong>
|
||||
Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79].
|
||||
Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68].
|
||||
Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85].
|
||||
Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658].
|
||||
The most frequent prime number among all the created numbers is 97.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 6</code></li>
|
||||
<li><code>1 <= mat[i][j] <= 9</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
39
leetcode/problem/type-of-triangle.html
Normal file
39
leetcode/problem/type-of-triangle.html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>3</code> which can form the sides of a triangle.</p>
|
||||
|
||||
<ul>
|
||||
<li>A triangle is called <strong>equilateral</strong> if it has all sides of equal length.</li>
|
||||
<li>A triangle is called <strong>isosceles</strong> if it has exactly two sides of equal length.</li>
|
||||
<li>A triangle is called <strong>scalene</strong> if all its sides are of different lengths.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a string representing</em> <em>the type of triangle that can be formed </em><em>or </em><code>"none"</code><em> if it <strong>cannot</strong> form a triangle.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3]
|
||||
<strong>Output:</strong> "equilateral"
|
||||
<strong>Explanation:</strong> Since all the sides are of equal length, therefore, it will form an equilateral triangle.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5]
|
||||
<strong>Output:</strong> "scalene"
|
||||
<strong>Explanation:</strong>
|
||||
nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5.
|
||||
nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4.
|
||||
nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3.
|
||||
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
|
||||
As all the sides are of different lengths, it will form a scalene triangle.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user