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
9f463eb5f6
commit
947eefea5d
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.02.02**
|
||||
> 最后更新日期: **2023.02.11**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
185
leetcode-cn/originData/count-vowel-strings-in-ranges.json
Normal file
185
leetcode-cn/originData/count-vowel-strings-in-ranges.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
179
leetcode-cn/originData/house-robber-iv.json
Normal file
179
leetcode-cn/originData/house-robber-iv.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/maximize-win-from-two-segments.json
Normal file
183
leetcode-cn/originData/maximize-win-from-two-segments.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
186
leetcode-cn/originData/rearranging-fruits.json
Normal file
186
leetcode-cn/originData/rearranging-fruits.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/separate-the-digits-in-an-array.json
Normal file
177
leetcode-cn/originData/separate-the-digits-in-an-array.json
Normal file
File diff suppressed because one or more lines are too long
185
leetcode-cn/originData/take-gifts-from-the-richest-pile.json
Normal file
185
leetcode-cn/originData/take-gifts-from-the-richest-pile.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,38 @@
|
||||
<p>在 <strong>X轴</strong> 上有一些奖品。给你一个整数数组 <code>prizePositions</code> ,它按照 <strong>非递减</strong> 顺序排列,其中 <code>prizePositions[i]</code> 是第 <code>i</code> 件奖品的位置。数轴上一个位置可能会有多件奖品。再给你一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>你可以选择两个端点为整数的线段。每个线段的长度都必须是 <code>k</code> 。你可以获得位置在任一线段上的所有奖品(包括线段的两个端点)。注意,两个线段可能会有相交。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说 <code>k = 2</code> ,你可以选择线段 <code>[1, 3]</code> 和 <code>[2, 4]</code> ,你可以获得满足 <code>1 <= prizePositions[i] <= 3</code> 或者 <code>2 <= prizePositions[i] <= 4</code> 的所有奖品 i 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回在选择两个最优线段的前提下,可以获得的 <strong>最多</strong> 奖品数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>prizePositions = [1,1,2,2,3,3,5], k = 2
|
||||
<b>输出:</b>7
|
||||
<b>解释:</b>这个例子中,你可以选择线段 [1, 3] 和 [3, 5] ,获得 7 个奖品。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>prizePositions = [1,2,3,4], k = 0
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>这个例子中,一个选择是选择线段 <code>[3, 3]</code> 和 <code>[4, 4] ,获得 2 个奖品。</code>
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup> </code></li>
|
||||
<li><code>prizePositions</code> 有序非递减。</li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的 <code>m x n</code> <strong>二进制</strong> 矩阵 <code>grid</code> 。你可以从一个格子 <code>(row, col)</code> 移动到格子 <code>(row + 1, col)</code> 或者 <code>(row, col + 1)</code> ,前提是前往的格子值为 <code>1</code> 。如果从 <code>(0, 0)</code> 到 <code>(m - 1, n - 1)</code> 没有任何路径,我们称该矩阵是 <strong>不连通</strong> 的。</p>
|
||||
|
||||
<p>你可以翻转 <strong>最多一个</strong> 格子的值(也可以不翻转)。你 <strong>不能翻转</strong> 格子 <code>(0, 0)</code> 和 <code>(m - 1, n - 1)</code> 。</p>
|
||||
|
||||
<p>如果可以使矩阵不连通,请你返回 <code>true</code> ,否则返回<em> </em><code>false</code><em> </em>。</p>
|
||||
|
||||
<p><strong>注意</strong> ,翻转一个格子的值,可以使它的值从 <code>0</code> 变 <code>1</code> ,或从 <code>1</code> 变 <code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>grid = [[1,1,1],[1,0,0],[1,1,1]]
|
||||
<strong>输出:</strong>true
|
||||
<b>解释:</b>按照上图所示我们翻转蓝色格子里的值,翻转后从 (0, 0) 到 (2, 2) 没有路径。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>无法翻转至多一个格子,使 (0, 0) 到 (2, 2) 没有路径。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个整数数组 <code>banned</code> 和两个整数 <code>n</code> 和 <code>maxSum</code> 。你需要按照以下规则选择一些整数:</p>
|
||||
|
||||
<ul>
|
||||
<li>被选择整数的范围是 <code>[1, n]</code> 。</li>
|
||||
<li>每个整数 <strong>至多</strong> 选择 <strong>一次</strong> 。</li>
|
||||
<li>被选择整数不能在数组 <code>banned</code> 中。</li>
|
||||
<li>被选择整数的和不超过 <code>maxSum</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回按照上述规则 <strong>最多</strong> 可以选择的整数数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>banned = [1,6,5], n = 5, maxSum = 6
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>你可以选择整数 2 和 4 。
|
||||
2 和 4 在范围 [1, 5] 内,且它们都不在 banned 中,它们的和是 6 ,没有超过 maxSum 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>按照上述规则无法选择任何整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>banned = [11], n = 7, maxSum = 50
|
||||
<b>输出:</b>7
|
||||
<b>解释:</b>你可以选择整数 1, 2, 3, 4, 5, 6 和 7 。
|
||||
它们都在范围 [1, 7] 中,且都没出现在 banned 中,它们的和是 28 ,没有超过 maxSum 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= banned.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= banned[i], n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxSum <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个整数数组 <code>gifts</code> ,表示各堆礼物的数量。每一秒,你需要执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择礼物数量最多的那一堆。</li>
|
||||
<li>如果不止一堆都符合礼物数量最多,从中选择任一堆即可。</li>
|
||||
<li>选中的那一堆留下平方根数量的礼物(向下取整),取走其他的礼物。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在 <code>k</code> 秒后剩下的礼物数量<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>gifts = [25,64,9,4,100], k = 4
|
||||
<strong>输出:</strong>29
|
||||
<strong>解释:</strong>
|
||||
按下述方式取走礼物:
|
||||
- 在第一秒,选中最后一堆,剩下 10 个礼物。
|
||||
- 接着第二秒选中第二堆礼物,剩下 8 个礼物。
|
||||
- 然后选中第一堆礼物,剩下 5 个礼物。
|
||||
- 最后,再次选中最后一堆礼物,剩下 3 个礼物。
|
||||
最后剩下的礼物数量分别是 [5,8,9,4,3] ,所以,剩下礼物的总数量是 29 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>gifts = [1,1,1,1], k = 4
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
在本例中,不管选中哪一堆礼物,都必须剩下 1 个礼物。
|
||||
也就是说,你无法获取任一堆中的礼物。
|
||||
所以,剩下礼物的总数量是 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> ,请你返回一个数组<em> </em><code>answer</code> ,你需要将 <code>nums</code> 中每个整数进行数位分割后,按照 <code>nums</code> 中出现的 <strong>相同顺序</strong> 放入答案数组中。</p>
|
||||
|
||||
<p>对一个整数进行数位分割,指的是将整数各个数位按原本出现的顺序排列成数组。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,整数 <code>10921</code> ,分割它的各个数位得到 <code>[1,0,9,2,1]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [13,25,83,77]
|
||||
<b>输出:</b>[1,3,2,5,8,3,7,7]
|
||||
<b>解释:</b>
|
||||
- 分割 13 得到 [1,3] 。
|
||||
- 分割 25 得到 [2,5] 。
|
||||
- 分割 83 得到 [8,3] 。
|
||||
- 分割 77 得到 [7,7] 。
|
||||
answer = [1,3,2,5,8,3,7,7] 。answer 中的数字分割结果按照原数字在数组中的相同顺序排列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [7,1,3,9]
|
||||
<b>输出:</b>[7,1,3,9]
|
||||
<b>解释:</b>nums 中每个整数的分割是它自己。
|
||||
answer = [7,1,3,9] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
44
leetcode-cn/problem (Chinese)/打家劫舍 IV [house-robber-iv].html
Normal file
44
leetcode-cn/problem (Chinese)/打家劫舍 IV [house-robber-iv].html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>沿街有一排连续的房屋。每间房屋内都藏有一定的现金。现在有一位小偷计划从这些房屋中窃取现金。</p>
|
||||
|
||||
<p>由于相邻的房屋装有相互连通的防盗系统,所以小偷 <strong>不会窃取相邻的房屋</strong> 。</p>
|
||||
|
||||
<p>小偷的 <strong>窃取能力</strong> 定义为他在窃取过程中能从单间房屋中窃取的 <strong>最大金额</strong> 。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>nums</code> 表示每间房屋存放的现金金额。形式上,从左起第 <code>i</code> 间房屋中放有 <code>nums[i]</code> 美元。</p>
|
||||
|
||||
<p>另给你一个整数 <code>k</code> ,表示窃贼将会窃取的 <strong>最少</strong> 房屋数。小偷总能窃取至少 <code>k</code> 间房屋。</p>
|
||||
|
||||
<p>返回小偷的 <strong>最小</strong> 窃取能力。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,5,9], k = 2
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
小偷窃取至少 2 间房屋,共有 3 种方式:
|
||||
- 窃取下标 0 和 2 处的房屋,窃取能力为 max(nums[0], nums[2]) = 5 。
|
||||
- 窃取下标 0 和 3 处的房屋,窃取能力为 max(nums[0], nums[3]) = 9 。
|
||||
- 窃取下标 1 和 3 处的房屋,窃取能力为 max(nums[1], nums[3]) = 9 。
|
||||
因此,返回 min(5, 9, 9) = 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,7,9,3,1], k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>共有 7 种窃取方式。窃取能力最小的情况所对应的方式是窃取下标 0 和 4 处的房屋。返回 max(nums[0], nums[4]) = 2 。
|
||||
</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>9</sup></code></li>
|
||||
<li><code>1 <= k <= (nums.length + 1)/2</code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 以及一个二维整数数组 <code>queries</code> 。</p>
|
||||
|
||||
<p>每个查询 <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> 会要求我们统计在 <code>words</code> 中下标在 <code>l<sub>i</sub></code> 到 <code>r<sub>i</sub></code> 范围内(<strong>包含</strong> 这两个值)并且以元音开头和结尾的字符串的数目。</p>
|
||||
|
||||
<p>返回一个整数数组,其中数组的第 <code>i</code> 个元素对应第 <code>i</code> 个查询的答案。</p>
|
||||
|
||||
<p><strong>注意:</strong>元音字母是 <code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code> 和 <code>'u'</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
|
||||
<strong>输出:</strong>[2,3,0]
|
||||
<strong>解释:</strong>以元音开头和结尾的字符串是 "aba"、"ece"、"aa" 和 "e" 。
|
||||
查询 [0,2] 结果为 2(字符串 "aba" 和 "ece")。
|
||||
查询 [1,4] 结果为 3(字符串 "ece"、"aa"、"e")。
|
||||
查询 [1,1] 结果为 0 。
|
||||
返回结果 [2,3,0] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
|
||||
<strong>输出:</strong>[3,2,1]
|
||||
<strong>解释:</strong>每个字符串都满足这一条件,所以返回 [3,2,1] 。</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 <= 40</code></li>
|
||||
<li><code>words[i]</code> 仅由小写英文字母组成</li>
|
||||
<li><code>sum(words[i].length) <= 3 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= queries[j][0] <= queries[j][1] < words.length</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (Chinese)/重排水果 [rearranging-fruits].html
Normal file
40
leetcode-cn/problem (Chinese)/重排水果 [rearranging-fruits].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>你有两个果篮,每个果篮中有 <code>n</code> 个水果。给你两个下标从 <strong>0</strong> 开始的整数数组 <code>basket1</code> 和 <code>basket2</code> ,用以表示两个果篮中每个水果的成本。</p>
|
||||
|
||||
<p>你希望两个果篮相等。为此,可以根据需要多次执行下述操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选中两个下标 <code>i</code> 和 <code>j</code> ,并交换 <code>basket1</code> 中的第 <code>i</code> 个水果和 <code>basket2</code> 中的第 <code>j</code> 个水果。</li>
|
||||
<li>交换的成本是 <code>min(basket1<sub>i</sub>,basket2<sub>j</sub>)</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>根据果篮中水果的成本进行排序,如果排序后结果完全相同,则认为两个果篮相等。</p>
|
||||
|
||||
<p>返回使两个果篮相等的最小交换成本,如果无法使两个果篮相等,则返回<em> </em><code>-1</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>basket1 = [4,2,2,2], basket2 = [1,4,1,2]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>交换 basket1 中下标为 1 的水果和 basket2 中下标为 0 的水果,交换的成本为 1 。此时,basket1 = [4,1,2,2] 且 basket2 = [2,4,1,2] 。重排两个数组,发现二者相等。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>basket1 = [2,3,4,1], basket2 = [3,2,5,1]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>可以证明无法使两个果篮相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>basket1.length == bakste2.length</code></li>
|
||||
<li><code>1 <= basket1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= basket1<sub>i</sub>,basket2<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 <= prizePositions[i] <= 3</code> or <code>2 <= prizePositions[i] <= 4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup> </code></li>
|
||||
<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> <strong>binary</strong> matrix <code>grid</code>. You can move from a cell <code>(row, col)</code> to any of the cells <code>(row + 1, col)</code> or <code>(row, col + 1)</code> that has the value <code>1</code>. The matrix is <strong>disconnected</strong> if there is no path from <code>(0, 0)</code> to <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>You can flip the value of <strong>at most one</strong> (possibly none) cell. You <strong>cannot flip</strong> the cells <code>(0, 0)</code> and <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make the matrix disconnect or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that flipping a cell changes its value from <code>0</code> to <code>1</code> or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,0],[1,1,1]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The chosen integers have to be in the range <code>[1, n]</code>.</li>
|
||||
<li>Each integer can be chosen <strong>at most once</strong>.</li>
|
||||
<li>The chosen integers should not be in the array <code>banned</code>.</li>
|
||||
<li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can choose the integers 2 and 4.
|
||||
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [11], n = 7, maxSum = 50
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
|
||||
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= banned.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= banned[i], n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxSum <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the pile with the maximum number of gifts.</li>
|
||||
<li>If there is more than one pile with the maximum number of gifts, choose any.</li>
|
||||
<li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
|
||||
<strong>Output:</strong> 29
|
||||
<strong>Explanation:</strong>
|
||||
The gifts are taken in the following way:
|
||||
- In the first second, the last pile is chosen and 10 gifts are left behind.
|
||||
- Then the second pile is chosen and 8 gifts are left behind.
|
||||
- After that the first pile is chosen and 5 gifts are left behind.
|
||||
- Finally, the last pile is chosen again and 3 gifts are left behind.
|
||||
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [1,1,1,1], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
|
||||
That is, you can't take any pile with you.
|
||||
So, the total gifts remaining are 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>an array </em><code>answer</code><em> that consists of the digits of each integer in </em><code>nums</code><em> after separating them in <strong>the same order</strong> they appear in </em><code>nums</code>.</p>
|
||||
|
||||
<p>To separate the digits of an integer is to get all the digits it has in the same order.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the integer <code>10921</code>, the separation of its digits is <code>[1,0,9,2,1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [13,25,83,77]
|
||||
<strong>Output:</strong> [1,3,2,5,8,3,7,7]
|
||||
<strong>Explanation:</strong>
|
||||
- The separation of 13 is [1,3].
|
||||
- The separation of 25 is [2,5].
|
||||
- The separation of 83 is [8,3].
|
||||
- The separation of 77 is [7,7].
|
||||
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,1,3,9]
|
||||
<strong>Output:</strong> [7,1,3,9]
|
||||
<strong>Explanation:</strong> The separation of each integer in nums is itself.
|
||||
answer = [7,1,3,9].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he <strong>refuses to steal from adjacent homes</strong>.</p>
|
||||
|
||||
<p>The <strong>capability</strong> of the robber is the maximum amount of money he steals from one house of all the houses he robbed.</p>
|
||||
|
||||
<p>You are given an integer array <code>nums</code> representing how much money is stashed in each house. More formally, the <code>i<sup>th</sup></code> house from the left has <code>nums[i]</code> dollars.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>, representing the <strong>minimum</strong> number of houses the robber will steal from. It is always possible to steal at least <code>k</code> houses.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> capability of the robber out of all the possible ways to steal at least </em><code>k</code><em> houses</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,5,9], k = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
There are three ways to rob at least 2 houses:
|
||||
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
|
||||
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
|
||||
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
|
||||
Therefore, we return min(5, 9, 9) = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,7,9,3,1], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2.
|
||||
</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>9</sup></code></li>
|
||||
<li><code>1 <= k <= (nums.length + 1)/2</code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a 2D array of integers <code>queries</code>.</p>
|
||||
|
||||
<p>Each query <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> asks us to find the number of strings present in the range <code>l<sub>i</sub></code> to <code>r<sub>i</sub></code> (both <strong>inclusive</strong>) of <code>words</code> that start and end with a vowel.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>ans</code><em> of size </em><code>queries.length</code><em>, where </em><code>ans[i]</code><em> is the answer to the </em><code>i</code><sup>th</sup><em> query</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the vowel letters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
|
||||
<strong>Output:</strong> [2,3,0]
|
||||
<strong>Explanation:</strong> The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
|
||||
The answer to the query [0,2] is 2 (strings "aba" and "ece").
|
||||
to query [1,4] is 3 (strings "ece", "aa", "e").
|
||||
to query [1,1] is 0.
|
||||
We return [2,3,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
|
||||
<strong>Output:</strong> [3,2,1]
|
||||
<strong>Explanation:</strong> Every string satisfies the conditions, so we return [3,2,1].</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 <= 40</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
<li><code>sum(words[i].length) <= 3 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>
|
||||
|
||||
<ul>
|
||||
<li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<sup>th</sup> </code>fruit of <code>basket1</code> with the <code>j<sup>th</sup></code> fruit of <code>basket2</code>.</li>
|
||||
<li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>
|
||||
|
||||
<p>Return <em>the minimum cost to make both the baskets equal or </em><code>-1</code><em> if impossible.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>basket1.length == bakste2.length</code></li>
|
||||
<li><code>1 <= basket1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= basket1[i],basket2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
182
leetcode/originData/count-vowel-strings-in-ranges.json
Normal file
182
leetcode/originData/count-vowel-strings-in-ranges.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
176
leetcode/originData/house-robber-iv.json
Normal file
176
leetcode/originData/house-robber-iv.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/maximize-win-from-two-segments.json
Normal file
180
leetcode/originData/maximize-win-from-two-segments.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
183
leetcode/originData/rearranging-fruits.json
Normal file
183
leetcode/originData/rearranging-fruits.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/separate-the-digits-in-an-array.json
Normal file
174
leetcode/originData/separate-the-digits-in-an-array.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/take-gifts-from-the-richest-pile.json
Normal file
182
leetcode/originData/take-gifts-from-the-richest-pile.json
Normal file
File diff suppressed because one or more lines are too long
39
leetcode/problem/count-vowel-strings-in-ranges.html
Normal file
39
leetcode/problem/count-vowel-strings-in-ranges.html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a 2D array of integers <code>queries</code>.</p>
|
||||
|
||||
<p>Each query <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> asks us to find the number of strings present in the range <code>l<sub>i</sub></code> to <code>r<sub>i</sub></code> (both <strong>inclusive</strong>) of <code>words</code> that start and end with a vowel.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>ans</code><em> of size </em><code>queries.length</code><em>, where </em><code>ans[i]</code><em> is the answer to the </em><code>i</code><sup>th</sup><em> query</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the vowel letters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
|
||||
<strong>Output:</strong> [2,3,0]
|
||||
<strong>Explanation:</strong> The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
|
||||
The answer to the query [0,2] is 2 (strings "aba" and "ece").
|
||||
to query [1,4] is 3 (strings "ece", "aa", "e").
|
||||
to query [1,1] is 0.
|
||||
We return [2,3,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
|
||||
<strong>Output:</strong> [3,2,1]
|
||||
<strong>Explanation:</strong> Every string satisfies the conditions, so we return [3,2,1].</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 <= 40</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
<li><code>sum(words[i].length) <= 3 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> <strong>binary</strong> matrix <code>grid</code>. You can move from a cell <code>(row, col)</code> to any of the cells <code>(row + 1, col)</code> or <code>(row, col + 1)</code> that has the value <code>1</code>. The matrix is <strong>disconnected</strong> if there is no path from <code>(0, 0)</code> to <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>You can flip the value of <strong>at most one</strong> (possibly none) cell. You <strong>cannot flip</strong> the cells <code>(0, 0)</code> and <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make the matrix disconnect or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that flipping a cell changes its value from <code>0</code> to <code>1</code> or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,0],[1,1,1]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li>
|
||||
</ul>
|
40
leetcode/problem/house-robber-iv.html
Normal file
40
leetcode/problem/house-robber-iv.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he <strong>refuses to steal from adjacent homes</strong>.</p>
|
||||
|
||||
<p>The <strong>capability</strong> of the robber is the maximum amount of money he steals from one house of all the houses he robbed.</p>
|
||||
|
||||
<p>You are given an integer array <code>nums</code> representing how much money is stashed in each house. More formally, the <code>i<sup>th</sup></code> house from the left has <code>nums[i]</code> dollars.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>, representing the <strong>minimum</strong> number of houses the robber will steal from. It is always possible to steal at least <code>k</code> houses.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> capability of the robber out of all the possible ways to steal at least </em><code>k</code><em> houses</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,5,9], k = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
There are three ways to rob at least 2 houses:
|
||||
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
|
||||
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
|
||||
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
|
||||
Therefore, we return min(5, 9, 9) = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,7,9,3,1], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2.
|
||||
</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>9</sup></code></li>
|
||||
<li><code>1 <= k <= (nums.length + 1)/2</code></li>
|
||||
</ul>
|
45
leetcode/problem/maximize-win-from-two-segments.html
Normal file
45
leetcode/problem/maximize-win-from-two-segments.html
Normal file
@ -0,0 +1,45 @@
|
||||
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 <= prizePositions[i] <= 3</code> or <code>2 <= prizePositions[i] <= 4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup> </code></li>
|
||||
<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The chosen integers have to be in the range <code>[1, n]</code>.</li>
|
||||
<li>Each integer can be chosen <strong>at most once</strong>.</li>
|
||||
<li>The chosen integers should not be in the array <code>banned</code>.</li>
|
||||
<li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can choose the integers 2 and 4.
|
||||
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [11], n = 7, maxSum = 50
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
|
||||
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= banned.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= banned[i], n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxSum <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
36
leetcode/problem/rearranging-fruits.html
Normal file
36
leetcode/problem/rearranging-fruits.html
Normal file
@ -0,0 +1,36 @@
|
||||
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>
|
||||
|
||||
<ul>
|
||||
<li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<sup>th</sup> </code>fruit of <code>basket1</code> with the <code>j<sup>th</sup></code> fruit of <code>basket2</code>.</li>
|
||||
<li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>
|
||||
|
||||
<p>Return <em>the minimum cost to make both the baskets equal or </em><code>-1</code><em> if impossible.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>basket1.length == bakste2.length</code></li>
|
||||
<li><code>1 <= basket1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= basket1[i],basket2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
38
leetcode/problem/separate-the-digits-in-an-array.html
Normal file
38
leetcode/problem/separate-the-digits-in-an-array.html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>an array </em><code>answer</code><em> that consists of the digits of each integer in </em><code>nums</code><em> after separating them in <strong>the same order</strong> they appear in </em><code>nums</code>.</p>
|
||||
|
||||
<p>To separate the digits of an integer is to get all the digits it has in the same order.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the integer <code>10921</code>, the separation of its digits is <code>[1,0,9,2,1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [13,25,83,77]
|
||||
<strong>Output:</strong> [1,3,2,5,8,3,7,7]
|
||||
<strong>Explanation:</strong>
|
||||
- The separation of 13 is [1,3].
|
||||
- The separation of 25 is [2,5].
|
||||
- The separation of 83 is [8,3].
|
||||
- The separation of 77 is [7,7].
|
||||
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,1,3,9]
|
||||
<strong>Output:</strong> [7,1,3,9]
|
||||
<strong>Explanation:</strong> The separation of each integer in nums is itself.
|
||||
answer = [7,1,3,9].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
44
leetcode/problem/take-gifts-from-the-richest-pile.html
Normal file
44
leetcode/problem/take-gifts-from-the-richest-pile.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the pile with the maximum number of gifts.</li>
|
||||
<li>If there is more than one pile with the maximum number of gifts, choose any.</li>
|
||||
<li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
|
||||
<strong>Output:</strong> 29
|
||||
<strong>Explanation:</strong>
|
||||
The gifts are taken in the following way:
|
||||
- In the first second, the last pile is chosen and 10 gifts are left behind.
|
||||
- Then the second pile is chosen and 8 gifts are left behind.
|
||||
- After that the first pile is chosen and 5 gifts are left behind.
|
||||
- Finally, the last pile is chosen again and 3 gifts are left behind.
|
||||
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [1,1,1,1], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
|
||||
That is, you can't take any pile with you.
|
||||
So, the total gifts remaining are 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user