|
2 | 2 | comments: true |
3 | 3 | difficulty: 中等 |
4 | 4 | edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3874.Valid%20Subarrays%20With%20Exactly%20One%20Peak/README.md |
| 5 | +tags: |
| 6 | + - 数组 |
| 7 | + - 数学 |
5 | 8 | --- |
6 | 9 |
|
7 | 10 | <!-- problem:start --> |
8 | 11 |
|
9 | | -# [3874. Valid Subarrays With Exactly One Peak 🔒](https://leetcode.cn/problems/valid-subarrays-with-exactly-one-peak) |
| 12 | +# [3874. 具有恰好一个峰值的有效子数组 🔒](https://leetcode.cn/problems/valid-subarrays-with-exactly-one-peak) |
10 | 13 |
|
11 | 14 | [English Version](/solution/3800-3899/3874.Valid%20Subarrays%20With%20Exactly%20One%20Peak/README_EN.md) |
12 | 15 |
|
13 | 16 | ## 题目描述 |
14 | 17 |
|
15 | 18 | <!-- description:start --> |
16 | 19 |
|
17 | | -<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p> |
| 20 | +<p>给定一个长度为 <code>n</code> 的整数数组 <code>nums</code> 和一个整数 <code>k</code>。</p> |
18 | 21 |
|
19 | | -<p>An index <code>i</code> is a <strong>peak</strong> if:</p> |
| 22 | +<p>下标 <code>i</code> 是 <strong>峰值</strong> 的条件为:</p> |
20 | 23 |
|
21 | 24 | <ul> |
22 | 25 | <li><code>0 < i < n - 1</code></li> |
23 | | - <li><code>nums[i] > nums[i - 1]</code> and <code>nums[i] > nums[i + 1]</code></li> |
| 26 | + <li><code>nums[i] > nums[i - 1]</code> 且 <code>nums[i] > nums[i + 1]</code></li> |
24 | 27 | </ul> |
25 | 28 |
|
26 | | -<p>A subarray <code>[l, r]</code> is <strong>valid</strong> if:</p> |
| 29 | +<p>一个子数组 <code>[l, r]</code> 是 <strong>有效 </strong>的条件是:</p> |
27 | 30 |
|
28 | 31 | <ul> |
29 | | - <li>It contains <strong>exactly one</strong> peak at index <code>i</code> from <code>nums</code></li> |
30 | | - <li><code>i - l <= k</code> and <code>r - i <= k</code></li> |
| 32 | + <li>它 <strong>恰好有一个</strong> <code>nums</code> 中下标 <code>i</code> 处的峰值</li> |
| 33 | + <li><code>i - l <= k</code> 且 <code>r - i <= k</code></li> |
31 | 34 | </ul> |
32 | 35 |
|
33 | | -<p>Return an integer denoting the number of <strong>valid subarrays</strong> in <code>nums</code>.</p> |
34 | | -A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array. |
| 36 | +<p>返回一个整数,表示 <code>nums</code> 中 <strong>有效子数组</strong> 的数量。</p> |
| 37 | +<strong>子数组</strong> 是数组中的连续 <strong>非空</strong> 元素序列。 |
| 38 | + |
35 | 39 | <p> </p> |
36 | | -<p><strong class="example">Example 1:</strong></p> |
| 40 | + |
| 41 | +<p><strong class="example">示例 1:</strong></p> |
37 | 42 |
|
38 | 43 | <div class="example-block"> |
39 | | -<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], k = 1</span></p> |
| 44 | +<p><span class="example-io"><b>输入:</b>nums = [1,3,2], k = 1</span></p> |
40 | 45 |
|
41 | | -<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 46 | +<p><span class="example-io"><b>输出:</b>4</span></p> |
42 | 47 |
|
43 | | -<p><strong>Explanation:</strong></p> |
| 48 | +<p><strong>解释:</strong></p> |
44 | 49 |
|
45 | 50 | <ul> |
46 | | - <li>Index <code>i = 1</code> is a peak because <code>nums[1] = 3</code> is greater than <code>nums[0] = 1</code> and <code>nums[2] = 2</code>.</li> |
47 | | - <li>Any valid subarray must include index 1, and the distance from the peak to both ends of the subarray must not exceed <code>k = 1</code>.</li> |
48 | | - <li>The valid subarrays are <code>[3]</code>, <code>[1, 3]</code>, <code>[3, 2]</code>, and <code>[1, 3, 2]</code>, so the answer is 4.</li> |
| 51 | + <li>下标 <code>i = 1</code> 是一个峰值,因为 <code>nums[1] = 3</code> 大于 <code>nums[0] = 1</code> 和 <code>nums[2] = 2</code>。</li> |
| 52 | + <li>任何有效的子数组必须包含下标 1,且子数组两端到峰值的距离不得超过 <code>k = 1</code>。</li> |
| 53 | + <li>有效的子数组是 <code>[3]</code>,<code>[1, 3]</code>,<code>[3, 2]</code> 和 <code>[1, 3, 2]</code>,所以答案是 4。</li> |
49 | 54 | </ul> |
50 | 55 | </div> |
51 | 56 |
|
52 | | -<p><strong class="example">Example 2:</strong></p> |
| 57 | +<p><strong class="example">示例 2:</strong></p> |
53 | 58 |
|
54 | 59 | <div class="example-block"> |
55 | | -<p><strong>Input:</strong> <span class="example-io">nums = [7,8,9], k = 2</span></p> |
| 60 | +<p><span class="example-io"><b>输入:</b>nums = [7,8,9], k = 2</span></p> |
56 | 61 |
|
57 | | -<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 62 | +<p><span class="example-io"><b>输出:</b>0</span></p> |
58 | 63 |
|
59 | | -<p><strong>Explanation:</strong></p> |
| 64 | +<p><strong>解释:</strong></p> |
60 | 65 |
|
61 | 66 | <ul> |
62 | | - <li>There is no index <code>i</code> such that <code>nums[i]</code> is greater than both <code>nums[i - 1]</code> and <code>nums[i + 1]</code>.</li> |
63 | | - <li>Therefore, the array contains no peak. Thus, the number of valid subarrays is 0.</li> |
| 67 | + <li>没有下标 <code>i</code> 使得 <code>nums[i]</code> 同时比 <code>nums[i - 1]</code> 和 <code>nums[i + 1]</code> 更大。</li> |
| 68 | + <li>因此,该数组中没有峰值。所以,有效子数组的数量为 0。</li> |
64 | 69 | </ul> |
65 | 70 | </div> |
66 | 71 |
|
67 | | -<p><strong class="example">Example 3:</strong></p> |
| 72 | +<p><strong class="example">示例 3:</strong></p> |
68 | 73 |
|
69 | 74 | <div class="example-block"> |
70 | | -<p><strong>Input:</strong> <span class="example-io">nums = [4,3,5,1], k = 2</span></p> |
| 75 | +<p><span class="example-io"><b>输入:</b>nums = [4,3,5,1], k = 2</span></p> |
71 | 76 |
|
72 | | -<p><strong>Output:</strong> <span class="example-io">6</span></p> |
| 77 | +<p><span class="example-io"><b>输出:</b>6</span></p> |
73 | 78 |
|
74 | | -<p><strong>Explanation:</strong></p> |
| 79 | +<p><strong>解释:</strong></p> |
75 | 80 |
|
76 | 81 | <ul> |
77 | | - <li>Index <code>i = 2</code> is a peak because <code>nums[2] = 5</code> is greater than <code>nums[1] = 3</code> and <code>nums[3] = 1</code>.</li> |
78 | | - <li>Any valid subarray must contain this peak, and the distance from the peak to both ends of the subarray must not exceed <code>k = 2</code>.</li> |
79 | | - <li>The valid subarrays are <code>[5]</code>, <code>[3, 5]</code>, <code>[5, 1]</code>, <code>[3, 5, 1]</code>, <code>[4, 3, 5]</code>, and <code>[4, 3, 5, 1]</code>, so the answer is 6.</li> |
| 82 | + <li>下标 <code>i = 2</code> 是一个峰值,因为 <code>nums[2] = 5</code> 大于 <code>nums[1] = 3</code> 和 <code>nums[3] = 1</code>。</li> |
| 83 | + <li>任何有效的子数组都必须包含这个峰值,并且峰值到子数组两端的距离不得超过 <code>k = 2</code>。</li> |
| 84 | + <li>合法子数组是 <code>[5]</code>,<code>[3, 5]</code>,<code>[5, 1]</code>,<code>[3, 5, 1]</code>,<code>[4, 3, 5]</code> 和 <code>[4, 3, 5, 1]</code>,所以答案是 6。</li> |
80 | 85 | </ul> |
81 | 86 | </div> |
82 | 87 |
|
83 | 88 | <p> </p> |
84 | | -<p><strong>Constraints:</strong></p> |
| 89 | + |
| 90 | +<p><strong>提示:</strong></p> |
85 | 91 |
|
86 | 92 | <ul> |
87 | 93 | <li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li> |
|
0 commit comments