mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
37
leetcode-cn/problem (Chinese)/分割数组 [split-the-array].html
Normal file
37
leetcode-cn/problem (Chinese)/分割数组 [split-the-array].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>给你一个长度为 <strong>偶数 </strong>的整数数组 <code>nums</code> 。你需要将这个数组分割成 <code>nums1</code> 和 <code>nums2</code> 两部分,要求:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1.length == nums2.length == nums.length / 2</code> 。</li>
|
||||
<li><code>nums1</code> 应包含 <strong>互不相同</strong><strong> </strong>的元素。</li>
|
||||
<li><code>nums2</code>也应包含<strong> 互不相同</strong> 的元素。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果能够分割数组就返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,2,2,3,4]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>分割 nums 的可行方案之一是 nums1 = [1,2,3] 和 nums2 = [1,2,4] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,1]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>分割 nums 的唯一可行方案是 nums1 = [1,1] 和 nums2 = [1,1] 。但 nums1 和 nums2 都不是由互不相同的元素构成。因此,返回 false 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length % 2 == 0</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,71 @@
|
||||
<p>给你两个下标从 <strong>1</strong> 开始的整数数组 <code>nums</code> 和 <code>changeIndices</code> ,数组的长度分别为 <code>n</code> 和 <code>m</code> 。</p>
|
||||
|
||||
<p>一开始,<code>nums</code> 中所有下标都是未标记的,你的任务是标记 <code>nums</code> 中 <strong>所有</strong> 下标。</p>
|
||||
|
||||
<p>从第 <code>1</code> 秒到第 <code>m</code> 秒(<b>包括 </b>第 <code>m</code> 秒),对于每一秒 <code>s</code> ,你可以执行以下操作 <strong>之一</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>选择范围 <code>[1, n]</code> 中的一个下标 <code>i</code> ,并且将 <code>nums[i]</code> <strong>减少</strong> <code>1</code> 。</li>
|
||||
<li>如果 <code>nums[changeIndices[s]]</code> <strong>等于</strong> <code>0</code> ,<strong>标记</strong> 下标 <code>changeIndices[s]</code> 。</li>
|
||||
<li>什么也不做。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回范围 <code>[1, m]</code> 中的一个整数,表示最优操作下,标记 <code>nums</code> 中 <strong>所有</strong> 下标的 <strong>最早秒数</strong> ,如果无法标记所有下标,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1]
|
||||
<b>输出:</b>8
|
||||
<b>解释:</b>这个例子中,我们总共有 8 秒。按照以下操作标记所有下标:
|
||||
第 1 秒:选择下标 1 ,将 nums[1] 减少 1 。nums 变为 [1,2,0] 。
|
||||
第 2 秒:选择下标 1 ,将 nums[1] 减少 1 。nums 变为 [0,2,0] 。
|
||||
第 3 秒:选择下标 2 ,将 nums[2] 减少 1 。nums 变为 [0,1,0] 。
|
||||
第 4 秒:选择下标 2 ,将 nums[2] 减少 1 。nums 变为 [0,0,0] 。
|
||||
第 5 秒,标记 changeIndices[5] ,也就是标记下标 3 ,因为 nums[3] 等于 0 。
|
||||
第 6 秒,标记 changeIndices[6] ,也就是标记下标 2 ,因为 nums[2] 等于 0 。
|
||||
第 7 秒,什么也不做。
|
||||
第 8 秒,标记 changeIndices[8] ,也就是标记下标 1 ,因为 nums[1] 等于 0 。
|
||||
现在所有下标已被标记。
|
||||
最早可以在第 8 秒标记所有下标。
|
||||
所以答案是 8 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,3], changeIndices = [1,1,1,2,1,1,1]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>这个例子中,我们总共有 7 秒。按照以下操作标记所有下标:
|
||||
第 1 秒:选择下标 2 ,将 nums[2] 减少 1 。nums 变为 [1,2] 。
|
||||
第 2 秒:选择下标 2 ,将 nums[2] 减少 1 。nums 变为 [1,1] 。
|
||||
第 3 秒:选择下标 2 ,将 nums[2] 减少 1 。nums 变为 [1,0] 。
|
||||
第 4 秒:标记 changeIndices[4] ,也就是标记下标 2 ,因为 nums[2] 等于 0 。
|
||||
第 5 秒:选择下标 1 ,将 nums[1] 减少 1 。nums 变为 [0,0] 。
|
||||
第 6 秒:标记 changeIndices[6] ,也就是标记下标 1 ,因为 nums[1] 等于 0 。
|
||||
现在所有下标已被标记。
|
||||
最早可以在第 6 秒标记所有下标。
|
||||
所以答案是 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1], changeIndices = [2,2,2]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> 这个例子中,无法标记所有下标,因为下标 1 不在 changeIndices 中。
|
||||
所以答案是 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == changeIndices.length <= 2000</code></li>
|
||||
<li><code>1 <= changeIndices[i] <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,71 @@
|
||||
<p>给你两个下标从 <strong>1</strong> 开始的整数数组 <code>nums</code> 和 <code>changeIndices</code> ,数组的长度分别为 <code>n</code> 和 <code>m</code> 。</p>
|
||||
|
||||
<p>一开始,<code>nums</code> 中所有下标都是未标记的,你的任务是标记 <code>nums</code> 中 <strong>所有</strong> 下标。</p>
|
||||
|
||||
<p>从第 <code>1</code> 秒到第 <code>m</code> 秒(<b>包括 </b>第 <code>m</code> 秒),对于每一秒 <code>s</code> ,你可以执行以下操作 <strong>之一</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>选择范围 <code>[1, n]</code> 中的一个下标 <code>i</code> ,并且将 <code>nums[i]</code> <strong>减少</strong> <code>1</code> 。</li>
|
||||
<li>将 <code>nums[changeIndices[s]]</code> 设置成任意的 <strong>非负</strong> 整数。</li>
|
||||
<li>选择范围 <code>[1, n]</code> 中的一个下标 <code>i</code> , 满足 <code>nums[i]</code> <strong>等于</strong> <code>0</code>, 并 <strong>标记</strong> 下标 <code>i</code> 。</li>
|
||||
<li>什么也不做。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回范围 <code>[1, m]</code> 中的一个整数,表示最优操作下,标记 <code>nums</code> 中 <strong>所有</strong> 下标的 <strong>最早秒数</strong> ,如果无法标记所有下标,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,3], changeIndices = [1,3,2,2,2,2,3]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>这个例子中,我们总共有 7 秒。按照以下操作标记所有下标:
|
||||
第 1 秒:将 nums[changeIndices[1]] 变为 0 。nums 变为 [0,2,3] 。
|
||||
第 2 秒:将 nums[changeIndices[2]] 变为 0 。nums 变为 [0,2,0] 。
|
||||
第 3 秒:将 nums[changeIndices[3]] 变为 0 。nums 变为 [0,0,0] 。
|
||||
第 4 秒:标记下标 1 ,因为 nums[1] 等于 0 。
|
||||
第 5 秒:标记下标 2 ,因为 nums[2] 等于 0 。
|
||||
第 6 秒:标记下标 3 ,因为 nums[3] 等于 0 。
|
||||
现在所有下标已被标记。
|
||||
最早可以在第 6 秒标记所有下标。
|
||||
所以答案是 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [0,0,1,2], changeIndices = [1,2,1,2,1,2,1,2]
|
||||
<b>输出:</b>7
|
||||
<b>解释:</b>这个例子中,我们总共有 8 秒。按照以下操作标记所有下标:
|
||||
第 1 秒:标记下标 1 ,因为 nums[1] 等于 0 。
|
||||
第 2 秒:标记下标 2 ,因为 nums[2] 等于 0 。
|
||||
第 3 秒:将 nums[4] 减少 1 。nums 变为 [0,0,1,1] 。
|
||||
第 4 秒:将 nums[4] 减少 1 。nums 变为 [0,0,1,0] 。
|
||||
第 5 秒:将 nums[3] 减少 1 。nums 变为 [0,0,0,0] 。
|
||||
第 6 秒:标记下标 3 ,因为 nums[3] 等于 0 。
|
||||
第 7 秒:标记下标 4 ,因为 nums[4] 等于 0 。
|
||||
现在所有下标已被标记。
|
||||
最早可以在第 7 秒标记所有下标。
|
||||
所以答案是 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3], changeIndices = [1,2,3]
|
||||
<b>输出:</b>-1
|
||||
<strong>解释:</strong>这个例子中,无法标记所有下标,因为我们没有足够的秒数。
|
||||
所以答案是 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 5000</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == changeIndices.length <= 5000</code></li>
|
||||
<li><code>1 <= changeIndices[i] <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>在二维平面上存在 <code>n</code> 个矩形。给你两个下标从 <strong>0</strong> 开始的二维整数数组 <code>bottomLeft</code> 和 <code>topRight</code>,两个数组的大小都是 <code>n x 2</code> ,其中 <code>bottomLeft[i]</code> 和 <code>topRight[i]</code> 分别代表第 <code>i</code> 个矩形的<strong> 左下角 </strong>和 <strong>右上角 </strong>坐标。</p>
|
||||
|
||||
<p>我们定义 <strong>向右 </strong>的方向为 x 轴正半轴(<strong>x 坐标增加</strong>),<strong>向左 </strong>的方向为 x 轴负半轴(<strong>x 坐标减少</strong>)。同样地,定义 <strong>向上 </strong>的方向为 y 轴正半轴(<strong>y 坐标增加</strong>)<strong>,向下 </strong>的方向为 y 轴负半轴(<strong>y 坐标减少</strong>)。</p>
|
||||
|
||||
<p>你可以选择一个区域,该区域由两个矩形的 <strong>交集</strong> 形成。你需要找出能够放入该区域 <strong>内 </strong>的<strong> 最大 </strong>正方形面积,并选择最优解。</p>
|
||||
|
||||
<p>返回能够放入交集区域的正方形的 <strong>最大 </strong>可能面积,如果矩形之间不存在任何交集区域,则返回 <code>0</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/05/example12.png" style="width: 443px; height: 364px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>边长为 1 的正方形可以放入矩形 0 和矩形 1 的交集区域,或矩形 1 和矩形 2 的交集区域。因此最大面积是边长 * 边长,即 1 * 1 = 1。
|
||||
可以证明,边长更大的正方形无法放入任何交集区域。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/rectanglesexample2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 445px; height: 365px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>边长为 1 的正方形可以放入矩形 0 和矩形 1,矩形 1 和矩形 2,或所有三个矩形的交集区域。因此最大面积是边长 * 边长,即 1 * 1 = 1。
|
||||
可以证明,边长更大的正方形无法放入任何交集区域。
|
||||
请注意,区域可以由多于两个矩形的交集构成。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/rectanglesexample3.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 444px; height: 364px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不存在相交的矩形,因此,返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == bottomLeft.length == topRight.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>3</sup></code></li>
|
||||
<li><code>bottomLeft[i].length == topRight[i].length == 2</code></li>
|
||||
<li><code>1 <= bottomLeft[i][0], bottomLeft[i][1] <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= topRight[i][0], topRight[i][1] <= 10<sup>7</sup></code></li>
|
||||
<li><code>bottomLeft[i][0] < topRight[i][0]</code></li>
|
||||
<li><code>bottomLeft[i][1] < topRight[i][1]</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user