Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算`、`lowbit`
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
<!-- 排序算法、待补充 -->
<!-- 排序算法、待补充 -->

### 2. 数据结构

Expand Down
147 changes: 147 additions & 0 deletions solution/3800-3899/3802.Number of Ways to Paint Sheets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README.md
---

<!-- problem:start -->

# [3802. 给纸张涂色的方式数量 🔒](https://leetcode.cn/problems/number-of-ways-to-paint-sheets)

[English Version](/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给定一个整数&nbsp;<code>n</code>&nbsp;表示纸张的数量。</p>

<p>同时给定一个长度为&nbsp;<code>m</code>&nbsp;的整数数组&nbsp;<code>limit</code>,其中&nbsp;<code>limit[i]</code> 是使用颜色 <code>i</code>&nbsp;能够涂色的最大纸张数。</p>

<p>你必须在下列条件下给 <strong>所有</strong>&nbsp;<code>n</code>&nbsp;张纸涂色:</p>

<ul>
<li><strong>恰好使用两种不同</strong>&nbsp;颜色。</li>
<li>每种颜色必须覆盖 <strong>连续的一段</strong> 纸张。</li>
<li>用颜色 <code>i</code> 涂的纸张数量不能超过 <code>limit[i]</code>。</li>
</ul>

<p>返回一个整数表示给所有纸张染色的 <strong>不同</strong>&nbsp;方式数量。由于答案可能很大,返回答案对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取模的结果。</p>

<p><strong>注意:</strong>如果 <strong>至少</strong> 有一张纸涂上了不同的颜色,就是不同的两种方式。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 4, limit = [3,1,2]</span></p>

<p><span class="example-io"><b>输出:</b>6</span></p>

<p><b>解释:</b></p>
对于每个有序数对&nbsp;<code>(i, j)</code>,其中颜色&nbsp;<code>i</code>&nbsp;被用于给第一段涂色,颜色&nbsp;<code>j</code>&nbsp;被用于给第二段涂色(<code>i != j</code>),<code>x</code> 和&nbsp;<code>4 - x</code>&nbsp;的分割是有效的,当且仅当&nbsp;<code>1 &lt;= x &lt;= limit[i]</code> 且&nbsp;<code>1 &lt;= 4 - x &lt;= limit[j]</code>。

<p>合法的数对以及数量是:</p>

<ul>
<li><code>(0, 1): x = 3</code></li>
<li><code>(0, 2): x = 2, 3</code></li>
<li><code>(1, 0): x = 1</code></li>
<li><code>(2, 0): x = 1, 2</code></li>
</ul>

<p>因此,总共有 6 种有效的方式。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 3, limit = [1,2]</span></p>

<p><span class="example-io"><b>输出:</b>2</span></p>

<p><strong>解释:</strong></p>

<p>对于每个有序数对&nbsp;<code>(i, j)</code>,其中颜色&nbsp;<code>i</code>&nbsp;被用于给第一段涂色,颜色&nbsp;<code>j</code>&nbsp;被用于给第二段涂色(<code>i != j</code>),<code>x</code> 和 <code>3 - x</code>&nbsp;的分割是有效的,当且仅当&nbsp;<code>1 &lt;= x &lt;= limit[i]</code> 且&nbsp;<code>1 &lt;= 3 - x &lt;= limit[j]</code>。</p>

<p>合法的数对和数量是:</p>

<ul>
<li><code>(0, 1): x = 1</code></li>
<li><code>(1, 0): x = 2</code></li>
</ul>

<p>因此,总共有 2 种合法的方式。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 3, limit = [2,2]</span></p>

<p><span class="example-io"><b>输出:</b>4</span></p>

<p><strong>解释:</strong></p>

<p>对于每个有序数对&nbsp;<code>(i, j)</code>,其中颜色&nbsp;<code>i</code>&nbsp;被用于给第一段涂色,颜色&nbsp;<code>j</code>&nbsp;被用于给第二段涂色(<code>i != j</code>),<code>x</code> 和 <code>3 - x</code>&nbsp;的分割是有效的,当且仅当&nbsp;<code>1 &lt;= x &lt;= limit[i]</code> 且&nbsp;<code>1 &lt;= 3 - x &lt;= limit[j]</code>。</p>

<p>合法的数对和数量是:</p>

<ul>
<li><code>(0, 1): x = 1, 2</code></li>
<li><code>(1, 0): x = 1, 2</code></li>
</ul>

<p>因此,总共有 4 种合法的方式。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>2 &lt;= n &lt;= 10<sup>9</sup></code></li>
<li><code>2 &lt;= m == limit.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= limit[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
145 changes: 145 additions & 0 deletions solution/3800-3899/3802.Number of Ways to Paint Sheets/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README_EN.md
---

<!-- problem:start -->

# [3802. Number of Ways to Paint Sheets 🔒](https://leetcode.com/problems/number-of-ways-to-paint-sheets)

[中文文档](/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README.md)

## Description

<!-- description:start -->

<p>You are given an integer <code>n</code> representing the number of sheets.</p>

<p>You are also given an integer array <code>limit</code> of size <code>m</code>, where <code>limit[i]</code> is the <strong>maximum</strong> number of sheets that can be painted using color <code>i</code>.</p>

<p>You must paint <strong>all</strong> <code>n</code> sheets under the following conditions:</p>

<ul>
<li><strong>Exactly two distinct</strong> colors are used.</li>
<li>Each color must cover a <strong>single contiguous</strong> segment of sheets.</li>
<li>The number of sheets painted with color <code>i</code> cannot exceed <code>limit[i]</code>.</li>
</ul>

<p>Return an integer denoting the number of <strong>distinct</strong> ways to paint all sheets. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p><strong>Note:</strong> Two ways differ if <strong>at least</strong> one sheet is painted with a different color.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, limit = [3,1,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong>​​​​​​​</p>
For each ordered pair <code>(i, j)</code>, where color <code>i</code> is used for the first segment and color <code>j</code> for the second segment (<code>i != j</code>), a split of <code>x</code> and <code>4 - x</code> is valid if <code>1 &lt;= x &lt;= limit[i]</code> and <code>1 &lt;= 4 - x &lt;= limit[j]</code>.

<p>Valid pairs and counts are:</p>

<ul>
<li><code>(0, 1): x = 3</code></li>
<li><code>(0, 2): x = 2, 3</code></li>
<li><code>(1, 0): x = 1</code></li>
<li><code>(2, 0): x = 1, 2</code></li>
</ul>

<p>Therefore, there are 6 valid ways in total.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, limit = [1,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>For each ordered pair <code>(i, j)</code>, where color <code>i</code> is used for the first segment and color <code>j</code> for the second segment (<code>i != j</code>), a split of <code>x</code> and <code>3 - x</code> is valid if <code>1 &lt;= x &lt;= limit[i]</code> and <code>1 &lt;= 3 - x &lt;= limit[j]</code>.</p>

<p>Valid pairs and counts are:</p>

<ul>
<li><code>(0, 1): x = 1</code></li>
<li><code>(1, 0): x = 2</code></li>
</ul>

<p>Hence, there are 2 valid ways in total.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, limit = [2,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>

<p>For each ordered pair <code>(i, j)</code>, where color <code>i</code> is used for the first segment and color <code>j</code> for the second segment (<code>i != j</code>), a split of <code>x</code> and <code>3 - x</code> is valid if <code>1 &lt;= x &lt;= limit[i]</code> and <code>1 &lt;= 3 - x &lt;= limit[j]</code>.</p>

<p>Valid pairs and counts are:</p>

<ul>
<li><code>(0, 1): x = 1, 2</code></li>
<li><code>(1, 0): x = 1, 2</code></li>
</ul>

<p>Therefore, there are 4 valid ways in total.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= n &lt;= 10<sup>9</sup></code></li>
<li><code>2 &lt;= m == limit.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= limit[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,7 @@
| 3799 | [单词方块 II](/solution/3700-3799/3799.Word%20Squares%20II/README.md) | | 中等 | 第 483 场周赛 |
| 3800 | [使二进制字符串相等的最小成本](/solution/3800-3899/3800.Minimum%20Cost%20to%20Make%20Two%20Binary%20Strings%20Equal/README.md) | | 中等 | 第 483 场周赛 |
| 3801 | [合并有序列表的最小成本](/solution/3800-3899/3801.Minimum%20Cost%20to%20Merge%20Sorted%20Lists/README.md) | | 困难 | 第 483 场周赛 |
| 3802 | [给纸张涂色的方式数量](/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README.md) | | 困难 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3810,6 +3810,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3799 | [Word Squares II](/solution/3700-3799/3799.Word%20Squares%20II/README_EN.md) | | Medium | Weekly Contest 483 |
| 3800 | [Minimum Cost to Make Two Binary Strings Equal](/solution/3800-3899/3800.Minimum%20Cost%20to%20Make%20Two%20Binary%20Strings%20Equal/README_EN.md) | | Medium | Weekly Contest 483 |
| 3801 | [Minimum Cost to Merge Sorted Lists](/solution/3800-3899/3801.Minimum%20Cost%20to%20Merge%20Sorted%20Lists/README_EN.md) | | Hard | Weekly Contest 483 |
| 3802 | [Number of Ways to Paint Sheets](/solution/3800-3899/3802.Number%20of%20Ways%20to%20Paint%20Sheets/README_EN.md) | | Hard | 🔒 |

## Copyright

Expand Down
2 changes: 1 addition & 1 deletion solution/result.json

Large diffs are not rendered by default.