mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
9d367dc01f
commit
9f463eb5f6
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.01.23**
|
||||
> 最后更新日期: **2023.02.02**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `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
190
leetcode-cn/originData/count-distinct-numbers-on-board.json
Normal file
190
leetcode-cn/originData/count-distinct-numbers-on-board.json
Normal file
File diff suppressed because one or more lines are too long
196
leetcode-cn/originData/count-increasing-quadruplets.json
Normal file
196
leetcode-cn/originData/count-increasing-quadruplets.json
Normal file
File diff suppressed because one or more lines are too long
190
leetcode-cn/originData/put-marbles-in-bags.json
Normal file
190
leetcode-cn/originData/put-marbles-in-bags.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,42 @@
|
||||
<p>你有 <code>k</code> 个背包。给你一个下标从 <strong>0</strong> 开始的整数数组 <code>weights</code> ,其中 <code>weights[i]</code> 是第 <code>i</code> 个珠子的重量。同时给你整数 <code>k</code> 。</p>
|
||||
|
||||
<p>请你按照如下规则将所有的珠子放进 <code>k</code> 个背包。</p>
|
||||
|
||||
<ul>
|
||||
<li>没有背包是空的。</li>
|
||||
<li>如果第 <code>i</code> 个珠子和第 <code>j</code> 个珠子在同一个背包里,那么下标在 <code>i</code> 到 <code>j</code> 之间的所有珠子都必须在这同一个背包中。</li>
|
||||
<li>如果一个背包有下标从 <code>i</code> 到 <code>j</code> 的所有珠子,那么这个背包的价格是 <code>weights[i] + weights[j]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>一个珠子分配方案的 <strong>分数</strong> 是所有 <code>k</code> 个背包的价格之和。</p>
|
||||
|
||||
<p>请你返回所有分配方案中,<strong>最大分数</strong> 与 <strong>最小分数</strong> 的 <strong>差值</strong> 为多少。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>weights = [1,3,5,1], k = 2
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>
|
||||
分配方案 [1],[3,5,1] 得到最小得分 (1+1) + (3+1) = 6 。
|
||||
分配方案 [1,3],[5,1] 得到最大得分 (1+3) + (5+1) = 10 。
|
||||
所以差值为 10 - 6 = 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>weights = [1, 3], k = 2
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>唯一的分配方案为 [1],[3] 。
|
||||
最大最小得分相等,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= weights.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= weights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>现在有一个正凸多边形,其上共有 <code>n</code> 个顶点。顶点按顺时针方向从 <code>0</code> 到 <code>n - 1</code> 依次编号。每个顶点上 <strong>正好有一只猴子</strong> 。下图中是一个 6 个顶点的凸多边形。</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg" style="width: 300px; height: 293px;"></p>
|
||||
|
||||
<p>每个猴子同时移动到相邻的顶点。顶点 <code>i</code> 的相邻顶点可以是:</p>
|
||||
|
||||
<ul>
|
||||
<li>顺时针方向的顶点 <code>(i + 1) % n</code> ,或</li>
|
||||
<li>逆时针方向的顶点 <code>(i - 1 + n) % n</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果移动后至少有两个猴子位于同一顶点,则会发生 <strong>碰撞</strong> 。</p>
|
||||
|
||||
<p>返回猴子至少发生 <strong>一次碰撞 </strong>的移动方法数。由于答案可能非常大,请返回对 <code>10<sup>9</sup>+7</code> 取余后的结果。</p>
|
||||
|
||||
<p><strong>注意</strong>,每只猴子只能移动一次。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>共计 8 种移动方式。
|
||||
下面列出两种会发生碰撞的方式:
|
||||
- 猴子 1 顺时针移动;猴子 2 逆时针移动;猴子 3 顺时针移动。猴子 1 和猴子 2 碰撞。
|
||||
- 猴子 1 逆时针移动;猴子 2 逆时针移动;猴子 3 顺时针移动。猴子 1 和猴子 3 碰撞。
|
||||
可以证明,有 6 种让猴子碰撞的方法。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 4
|
||||
<strong>输出:</strong>14
|
||||
<strong>解释:</strong>可以证明,有 14 种让猴子碰撞的方法。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,它包含 <code>1</code> 到 <code>n</code> 的所有数字,请你返回上升四元组的数目。</p>
|
||||
|
||||
<p>如果一个四元组 <code>(i, j, k, l)</code> 满足以下条件,我们称它是上升的:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < k < l < n</code> 且</li>
|
||||
<li><code>nums[i] < nums[k] < nums[j] < nums[l]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,3,2,4,5]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
- 当 i = 0 ,j = 1 ,k = 2 且 l = 3 时,有 nums[i] < nums[k] < nums[j] < nums[l] 。
|
||||
- 当 i = 0 ,j = 1 ,k = 2 且 l = 4 时,有 nums[i] < nums[k] < nums[j] < nums[l] 。
|
||||
没有其他的四元组,所以我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,4]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>只存在一个四元组 i = 0 ,j = 1 ,k = 2 ,l = 3 ,但是 nums[j] < nums[k] ,所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= nums.length <= 4000</code></li>
|
||||
<li><code>1 <= nums[i] <= nums.length</code></li>
|
||||
<li><code>nums</code> 中所有数字 <strong>互不相同</strong> ,<code>nums</code> 是一个排列。</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个正整数 <code>n</code> ,开始时,它放在桌面上。在 <code>10<sup>9</sup></code> 天内,每天都要执行下述步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于出现在桌面上的每个数字 <code>x</code> ,找出符合 <code>1 <= i <= n</code> 且满足 <code>x % i == 1</code> 的所有数字 <code>i</code> 。</li>
|
||||
<li>然后,将这些数字放在桌面上。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在 <code>10<sup>9</sup></code> 天之后,出现在桌面上的 <strong>不同</strong> 整数的数目。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>一旦数字放在桌面上,则会一直保留直到结束。</li>
|
||||
<li><code>%</code> 表示取余运算。例如,<code>14 % 3</code> 等于 <code>2</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最开始,5 在桌面上。
|
||||
第二天,2 和 4 也出现在桌面上,因为 5 % 2 == 1 且 5 % 4 == 1 。
|
||||
再过一天 3 也出现在桌面上,因为 4 % 3 == 1 。
|
||||
在十亿天结束时,桌面上的不同数字有 2 、3 、4 、5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
因为 3 % 2 == 1 ,2 也出现在桌面上。
|
||||
在十亿天结束时,桌面上的不同数字只有两个:2 和 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
|
||||
|
||||
<p>Divide the marbles into the <code>k</code> bags according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>No bag is empty.</li>
|
||||
<li>If the <code>i<sup>th</sup></code> marble and <code>j<sup>th</sup></code> marble are in a bag, then all marbles with an index between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> indices should also be in that same bag.</li>
|
||||
<li>If a bag consists of all the marbles with an index from <code>i</code> to <code>j</code> inclusively, then the cost of the bag is <code>weights[i] + weights[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> after distributing the marbles is the sum of the costs of all the <code>k</code> bags.</p>
|
||||
|
||||
<p>Return <em>the <strong>difference</strong> between the <strong>maximum</strong> and <strong>minimum</strong> scores among marble distributions</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1,3,5,1], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
|
||||
The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
|
||||
Thus, we return their difference 10 - 6 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1, 3], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The only distribution possible is [1],[3].
|
||||
Since both the maximal and minimal score are the same, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= weights.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= weights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>There is a regular convex polygon with <code>n</code> vertices. The vertices are labeled from <code>0</code> to <code>n - 1</code> in a clockwise direction, and each vertex has <strong>exactly one monkey</strong>. The following figure shows a convex polygon of <code>6</code> vertices.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg" style="width: 300px; height: 293px;" />
|
||||
<p>Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex <code>i</code> can be:</p>
|
||||
|
||||
<ul>
|
||||
<li>the vertex <code>(i + 1) % n</code> in the clockwise direction, or</li>
|
||||
<li>the vertex <code>(i - 1 + n) % n</code> in the counter-clockwise direction.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>collision</strong> happens if at least two monkeys reside on the same vertex after the movement.</p>
|
||||
|
||||
<p>Return <em>the number of ways the monkeys can move so that at least <strong>one collision</strong></em> <em> happens</em>. Since the answer may be very large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each monkey can only move once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> There are 8 total possible movements.
|
||||
Two ways such that they collide at some point are:
|
||||
- Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.
|
||||
- Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide.
|
||||
It can be shown 6 total movements result in a collision.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> It can be shown that there are 14 ways for the monkeys to collide.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> containing all numbers from <code>1</code> to <code>n</code>, return <em>the number of increasing quadruplets</em>.</p>
|
||||
|
||||
<p>A quadruplet <code>(i, j, k, l)</code> is increasing if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < k < l < n</code>, and</li>
|
||||
<li><code>nums[i] < nums[k] < nums[j] < nums[l]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].
|
||||
- When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l].
|
||||
There are no other quadruplets, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= nums.length <= 4000</code></li>
|
||||
<li><code>1 <= nums[i] <= nums.length</code></li>
|
||||
<li>All the integers of <code>nums</code> are <strong>unique</strong>. <code>nums</code> is a permutation.</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a positive integer <code>n</code>, that is initially placed on a board. Every day, for <code>10<sup>9</sup></code> days, you perform the following procedure:</p>
|
||||
|
||||
<ul>
|
||||
<li>For each number <code>x</code> present on the board, find all numbers <code>1 <= i <= n</code> such that <code>x % i == 1</code>.</li>
|
||||
<li>Then, place those numbers on the board.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> integers present on the board after</em> <code>10<sup>9</sup></code> <em>days have elapsed</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Once a number is placed on the board, it will remain on it until the end.</li>
|
||||
<li><code>%</code> stands for the modulo operation. For example, <code>14 % 3</code> is <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Initially, 5 is present on the board.
|
||||
The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.
|
||||
After that day, 3 will be added to the board because 4 % 3 == 1.
|
||||
At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Since 3 % 2 == 1, 2 will be added to the board.
|
||||
After a billion days, the only two distinct numbers on the board are 2 and 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</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
187
leetcode/originData/count-distinct-numbers-on-board.json
Normal file
187
leetcode/originData/count-distinct-numbers-on-board.json
Normal file
File diff suppressed because one or more lines are too long
193
leetcode/originData/count-increasing-quadruplets.json
Normal file
193
leetcode/originData/count-increasing-quadruplets.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/put-marbles-in-bags.json
Normal file
187
leetcode/originData/put-marbles-in-bags.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,42 @@
|
||||
<p>There is a regular convex polygon with <code>n</code> vertices. The vertices are labeled from <code>0</code> to <code>n - 1</code> in a clockwise direction, and each vertex has <strong>exactly one monkey</strong>. The following figure shows a convex polygon of <code>6</code> vertices.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg" style="width: 300px; height: 293px;" />
|
||||
<p>Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex <code>i</code> can be:</p>
|
||||
|
||||
<ul>
|
||||
<li>the vertex <code>(i + 1) % n</code> in the clockwise direction, or</li>
|
||||
<li>the vertex <code>(i - 1 + n) % n</code> in the counter-clockwise direction.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>collision</strong> happens if at least two monkeys reside on the same vertex after the movement.</p>
|
||||
|
||||
<p>Return <em>the number of ways the monkeys can move so that at least <strong>one collision</strong></em> <em> happens</em>. Since the answer may be very large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each monkey can only move once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> There are 8 total possible movements.
|
||||
Two ways such that they collide at some point are:
|
||||
- Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.
|
||||
- Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide.
|
||||
It can be shown 6 total movements result in a collision.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> It can be shown that there are 14 ways for the monkeys to collide.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
44
leetcode/problem/count-distinct-numbers-on-board.html
Normal file
44
leetcode/problem/count-distinct-numbers-on-board.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are given a positive integer <code>n</code>, that is initially placed on a board. Every day, for <code>10<sup>9</sup></code> days, you perform the following procedure:</p>
|
||||
|
||||
<ul>
|
||||
<li>For each number <code>x</code> present on the board, find all numbers <code>1 <= i <= n</code> such that <code>x % i == 1</code>.</li>
|
||||
<li>Then, place those numbers on the board.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> integers present on the board after</em> <code>10<sup>9</sup></code> <em>days have elapsed</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Once a number is placed on the board, it will remain on it until the end.</li>
|
||||
<li><code>%</code> stands for the modulo operation. For example, <code>14 % 3</code> is <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Initially, 5 is present on the board.
|
||||
The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.
|
||||
After that day, 3 will be added to the board because 4 % 3 == 1.
|
||||
At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Since 3 % 2 == 1, 2 will be added to the board.
|
||||
After a billion days, the only two distinct numbers on the board are 2 and 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
</ul>
|
37
leetcode/problem/count-increasing-quadruplets.html
Normal file
37
leetcode/problem/count-increasing-quadruplets.html
Normal file
@ -0,0 +1,37 @@
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> containing all numbers from <code>1</code> to <code>n</code>, return <em>the number of increasing quadruplets</em>.</p>
|
||||
|
||||
<p>A quadruplet <code>(i, j, k, l)</code> is increasing if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < k < l < n</code>, and</li>
|
||||
<li><code>nums[i] < nums[k] < nums[j] < nums[l]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].
|
||||
- When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l].
|
||||
There are no other quadruplets, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= nums.length <= 4000</code></li>
|
||||
<li><code>1 <= nums[i] <= nums.length</code></li>
|
||||
<li>All the integers of <code>nums</code> are <strong>unique</strong>. <code>nums</code> is a permutation.</li>
|
||||
</ul>
|
42
leetcode/problem/put-marbles-in-bags.html
Normal file
42
leetcode/problem/put-marbles-in-bags.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
|
||||
|
||||
<p>Divide the marbles into the <code>k</code> bags according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>No bag is empty.</li>
|
||||
<li>If the <code>i<sup>th</sup></code> marble and <code>j<sup>th</sup></code> marble are in a bag, then all marbles with an index between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> indices should also be in that same bag.</li>
|
||||
<li>If a bag consists of all the marbles with an index from <code>i</code> to <code>j</code> inclusively, then the cost of the bag is <code>weights[i] + weights[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> after distributing the marbles is the sum of the costs of all the <code>k</code> bags.</p>
|
||||
|
||||
<p>Return <em>the <strong>difference</strong> between the <strong>maximum</strong> and <strong>minimum</strong> scores among marble distributions</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1,3,5,1], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
|
||||
The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
|
||||
Thus, we return their difference 10 - 6 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> weights = [1, 3], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The only distribution possible is [1],[3].
|
||||
Since both the maximal and minimal score are the same, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= weights.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= weights[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user