mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
a697596e35
commit
6bfbd3556c
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2024.01.30**
|
||||
> 最后更新日期: **2024.02.09**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `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
182
leetcode-cn/originData/ant-on-the-boundary.json
Normal file
182
leetcode-cn/originData/ant-on-the-boundary.json
Normal file
File diff suppressed because one or more lines are too long
178
leetcode-cn/originData/find-the-grid-of-region-average.json
Normal file
178
leetcode-cn/originData/find-the-grid-of-region-average.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
184
leetcode-cn/originData/maximum-good-subarray-sum.json
Normal file
184
leetcode-cn/originData/maximum-good-subarray-sum.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-ii.json
Normal file
183
leetcode-cn/originData/type-of-triangle-ii.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,41 @@
|
||||
<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<code> 。</code>
|
||||
nums[0] + nums[2] = 3 + 5 = 8 ,大于 nums[1] = 4 。
|
||||
nums[1] + nums[2] = 4 + 5 = 9 ,大于 nums[0] = 3 。
|
||||
由于任意两边纸盒都大于第三边,所以可以构成一个三角形。
|
||||
因为三条边的长度互不相等,所以返回 "scalene" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,64 @@
|
||||
<p>给你一个 <code>n x 2</code> 的二维数组 <code>points</code> ,它表示二维平面上的一些点坐标,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>我们定义 x 轴的正方向为 <strong>右</strong> (<strong>x 轴递增的方向</strong>),x 轴的负方向为 <strong>左</strong> (<strong>x 轴递减的方向</strong>)。类似的,我们定义 y 轴的正方向为 <strong>上</strong> (<strong>y 轴递增的方向</strong>),y 轴的负方向为 <strong>下</strong> (<strong>y 轴递减的方向</strong>)。</p>
|
||||
|
||||
<p>你需要安排这 <code>n</code> 个人的站位,这 <code>n</code> 个人中包括 liupengsay 和小羊肖恩 。你需要确保每个点处 <strong>恰好</strong> 有 <strong>一个</strong> 人。同时,liupengsay 想跟小羊肖恩单独玩耍,所以 liupengsay 会以 liupengsay<b> </b>的坐标为 <strong>左上角</strong> ,小羊肖恩的坐标为 <strong>右下角</strong> 建立一个矩形的围栏(<strong>注意</strong>,围栏可能 <strong>不</strong> 包含任何区域,也就是说围栏可能是一条线段)。如果围栏的 <strong>内部</strong> 或者 <strong>边缘</strong> 上有任何其他人,liupengsay 都会难过。</p>
|
||||
|
||||
<p>请你在确保 liupengsay <strong>不会</strong> 难过的前提下,返回 liupengsay 和小羊肖恩可以选择的 <strong>点对</strong> 数目。</p>
|
||||
|
||||
<p><b>注意</b>,liupengsay 建立的围栏必须确保 liupengsay 的位置是矩形的左上角,小羊肖恩的位置是矩形的右下角。比方说,以 <code>(1, 1)</code> ,<code>(1, 3)</code> ,<code>(3, 1)</code> 和 <code>(3, 3)</code> 为矩形的四个角,给定下图的两个输入,liupengsay 都不能建立围栏,原因如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>图一中,liupengsay 在 <code>(3, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,liupengsay 的位置不是左上角且小羊肖恩的位置不是右下角。</li>
|
||||
<li>图二中,liupengsay 在 <code>(1, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,小羊肖恩的位置不是在围栏的右下角。</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[1,1],[2,2],[3,3]]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>没有办法可以让 liupengsay 的围栏以 liupengsay 的位置为左上角且小羊肖恩的位置为右下角。所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg" style="width: 900px; height: 247px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[6,2],[4,4],[2,6]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (4, 4) ,小羊肖恩站在 (6, 2) 。
|
||||
- liupengsay 站在 (2, 6) ,小羊肖恩站在 (4, 4) 。
|
||||
不能安排 liupengsay 站在 (2, 6) 且小羊肖恩站在 (6, 2) ,因为站在 (4, 4) 的人处于围栏内。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg" style="width: 900px; height: 247px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[1,3],[1,1]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (1, 1) ,小羊肖恩站在 (3, 1) 。
|
||||
- liupengsay 站在 (1, 3) ,小羊肖恩站在 (1, 1) 。
|
||||
不能安排 liupengsay 站在 (1, 3) 且小羊肖恩站在 (3, 1) ,因为站在 (1, 1) 的人处于围栏内。
|
||||
注意围栏是可以不包含任何面积的,上图中第一和第二个围栏都是合法的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= points[i][0], points[i][1] <= 50</code></li>
|
||||
<li><code>points[i]</code> 点对两两不同。</li>
|
||||
</ul>
|
@ -0,0 +1,64 @@
|
||||
<p>给你一个 <code>n x 2</code> 的二维数组 <code>points</code> ,它表示二维平面上的一些点坐标,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>我们定义 x 轴的正方向为 <strong>右</strong> (<strong>x 轴递增的方向</strong>),x 轴的负方向为 <strong>左</strong> (<strong>x 轴递减的方向</strong>)。类似的,我们定义 y 轴的正方向为 <strong>上</strong> (<strong>y 轴递增的方向</strong>),y 轴的负方向为 <strong>下</strong> (<strong>y 轴递减的方向</strong>)。</p>
|
||||
|
||||
<p>你需要安排这 <code>n</code> 个人的站位,这 <code>n</code> 个人中包括 liupengsay 和小羊肖恩 。你需要确保每个点处 <strong>恰好</strong> 有 <strong>一个</strong> 人。同时,liupengsay 想跟小羊肖恩单独玩耍,所以 liupengsay 会以 liupengsay<b> </b>的坐标为 <strong>左上角</strong> ,小羊肖恩的坐标为 <strong>右下角</strong> 建立一个矩形的围栏(<strong>注意</strong>,围栏可能 <strong>不</strong> 包含任何区域,也就是说围栏可能是一条线段)。如果围栏的 <strong>内部</strong> 或者 <strong>边缘</strong> 上有任何其他人,liupengsay 都会难过。</p>
|
||||
|
||||
<p>请你在确保 liupengsay <strong>不会</strong> 难过的前提下,返回 liupengsay 和小羊肖恩可以选择的 <strong>点对</strong> 数目。</p>
|
||||
|
||||
<p><b>注意</b>,liupengsay 建立的围栏必须确保 liupengsay 的位置是矩形的左上角,小羊肖恩的位置是矩形的右下角。比方说,以 <code>(1, 1)</code> ,<code>(1, 3)</code> ,<code>(3, 1)</code> 和 <code>(3, 3)</code> 为矩形的四个角,给定下图的两个输入,liupengsay 都不能建立围栏,原因如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>图一中,liupengsay 在 <code>(3, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,liupengsay 的位置不是左上角且小羊肖恩的位置不是右下角。</li>
|
||||
<li>图二中,liupengsay 在 <code>(1, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,小羊肖恩的位置不是在围栏的右下角。</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[1,1],[2,2],[3,3]]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>没有办法可以让 liupengsay 的围栏以 liupengsay 的位置为左上角且小羊肖恩的位置为右下角。所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg" style="width: 900px; height: 250px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[6,2],[4,4],[2,6]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (4, 4) ,小羊肖恩站在 (6, 2) 。
|
||||
- liupengsay 站在 (2, 6) ,小羊肖恩站在 (4, 4) 。
|
||||
不能安排 liupengsay 站在 (2, 6) 且小羊肖恩站在 (6, 2) ,因为站在 (4, 4) 的人处于围栏内。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg" style="width: 911px; height: 250px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[1,3],[1,1]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (1, 1) ,小羊肖恩站在 (3, 1) 。
|
||||
- liupengsay 站在 (1, 3) ,小羊肖恩站在 (1, 1) 。
|
||||
不能安排 liupengsay 站在 (1, 3) 且小羊肖恩站在 (3, 1) ,因为站在 (1, 1) 的人处于围栏内。
|
||||
注意围栏是可以不包含任何面积的,上图中第一和第二个围栏都是合法的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code></li>
|
||||
<li><code>points[i]</code> 点对两两不同。</li>
|
||||
</ul>
|
@ -0,0 +1,56 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>在每一秒,你必须执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>移除 <code>word</code> 的前 <code>k</code> 个字符。</li>
|
||||
<li>在 <code>word</code> 的末尾添加 <code>k</code> 个任意字符。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意 </strong>添加的字符不必和移除的字符相同。但是,必须在每一秒钟都执行 <strong>两种 </strong>操作。</p>
|
||||
|
||||
<p>返回将 <code>word</code> 恢复到其 <strong>初始 </strong>状态所需的 <strong>最短 </strong>时间(该时间必须大于零)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
第 1 秒,移除 word 的前缀 "aba",并在末尾添加 "bac" 。因此,word 变为 "cababac"。
|
||||
第 2 秒,移除 word 的前缀 "cab",并在末尾添加 "aba" 。因此,word 变为 "abacaba" 并恢复到始状态。
|
||||
可以证明,2 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 4
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>第 1 秒,移除 word 的前缀 "abac",并在末尾添加 "caba" 。因此,word 变为 "abacaba" 并恢复到初始状态。
|
||||
可以证明,1 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abcbabcd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
每一秒,我们都移除 word 的前 2 个字符,并在 word 末尾添加相同的字符。
|
||||
4 秒后,word 变为 "abcbabcd" 并恢复到初始状态。
|
||||
可以证明,4 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 50</code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code>仅由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,56 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>在每一秒,你必须执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>移除 <code>word</code> 的前 <code>k</code> 个字符。</li>
|
||||
<li>在 <code>word</code> 的末尾添加 <code>k</code> 个任意字符。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意 </strong>添加的字符不必和移除的字符相同。但是,必须在每一秒钟都执行 <strong>两种 </strong>操作。</p>
|
||||
|
||||
<p>返回将 <code>word</code> 恢复到其 <strong>初始 </strong>状态所需的 <strong>最短 </strong>时间(该时间必须大于零)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
第 1 秒,移除 word 的前缀 "aba",并在末尾添加 "bac" 。因此,word 变为 "cababac"。
|
||||
第 2 秒,移除 word 的前缀 "cab",并在末尾添加 "aba" 。因此,word 变为 "abacaba" 并恢复到始状态。
|
||||
可以证明,2 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 4
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>第 1 秒,移除 word 的前缀 "abac",并在末尾添加 "caba" 。因此,word 变为 "abacaba" 并恢复到初始状态。
|
||||
可以证明,1 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abcbabcd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
每一秒,我们都移除 word 的前 2 个字符,并在 word 末尾添加相同的字符。
|
||||
4 秒后,word 变为 "abcbabcd" 并恢复到初始状态。
|
||||
可以证明,4 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code>仅由小写英文字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的网格 <code>image</code> ,表示一个灰度图像,其中 <code>image[i][j]</code> 表示在范围 <code>[0..255]</code> 内的某个像素强度。另给你一个<strong> 非负 </strong>整数 <code>threshold</code> 。</p>
|
||||
|
||||
<p>如果 <code>image[a][b]</code> 和 <code>image[c][d]</code> 满足 <code>|a - c| + |b - d| == 1</code> ,则称这两个像素是<strong> 相邻像素</strong> 。</p>
|
||||
|
||||
<p><strong>区域 </strong>是一个 <code>3 x 3</code> 的子网格,且满足区域中任意两个 <strong>相邻</strong> 像素之间,像素强度的<strong> 绝对差 </strong><strong> 小于或等于 </strong><code>threshold</code> 。</p>
|
||||
|
||||
<p><strong>区域</strong> 内的所有像素都认为属于该区域,而一个像素 <strong>可以 </strong>属于 <strong>多个</strong> 区域。</p>
|
||||
|
||||
<p>你需要计算一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的网格 <code>result</code> ,其中 <code>result[i][j]</code> 是 <code>image[i][j]</code> 所属区域的 <strong>平均 </strong>强度,<strong>向下取整 </strong>到最接近的整数。如果 <code>image[i][j]</code> 属于多个区域,<code>result[i][j]</code> 是这些区域的<strong> </strong><strong>“取整后的平均强度”</strong> 的<strong> 平均值</strong>,也 <strong>向下取整 </strong>到最接近的整数。如果 <code>image[i][j]</code> 不属于任何区域,则 <code>result[i][j]</code><strong> 等于 </strong><code>image[i][j]</code> 。</p>
|
||||
|
||||
<p>返回网格 <code>result</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example0corrected.png" style="width: 832px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
|
||||
<strong>输出:</strong>[[9,9,9,9],[9,9,9,9],[9,9,9,9]]
|
||||
<strong>解释:</strong>图像中存在两个区域,如图片中的阴影区域所示。第一个区域的平均强度为 9 ,而第二个区域的平均强度为 9.67 ,向下取整为 9 。两个区域的平均强度为 (9 + 9) / 2 = 9 。由于所有像素都属于区域 1 、区域 2 或两者,因此 result 中每个像素的强度都为 9 。
|
||||
注意,在计算多个区域的平均值时使用了向下取整的值,因此使用区域 2 的平均强度 9 来进行计算,而不是 9.67 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example1corrected.png" style="width: 805px; height: 377px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12
|
||||
<strong>输出:</strong>[[25,25,25],[27,27,27],[27,27,27],[30,30,30]]
|
||||
<strong>解释:</strong>图像中存在两个区域,如图片中的阴影区域所示。第一个区域的平均强度为 25 ,而第二个区域的平均强度为 30 。两个区域的平均强度为 (25 + 30) / 2 = 27.5 ,向下取整为 27 。图像中第 0 行的所有像素属于区域 1 ,因此 result 中第 0 行的所有像素为 25 。同理,result 中第 3 行的所有像素为 30 。图像中第 1 行和第 2 行的像素属于区域 1 和区域 2 ,因此它们在 result 中的值为 27 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1
|
||||
<strong>输出:</strong>[[5,6,7],[8,9,10],[11,12,13]]
|
||||
<strong>解释:</strong>图像中不存在任何区域,因此对于所有像素,result[i][j] == image[i][j] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n, m <= 500</code></li>
|
||||
<li><code>0 <= image[i][j] <= 255</code></li>
|
||||
<li><code>0 <= threshold <= 255</code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个长度为 <code>n</code> 的数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果 <code>nums</code> 的一个子数组中,第一个元素和最后一个元素 <strong>差的绝对值恰好</strong> 为 <code>k</code> ,我们称这个子数组为 <strong>好</strong> 的。换句话说,如果子数组 <code>nums[i..j]</code> 满足 <code>|nums[i] - nums[j]| == k</code> ,那么它是一个好子数组。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>好</strong> 子数组的 <strong>最大</strong> 和,如果没有好子数组,返回<em> </em><code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5,6], k = 1
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 1 。好子数组有 [1,2] ,[2,3] ,[3,4] ,[4,5] 和 [5,6] 。最大子数组和为 11 ,对应的子数组为 [5,6] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-1,3,2,4,5], k = 3
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 3 。好子数组有 [-1,3,2] 和 [2,4,5] 。最大子数组和为 11 ,对应的子数组为 [2,4,5] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-1,-2,-3,-4], k = 2
|
||||
<b>输出:</b>-6
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 2 。好子数组有 [-1,-2,-3] 和 [-2,-3,-4] 。最大子数组和为 -6 ,对应的子数组为 [-1,-2,-3] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>边界上有一只蚂蚁,它有时向 <strong>左 </strong>走,有时向 <strong>右 </strong>走。</p>
|
||||
|
||||
<p>给你一个 <strong>非零</strong> 整数数组 <code>nums</code> 。蚂蚁会按顺序读取 <code>nums</code> 中的元素,从第一个元素开始直到结束。每一步,蚂蚁会根据当前元素的值移动:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>nums[i] < 0</code> ,向 <strong>左</strong> 移动<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code>单位。</li>
|
||||
<li>如果 <code>nums[i] > 0</code> ,向 <strong>右</strong> 移动 <code>nums[i]</code>单位。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回蚂蚁 <strong>返回 </strong>到边界上的次数。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>边界两侧有无限的空间。</li>
|
||||
<li>只有在蚂蚁移动了 <code>|nums[i]|</code> 单位后才检查它是否位于边界上。换句话说,如果蚂蚁只是在移动过程中穿过了边界,则不会计算在内。<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,-5]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 3 步后,蚂蚁位于边界上。
|
||||
所以答案是 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,2,-3,-4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 3 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 3 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 4 步后,蚂蚁距边界左侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
蚂蚁从未返回到边界上,所以答案是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li><code>nums[i] != 0</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>
|
@ -0,0 +1,56 @@
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>We define the <strong>right</strong> direction as positive x-axis (<strong>increasing x-coordinate</strong>) and the <strong>left</strong> direction as negative x-axis (<strong>decreasing x-coordinate</strong>). Similarly, we define the <strong>up</strong> direction as positive y-axis (<strong>increasing y-coordinate</strong>) and the <strong>down</strong> direction as negative y-axis (<strong>decreasing y-coordinate</strong>)</p>
|
||||
|
||||
<p>You have to place <code>n</code> people, including Alice and Bob, at these points such that there is <strong>exactly one</strong> person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the <strong>upper left corner</strong> and Bob's position as the <strong>lower right corner</strong> of the fence (<strong>Note</strong> that the fence <strong>might not</strong> enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either <strong>inside</strong> the fence or <strong>on</strong> the fence, Alice will be sad.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>pairs of points</strong> where you can place Alice and Bob, such that Alice <strong>does not</strong> become sad on building the fence</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners <code>(1, 1)</code>, <code>(1, 3)</code>, <code>(3, 1)</code>, and <code>(3, 3)</code>, because:</p>
|
||||
|
||||
<ul>
|
||||
<li>With Alice at <code>(3, 3)</code> and Bob at <code>(1, 1)</code>, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.</li>
|
||||
<li>With Alice at <code>(1, 3)</code> and Bob at <code>(1, 1)</code>, Bob's position is not the lower right corner of the fence.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png" style="width: 1321px; height: 363px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[6,2],[4,4],[2,6]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (4, 4) and Bob at (6, 2).
|
||||
- Place Alice at (2, 6) and Bob at (4, 4).
|
||||
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png" style="width: 1123px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[1,3],[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (1, 1) and Bob at (3, 1).
|
||||
- Place Alice at (1, 3) and Bob at (1, 1).
|
||||
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
|
||||
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= points[i][0], points[i][1] <= 50</code></li>
|
||||
<li>All <code>points[i]</code> are distinct.</li>
|
||||
</ul>
|
@ -0,0 +1,56 @@
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>We define the <strong>right</strong> direction as positive x-axis (<strong>increasing x-coordinate</strong>) and the <strong>left</strong> direction as negative x-axis (<strong>decreasing x-coordinate</strong>). Similarly, we define the <strong>up</strong> direction as positive y-axis (<strong>increasing y-coordinate</strong>) and the <strong>down</strong> direction as negative y-axis (<strong>decreasing y-coordinate</strong>)</p>
|
||||
|
||||
<p>You have to place <code>n</code> people, including Alice and Bob, at these points such that there is <strong>exactly one</strong> person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the <strong>upper left corner</strong> and Bob's position as the <strong>lower right corner</strong> of the fence (<strong>Note</strong> that the fence <strong>might not</strong> enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either <strong>inside</strong> the fence or <strong>on</strong> the fence, Alice will be sad.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>pairs of points</strong> where you can place Alice and Bob, such that Alice <strong>does not</strong> become sad on building the fence</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners <code>(1, 1)</code>, <code>(1, 3)</code>, <code>(3, 1)</code>, and <code>(3, 3)</code>, because:</p>
|
||||
|
||||
<ul>
|
||||
<li>With Alice at <code>(3, 3)</code> and Bob at <code>(1, 1)</code>, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.</li>
|
||||
<li>With Alice at <code>(1, 3)</code> and Bob at <code>(1, 1)</code>, Bob's position is not the lower right corner of the fence.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png" style="width: 1321px; height: 363px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[6,2],[4,4],[2,6]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (4, 4) and Bob at (6, 2).
|
||||
- Place Alice at (2, 6) and Bob at (4, 4).
|
||||
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png" style="width: 1123px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[1,3],[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (1, 1) and Bob at (3, 1).
|
||||
- Place Alice at (1, 3) and Bob at (1, 1).
|
||||
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
|
||||
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code></li>
|
||||
<li>All <code>points[i]</code> are distinct.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>At every second, you must perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the first <code>k</code> characters of <code>word</code>.</li>
|
||||
<li>Add any <code>k</code> characters to the end of <code>word</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you do not necessarily need to add the same characters that you removed. However, you must perform <strong>both</strong> operations at every second.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time greater than zero required for</em> <code>word</code> <em>to revert to its <strong>initial</strong> state</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
|
||||
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcbabcd", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
|
||||
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
|
||||
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 50 </code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>At every second, you must perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the first <code>k</code> characters of <code>word</code>.</li>
|
||||
<li>Add any <code>k</code> characters to the end of <code>word</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you do not necessarily need to add the same characters that you removed. However, you must perform <strong>both</strong> operations at every second.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time greater than zero required for</em> <code>word</code> <em>to revert to its <strong>initial</strong> state</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
|
||||
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcbabcd", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
|
||||
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
|
||||
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> grid <code>image</code> which represents a grayscale image, where <code>image[i][j]</code> represents a pixel with intensity in the range<code>[0..255]</code>. You are also given a <strong>non-negative</strong> integer <code>threshold</code>.</p>
|
||||
|
||||
<p>Two pixels <code>image[a][b]</code> and <code>image[c][d]</code> are said to be <strong>adjacent</strong> if <code>|a - c| + |b - d| == 1</code>.</p>
|
||||
|
||||
<p>A <strong>region</strong> is a <code>3 x 3</code> subgrid where the <strong>absolute difference</strong> in intensity between any two <strong>adjacent</strong> pixels is <strong>less than or equal to</strong> <code>threshold</code>.</p>
|
||||
|
||||
<p>All pixels in a <strong>region</strong> belong to that region, note that a pixel <strong>can</strong> belong to <strong>multiple</strong> regions.</p>
|
||||
|
||||
<p>You need to calculate a <strong>0-indexed</strong> <code>m x n</code> grid <code>result</code>, where <code>result[i][j]</code> is the <strong>average</strong> intensity of the region to which <code>image[i][j]</code> belongs, <strong>rounded down</strong> to the nearest integer. If <code>image[i][j]</code> belongs to multiple regions, <code>result[i][j]</code> is the <strong>average </strong>of the<strong> rounded down average </strong>intensities of these regions, <strong>rounded down</strong> to the nearest integer. If <code>image[i][j]</code> does<strong> not</strong> belong to any region, <code>result[i][j]</code> is <strong>equal to</strong> <code>image[i][j]</code>.</p>
|
||||
|
||||
<p>Return <em>the grid</em> <code>result</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example0corrected.png" style="width: 832px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
|
||||
<strong>Output:</strong> [[9,9,9,9],[9,9,9,9],[9,9,9,9]]
|
||||
<strong>Explanation:</strong> There exist two regions in the image, which are shown as the shaded areas in the picture. The average intensity of the first region is 9, while the average intensity of the second region is 9.67 which is rounded down to 9. The average intensity of both of the regions is (9 + 9) / 2 = 9. As all the pixels belong to either region 1, region 2, or both of them, the intensity of every pixel in the result is 9.
|
||||
Please note that the rounded-down values are used when calculating the average of multiple regions, hence the calculation is done using 9 as the average intensity of region 2, not 9.67.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example1corrected.png" style="width: 805px; height: 377px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12
|
||||
<strong>Output:</strong> [[25,25,25],[27,27,27],[27,27,27],[30,30,30]]
|
||||
<strong>Explanation:</strong> There exist two regions in the image, which are shown as the shaded areas in the picture. The average intensity of the first region is 25, while the average intensity of the second region is 30. The average intensity of both of the regions is (25 + 30) / 2 = 27.5 which is rounded down to 27. All the pixels in row 0 of the image belong to region 1, hence all the pixels in row 0 in the result are 25. Similarly, all the pixels in row 3 in the result are 30. The pixels in rows 1 and 2 of the image belong to region 1 and region 2, hence their assigned value is 27 in the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1
|
||||
<strong>Output:</strong> [[5,6,7],[8,9,10],[11,12,13]]
|
||||
<strong>Explanation:</strong> There does not exist any region in image, hence result[i][j] == image[i][j] for all the pixels.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n, m <= 500</code></li>
|
||||
<li><code>0 <= image[i][j] <= 255</code></li>
|
||||
<li><code>0 <= threshold <= 255</code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given an array <code>nums</code> of length <code>n</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> is called <strong>good</strong> if the <strong>absolute difference</strong> between its first and last element is <strong>exactly</strong> <code>k</code>, in other words, the subarray <code>nums[i..j]</code> is good if <code>|nums[i] - nums[j]| == k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> sum of a <strong>good</strong> subarray of </em><code>nums</code>. <em>If there are no good subarrays</em><em>, return </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], k = 1
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,3,2,4,5], k = 3
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3,-4], k = 2
|
||||
<strong>Output:</strong> -6
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>An ant is on a boundary. It sometimes goes <strong>left</strong> and sometimes <strong>right</strong>.</p>
|
||||
|
||||
<p>You are given an array of <strong>non-zero</strong> integers <code>nums</code>. The ant starts reading <code>nums</code> from the first element of it to its end. At each step, it moves according to the value of the current element:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] < 0</code>, it moves <strong>left</strong> by<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code> units.</li>
|
||||
<li>If <code>nums[i] > 0</code>, it moves <strong>right</strong> by <code>nums[i]</code> units.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of times the ant <strong>returns</strong> to the boundary.</em></p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>There is an infinite space on both sides of the boundary.</li>
|
||||
<li>We check whether the ant is on the boundary only after it has moved <code>|nums[i]|</code> units. In other words, if the ant crosses the boundary during its movement, it does not count.<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,-5]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After the first step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the third step, the ant is on the boundary.
|
||||
So the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,-3,-4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> After the first step, the ant is 3 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the third step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the fourth step, the ant is 2 steps to the left of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
The ant never returned to the boundary, so the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
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
179
leetcode/originData/ant-on-the-boundary.json
Normal file
179
leetcode/originData/ant-on-the-boundary.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/find-the-grid-of-region-average.json
Normal file
175
leetcode/originData/find-the-grid-of-region-average.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
181
leetcode/originData/maximum-good-subarray-sum.json
Normal file
181
leetcode/originData/maximum-good-subarray-sum.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-ii.json
Normal file
180
leetcode/originData/type-of-triangle-ii.json
Normal file
File diff suppressed because one or more lines are too long
50
leetcode/problem/ant-on-the-boundary.html
Normal file
50
leetcode/problem/ant-on-the-boundary.html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>An ant is on a boundary. It sometimes goes <strong>left</strong> and sometimes <strong>right</strong>.</p>
|
||||
|
||||
<p>You are given an array of <strong>non-zero</strong> integers <code>nums</code>. The ant starts reading <code>nums</code> from the first element of it to its end. At each step, it moves according to the value of the current element:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] < 0</code>, it moves <strong>left</strong> by<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code> units.</li>
|
||||
<li>If <code>nums[i] > 0</code>, it moves <strong>right</strong> by <code>nums[i]</code> units.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of times the ant <strong>returns</strong> to the boundary.</em></p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>There is an infinite space on both sides of the boundary.</li>
|
||||
<li>We check whether the ant is on the boundary only after it has moved <code>|nums[i]|</code> units. In other words, if the ant crosses the boundary during its movement, it does not count.<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,-5]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After the first step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the third step, the ant is on the boundary.
|
||||
So the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,-3,-4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> After the first step, the ant is 3 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the third step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
After the fourth step, the ant is 2 steps to the left of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
|
||||
The ant never returned to the boundary, so the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
46
leetcode/problem/find-the-grid-of-region-average.html
Normal file
46
leetcode/problem/find-the-grid-of-region-average.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> grid <code>image</code> which represents a grayscale image, where <code>image[i][j]</code> represents a pixel with intensity in the range<code>[0..255]</code>. You are also given a <strong>non-negative</strong> integer <code>threshold</code>.</p>
|
||||
|
||||
<p>Two pixels <code>image[a][b]</code> and <code>image[c][d]</code> are said to be <strong>adjacent</strong> if <code>|a - c| + |b - d| == 1</code>.</p>
|
||||
|
||||
<p>A <strong>region</strong> is a <code>3 x 3</code> subgrid where the <strong>absolute difference</strong> in intensity between any two <strong>adjacent</strong> pixels is <strong>less than or equal to</strong> <code>threshold</code>.</p>
|
||||
|
||||
<p>All pixels in a <strong>region</strong> belong to that region, note that a pixel <strong>can</strong> belong to <strong>multiple</strong> regions.</p>
|
||||
|
||||
<p>You need to calculate a <strong>0-indexed</strong> <code>m x n</code> grid <code>result</code>, where <code>result[i][j]</code> is the <strong>average</strong> intensity of the region to which <code>image[i][j]</code> belongs, <strong>rounded down</strong> to the nearest integer. If <code>image[i][j]</code> belongs to multiple regions, <code>result[i][j]</code> is the <strong>average </strong>of the<strong> rounded down average </strong>intensities of these regions, <strong>rounded down</strong> to the nearest integer. If <code>image[i][j]</code> does<strong> not</strong> belong to any region, <code>result[i][j]</code> is <strong>equal to</strong> <code>image[i][j]</code>.</p>
|
||||
|
||||
<p>Return <em>the grid</em> <code>result</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example0corrected.png" style="width: 832px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
|
||||
<strong>Output:</strong> [[9,9,9,9],[9,9,9,9],[9,9,9,9]]
|
||||
<strong>Explanation:</strong> There exist two regions in the image, which are shown as the shaded areas in the picture. The average intensity of the first region is 9, while the average intensity of the second region is 9.67 which is rounded down to 9. The average intensity of both of the regions is (9 + 9) / 2 = 9. As all the pixels belong to either region 1, region 2, or both of them, the intensity of every pixel in the result is 9.
|
||||
Please note that the rounded-down values are used when calculating the average of multiple regions, hence the calculation is done using 9 as the average intensity of region 2, not 9.67.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example1corrected.png" style="width: 805px; height: 377px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12
|
||||
<strong>Output:</strong> [[25,25,25],[27,27,27],[27,27,27],[30,30,30]]
|
||||
<strong>Explanation:</strong> There exist two regions in the image, which are shown as the shaded areas in the picture. The average intensity of the first region is 25, while the average intensity of the second region is 30. The average intensity of both of the regions is (25 + 30) / 2 = 27.5 which is rounded down to 27. All the pixels in row 0 of the image belong to region 1, hence all the pixels in row 0 in the result are 25. Similarly, all the pixels in row 3 in the result are 30. The pixels in rows 1 and 2 of the image belong to region 1 and region 2, hence their assigned value is 27 in the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1
|
||||
<strong>Output:</strong> [[5,6,7],[8,9,10],[11,12,13]]
|
||||
<strong>Explanation:</strong> There does not exist any region in image, hence result[i][j] == image[i][j] for all the pixels.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n, m <= 500</code></li>
|
||||
<li><code>0 <= image[i][j] <= 255</code></li>
|
||||
<li><code>0 <= threshold <= 255</code></li>
|
||||
</ul>
|
@ -0,0 +1,56 @@
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>We define the <strong>right</strong> direction as positive x-axis (<strong>increasing x-coordinate</strong>) and the <strong>left</strong> direction as negative x-axis (<strong>decreasing x-coordinate</strong>). Similarly, we define the <strong>up</strong> direction as positive y-axis (<strong>increasing y-coordinate</strong>) and the <strong>down</strong> direction as negative y-axis (<strong>decreasing y-coordinate</strong>)</p>
|
||||
|
||||
<p>You have to place <code>n</code> people, including Alice and Bob, at these points such that there is <strong>exactly one</strong> person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the <strong>upper left corner</strong> and Bob's position as the <strong>lower right corner</strong> of the fence (<strong>Note</strong> that the fence <strong>might not</strong> enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either <strong>inside</strong> the fence or <strong>on</strong> the fence, Alice will be sad.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>pairs of points</strong> where you can place Alice and Bob, such that Alice <strong>does not</strong> become sad on building the fence</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners <code>(1, 1)</code>, <code>(1, 3)</code>, <code>(3, 1)</code>, and <code>(3, 3)</code>, because:</p>
|
||||
|
||||
<ul>
|
||||
<li>With Alice at <code>(3, 3)</code> and Bob at <code>(1, 1)</code>, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.</li>
|
||||
<li>With Alice at <code>(1, 3)</code> and Bob at <code>(1, 1)</code>, Bob's position is not the lower right corner of the fence.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png" style="width: 1321px; height: 363px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[6,2],[4,4],[2,6]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (4, 4) and Bob at (6, 2).
|
||||
- Place Alice at (2, 6) and Bob at (4, 4).
|
||||
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png" style="width: 1123px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[1,3],[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (1, 1) and Bob at (3, 1).
|
||||
- Place Alice at (1, 3) and Bob at (1, 1).
|
||||
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
|
||||
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= points[i][0], points[i][1] <= 50</code></li>
|
||||
<li>All <code>points[i]</code> are distinct.</li>
|
||||
</ul>
|
@ -0,0 +1,56 @@
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>We define the <strong>right</strong> direction as positive x-axis (<strong>increasing x-coordinate</strong>) and the <strong>left</strong> direction as negative x-axis (<strong>decreasing x-coordinate</strong>). Similarly, we define the <strong>up</strong> direction as positive y-axis (<strong>increasing y-coordinate</strong>) and the <strong>down</strong> direction as negative y-axis (<strong>decreasing y-coordinate</strong>)</p>
|
||||
|
||||
<p>You have to place <code>n</code> people, including Alice and Bob, at these points such that there is <strong>exactly one</strong> person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the <strong>upper left corner</strong> and Bob's position as the <strong>lower right corner</strong> of the fence (<strong>Note</strong> that the fence <strong>might not</strong> enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either <strong>inside</strong> the fence or <strong>on</strong> the fence, Alice will be sad.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>pairs of points</strong> where you can place Alice and Bob, such that Alice <strong>does not</strong> become sad on building the fence</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners <code>(1, 1)</code>, <code>(1, 3)</code>, <code>(3, 1)</code>, and <code>(3, 3)</code>, because:</p>
|
||||
|
||||
<ul>
|
||||
<li>With Alice at <code>(3, 3)</code> and Bob at <code>(1, 1)</code>, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.</li>
|
||||
<li>With Alice at <code>(1, 3)</code> and Bob at <code>(1, 1)</code>, Bob's position is not the lower right corner of the fence.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png" style="width: 1321px; height: 363px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[6,2],[4,4],[2,6]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (4, 4) and Bob at (6, 2).
|
||||
- Place Alice at (2, 6) and Bob at (4, 4).
|
||||
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png" style="width: 1123px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[1,3],[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (1, 1) and Bob at (3, 1).
|
||||
- Place Alice at (1, 3) and Bob at (1, 1).
|
||||
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
|
||||
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code></li>
|
||||
<li>All <code>points[i]</code> are distinct.</li>
|
||||
</ul>
|
39
leetcode/problem/maximum-good-subarray-sum.html
Normal file
39
leetcode/problem/maximum-good-subarray-sum.html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given an array <code>nums</code> of length <code>n</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> is called <strong>good</strong> if the <strong>absolute difference</strong> between its first and last element is <strong>exactly</strong> <code>k</code>, in other words, the subarray <code>nums[i..j]</code> is good if <code>|nums[i] - nums[j]| == k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> sum of a <strong>good</strong> subarray of </em><code>nums</code>. <em>If there are no good subarrays</em><em>, return </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], k = 1
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,3,2,4,5], k = 3
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3,-4], k = 2
|
||||
<strong>Output:</strong> -6
|
||||
<strong>Explanation:</strong> The absolute difference between the first and last element<!-- notionvc: 2a6d66c9-0149-4294-b267-8be9fe252de9 --> must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>At every second, you must perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the first <code>k</code> characters of <code>word</code>.</li>
|
||||
<li>Add any <code>k</code> characters to the end of <code>word</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you do not necessarily need to add the same characters that you removed. However, you must perform <strong>both</strong> operations at every second.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time greater than zero required for</em> <code>word</code> <em>to revert to its <strong>initial</strong> state</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
|
||||
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcbabcd", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
|
||||
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
|
||||
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 50 </code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>At every second, you must perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the first <code>k</code> characters of <code>word</code>.</li>
|
||||
<li>Add any <code>k</code> characters to the end of <code>word</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you do not necessarily need to add the same characters that you removed. However, you must perform <strong>both</strong> operations at every second.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time greater than zero required for</em> <code>word</code> <em>to revert to its <strong>initial</strong> state</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
|
||||
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abacaba", k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
|
||||
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcbabcd", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
|
||||
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
|
||||
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
39
leetcode/problem/type-of-triangle-ii.html
Normal file
39
leetcode/problem/type-of-triangle-ii.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…
Reference in New Issue
Block a user