1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/使数组和能被 P 整除 [make-sum-divisible-by-p].html
2022-03-29 12:43:11 +08:00

52 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个正整数数组&nbsp;<code>nums</code>,请你移除 <strong>最短</strong>&nbsp;子数组(可以为 <strong></strong>),使得剩余元素的 <strong></strong>&nbsp;能被 <code>p</code>&nbsp;整除。 <strong>不允许</strong>&nbsp;将整个数组都移除。</p>
<p>请你返回你需要移除的最短子数组的长度,如果无法满足题目要求,返回 <code>-1</code>&nbsp;</p>
<p><strong>子数组</strong>&nbsp;定义为原数组中连续的一组元素。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [3,1,4,2], p = 6
<strong>输出:</strong>1
<strong>解释:</strong>nums 中元素和为 10不能被 p 整除。我们可以移除子数组 [4] ,剩余元素的和为 6 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [6,3,5,2], p = 9
<strong>输出:</strong>2
<strong>解释:</strong>我们无法移除任何一个元素使得和被 9 整除,最优方案是移除子数组 [5,2] ,剩余元素为 [6,3],和为 9 。
</pre>
<p><strong>示例&nbsp;3</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3], p = 3
<strong>输出:</strong>0
<strong>解释:</strong>和恰好为 6 ,已经能被 3 整除了。所以我们不需要移除任何元素。
</pre>
<p><strong>示例&nbsp; 4</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3], p = 7
<strong>输出:</strong>-1
<strong>解释:</strong>没有任何方案使得移除子数组后剩余元素的和被 7 整除。
</pre>
<p><strong>示例 5</strong></p>
<pre><strong>输入:</strong>nums = [1000000000,1000000000,1000000000], p = 3
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= p &lt;= 10<sup>9</sup></code></li>
</ul>