|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3865.Reverse%20K%20Subarrays/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3865. Reverse K Subarrays 🔒](https://leetcode.cn/problems/reverse-k-subarrays) |
| 10 | + |
| 11 | +[English Version](/solution/3800-3899/3865.Reverse%20K%20Subarrays/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p> |
| 18 | + |
| 19 | +<p>You must <strong>partition</strong> the array into <code>k</code> contiguous subarrays of <strong>equal</strong> length and <strong>reverse</strong> each subarray.</p> |
| 20 | + |
| 21 | +<p>It is guaranteed that <code>n</code> is divisible by <code>k</code>.</p> |
| 22 | + |
| 23 | +<p>Return the resulting array after performing the above operation.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,3,5,6], k = 3</span></p> |
| 30 | + |
| 31 | +<p><strong>Output:</strong> <span class="example-io">[2,1,3,4,6,5]</span></p> |
| 32 | + |
| 33 | +<p><strong>Explanation:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>The array is partitioned into <code>k = 3</code> subarrays: <code>[1, 2]</code>, <code>[4, 3]</code>, and <code>[5, 6]</code>.</li> |
| 37 | + <li>After reversing each subarray: <code>[2, 1]</code>, <code>[3, 4]</code>, and <code>[6, 5]</code>.</li> |
| 38 | + <li>Combining them gives the final array <code>[2, 1, 3, 4, 6, 5]</code>.</li> |
| 39 | +</ul> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p><strong class="example">Example 2:</strong></p> |
| 43 | + |
| 44 | +<div class="example-block"> |
| 45 | +<p><strong>Input:</strong> <span class="example-io">nums = [5,4,4,2], k = 1</span></p> |
| 46 | + |
| 47 | +<p><strong>Output:</strong> <span class="example-io">[2,4,4,5]</span></p> |
| 48 | + |
| 49 | +<p><strong>Explanation:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li>The array is partitioned into <code>k = 1</code> subarray: <code>[5, 4, 4, 2]</code>.</li> |
| 53 | + <li>Reversing it produces <code>[2, 4, 4, 5]</code>, which is the final array.</li> |
| 54 | +</ul> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | +<p><strong>Constraints:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li><code>1 <= n == nums.length <= 1000</code></li> |
| 62 | + <li><code>1 <= nums[i] <= 1000</code></li> |
| 63 | + <li><code>1 <= k <= n</code></li> |
| 64 | + <li><code>n</code> is divisible by <code>k</code>.</li> |
| 65 | +</ul> |
| 66 | + |
| 67 | +<!-- description:end --> |
| 68 | + |
| 69 | +## 解法 |
| 70 | + |
| 71 | +<!-- solution:start --> |
| 72 | + |
| 73 | +### 方法一:模拟 |
| 74 | + |
| 75 | +由于需要将数组分成 $k$ 个长度相等的子数组,因此每个子数组的长度为 $m = \frac{n}{k}$。我们可以使用一个循环,按照步长 $m$ 遍历数组,并在每次迭代中将当前子数组进行反转。 |
| 76 | + |
| 77 | +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$,我们只使用了常数级别的额外空间。 |
| 78 | + |
| 79 | +<!-- tabs:start --> |
| 80 | + |
| 81 | +#### Python3 |
| 82 | + |
| 83 | +```python |
| 84 | +class Solution: |
| 85 | + def reverseSubarrays(self, nums: list[int], k: int) -> list[int]: |
| 86 | + n = len(nums) |
| 87 | + m = n // k |
| 88 | + for i in range(0, n, m): |
| 89 | + nums[i : i + m] = nums[i : i + m][::-1] |
| 90 | + return nums |
| 91 | +``` |
| 92 | + |
| 93 | +#### Java |
| 94 | + |
| 95 | +```java |
| 96 | +class Solution { |
| 97 | + public int[] reverseSubarrays(int[] nums, int k) { |
| 98 | + int n = nums.length; |
| 99 | + int m = n / k; |
| 100 | + for (int i = 0; i < n; i += m) { |
| 101 | + int l = i, r = i + m - 1; |
| 102 | + while (l < r) { |
| 103 | + int t = nums[l]; |
| 104 | + nums[l++] = nums[r]; |
| 105 | + nums[r--] = t; |
| 106 | + } |
| 107 | + } |
| 108 | + return nums; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +#### C++ |
| 114 | + |
| 115 | +```cpp |
| 116 | +class Solution { |
| 117 | +public: |
| 118 | + vector<int> reverseSubarrays(vector<int>& nums, int k) { |
| 119 | + int n = nums.size(); |
| 120 | + int m = n / k; |
| 121 | + for (int i = 0; i < n; i += m) { |
| 122 | + int l = i, r = i + m - 1; |
| 123 | + while (l < r) { |
| 124 | + swap(nums[l++], nums[r--]); |
| 125 | + } |
| 126 | + } |
| 127 | + return nums; |
| 128 | + } |
| 129 | +}; |
| 130 | +``` |
| 131 | +
|
| 132 | +#### Go |
| 133 | +
|
| 134 | +```go |
| 135 | +func reverseSubarrays(nums []int, k int) []int { |
| 136 | + n := len(nums) |
| 137 | + m := n / k |
| 138 | + for i := 0; i < n; i += m { |
| 139 | + l, r := i, i+m-1 |
| 140 | + for l < r { |
| 141 | + nums[l], nums[r] = nums[r], nums[l] |
| 142 | + l++ |
| 143 | + r-- |
| 144 | + } |
| 145 | + } |
| 146 | + return nums |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +#### TypeScript |
| 151 | + |
| 152 | +```ts |
| 153 | +function reverseSubarrays(nums: number[], k: number): number[] { |
| 154 | + const n = nums.length; |
| 155 | + const m = Math.floor(n / k); |
| 156 | + for (let i = 0; i < n; i += m) { |
| 157 | + let l = i, |
| 158 | + r = i + m - 1; |
| 159 | + while (l < r) { |
| 160 | + const t = nums[l]; |
| 161 | + nums[l++] = nums[r]; |
| 162 | + nums[r--] = t; |
| 163 | + } |
| 164 | + } |
| 165 | + return nums; |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +#### Rust |
| 170 | + |
| 171 | +```rust |
| 172 | +impl Solution { |
| 173 | + pub fn reverse_subarrays(mut nums: Vec<i32>, k: i32) -> Vec<i32> { |
| 174 | + let n = nums.len(); |
| 175 | + let m = n / k as usize; |
| 176 | + |
| 177 | + for i in (0..n).step_by(m) { |
| 178 | + let mut l = i; |
| 179 | + let mut r = i + m - 1; |
| 180 | + while l < r { |
| 181 | + nums.swap(l, r); |
| 182 | + l += 1; |
| 183 | + r -= 1; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + nums |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +<!-- tabs:end --> |
| 193 | + |
| 194 | +<!-- solution:end --> |
| 195 | + |
| 196 | +<!-- problem:end --> |
0 commit comments