Skip to content

Commit f29a530

Browse files
authored
feat: add solutions for lc No.3634 (#5013)
1 parent e512298 commit f29a530

15 files changed

Lines changed: 309 additions & 32 deletions

File tree

solution/3300-3399/3379.Transformed Array/README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### 方法一
85+
### 方法一:模拟
86+
87+
我们创建一个结果数组 $\textit{ans}$,对于每个下标,根据 $nums[i]$ 的正负向右或向左移动 $|nums[i]|$ 步,计算出落脚的下标,并将该下标对应的值赋给 $\textit{ans}[i]$。
88+
89+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
90+
8691

8792
<!-- tabs:start -->
8893

@@ -91,11 +96,8 @@ tags:
9196
```python
9297
class Solution:
9398
def constructTransformedArray(self, nums: List[int]) -> List[int]:
94-
ans = []
9599
n = len(nums)
96-
for i, x in enumerate(nums):
97-
ans.append(nums[(i + x + n) % n] if x else 0)
98-
return ans
100+
return [nums[(i + x % n + n) % n] for i, x in enumerate(nums)]
99101
```
100102

101103
#### Java
@@ -106,7 +108,7 @@ class Solution {
106108
int n = nums.length;
107109
int[] ans = new int[n];
108110
for (int i = 0; i < n; ++i) {
109-
ans[i] = nums[i] != 0 ? nums[(i + nums[i] % n + n) % n] : 0;
111+
ans[i] = nums[(i + nums[i] % n + n) % n];
110112
}
111113
return ans;
112114
}
@@ -122,7 +124,7 @@ public:
122124
int n = nums.size();
123125
vector<int> ans(n);
124126
for (int i = 0; i < n; ++i) {
125-
ans[i] = nums[i] ? nums[(i + nums[i] % n + n) % n] : 0;
127+
ans[i] = nums[(i + nums[i] % n + n) % n];
126128
}
127129
return ans;
128130
}
@@ -136,9 +138,7 @@ func constructTransformedArray(nums []int) []int {
136138
n := len(nums)
137139
ans := make([]int, n)
138140
for i, x := range nums {
139-
if x != 0 {
140-
ans[i] = nums[(i+x%n+n)%n]
141-
}
141+
ans[i] = nums[(i+x%n+n)%n]
142142
}
143143
return ans
144144
}
@@ -151,12 +151,27 @@ function constructTransformedArray(nums: number[]): number[] {
151151
const n = nums.length;
152152
const ans: number[] = [];
153153
for (let i = 0; i < n; ++i) {
154-
ans.push(nums[i] ? nums[(i + (nums[i] % n) + n) % n] : 0);
154+
ans.push(nums[(i + (nums[i] % n) + n) % n]);
155155
}
156156
return ans;
157157
}
158158
```
159159

160+
#### Rust
161+
162+
```rust
163+
impl Solution {
164+
pub fn construct_transformed_array(nums: Vec<i32>) -> Vec<i32> {
165+
let n = nums.len() as i32;
166+
let mut ans = vec![0; nums.len()];
167+
for (i, &x) in nums.iter().enumerate() {
168+
ans[i] = nums[(((i as i32 + x % n + n) % n) as usize)];
169+
}
170+
ans
171+
}
172+
}
173+
```
174+
160175
<!-- tabs:end -->
161176

162177
<!-- solution:end -->

solution/3300-3399/3379.Transformed Array/README_EN.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ For each index <code>i</code> (where <code>0 &lt;= i &lt; nums.length</code>), p
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Simulation
84+
85+
We create a result array $\textit{ans}$. For each index, we move right or left $|nums[i]|$ steps based on whether $nums[i]$ is positive or negative, calculate the landing index, and assign the value at that index to $\textit{ans}[i]$.
86+
87+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
8488

8589
<!-- tabs:start -->
8690

@@ -89,11 +93,8 @@ For each index <code>i</code> (where <code>0 &lt;= i &lt; nums.length</code>), p
8993
```python
9094
class Solution:
9195
def constructTransformedArray(self, nums: List[int]) -> List[int]:
92-
ans = []
9396
n = len(nums)
94-
for i, x in enumerate(nums):
95-
ans.append(nums[(i + x + n) % n] if x else 0)
96-
return ans
97+
return [nums[(i + x % n + n) % n] for i, x in enumerate(nums)]
9798
```
9899

99100
#### Java
@@ -104,7 +105,7 @@ class Solution {
104105
int n = nums.length;
105106
int[] ans = new int[n];
106107
for (int i = 0; i < n; ++i) {
107-
ans[i] = nums[i] != 0 ? nums[(i + nums[i] % n + n) % n] : 0;
108+
ans[i] = nums[(i + nums[i] % n + n) % n];
108109
}
109110
return ans;
110111
}
@@ -120,7 +121,7 @@ public:
120121
int n = nums.size();
121122
vector<int> ans(n);
122123
for (int i = 0; i < n; ++i) {
123-
ans[i] = nums[i] ? nums[(i + nums[i] % n + n) % n] : 0;
124+
ans[i] = nums[(i + nums[i] % n + n) % n];
124125
}
125126
return ans;
126127
}
@@ -134,9 +135,7 @@ func constructTransformedArray(nums []int) []int {
134135
n := len(nums)
135136
ans := make([]int, n)
136137
for i, x := range nums {
137-
if x != 0 {
138-
ans[i] = nums[(i+x%n+n)%n]
139-
}
138+
ans[i] = nums[(i+x%n+n)%n]
140139
}
141140
return ans
142141
}
@@ -149,12 +148,27 @@ function constructTransformedArray(nums: number[]): number[] {
149148
const n = nums.length;
150149
const ans: number[] = [];
151150
for (let i = 0; i < n; ++i) {
152-
ans.push(nums[i] ? nums[(i + (nums[i] % n) + n) % n] : 0);
151+
ans.push(nums[(i + (nums[i] % n) + n) % n]);
153152
}
154153
return ans;
155154
}
156155
```
157156

157+
#### Rust
158+
159+
```rust
160+
impl Solution {
161+
pub fn construct_transformed_array(nums: Vec<i32>) -> Vec<i32> {
162+
let n = nums.len() as i32;
163+
let mut ans = vec![0; nums.len()];
164+
for (i, &x) in nums.iter().enumerate() {
165+
ans[i] = nums[(((i as i32 + x % n + n) % n) as usize)];
166+
}
167+
ans
168+
}
169+
}
170+
```
171+
158172
<!-- tabs:end -->
159173

160174
<!-- solution:end -->

solution/3300-3399/3379.Transformed Array/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Solution {
44
int n = nums.size();
55
vector<int> ans(n);
66
for (int i = 0; i < n; ++i) {
7-
ans[i] = nums[i] ? nums[(i + nums[i] % n + n) % n] : 0;
7+
ans[i] = nums[(i + nums[i] % n + n) % n];
88
}
99
return ans;
1010
}

solution/3300-3399/3379.Transformed Array/Solution.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ func constructTransformedArray(nums []int) []int {
22
n := len(nums)
33
ans := make([]int, n)
44
for i, x := range nums {
5-
if x != 0 {
6-
ans[i] = nums[(i+x%n+n)%n]
7-
}
5+
ans[i] = nums[(i+x%n+n)%n]
86
}
97
return ans
108
}

solution/3300-3399/3379.Transformed Array/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public int[] constructTransformedArray(int[] nums) {
33
int n = nums.length;
44
int[] ans = new int[n];
55
for (int i = 0; i < n; ++i) {
6-
ans[i] = nums[i] != 0 ? nums[(i + nums[i] % n + n) % n] : 0;
6+
ans[i] = nums[(i + nums[i] % n + n) % n];
77
}
88
return ans;
99
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Solution:
22
def constructTransformedArray(self, nums: List[int]) -> List[int]:
3-
ans = []
43
n = len(nums)
5-
for i, x in enumerate(nums):
6-
ans.append(nums[(i + x + n) % n] if x else 0)
7-
return ans
4+
return [nums[(i + x % n + n) % n] for i, x in enumerate(nums)]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn construct_transformed_array(nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len() as i32;
4+
let mut ans = vec![0; nums.len()];
5+
for (i, &x) in nums.iter().enumerate() {
6+
ans[i] = nums[(((i as i32 + x % n + n) % n) as usize)];
7+
}
8+
ans
9+
}
10+
}

solution/3300-3399/3379.Transformed Array/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function constructTransformedArray(nums: number[]): number[] {
22
const n = nums.length;
33
const ans: number[] = [];
44
for (let i = 0; i < n; ++i) {
5-
ans.push(nums[i] ? nums[(i + (nums[i] % n) + n) % n] : 0);
5+
ans.push(nums[(i + (nums[i] % n) + n) % n]);
66
}
77
return ans;
88
}

solution/3600-3699/3634.Minimum Removals to Balance Array/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,92 @@ function minRemoval(nums: number[], k: number): number {
199199

200200
<!-- solution:end -->
201201

202+
<!-- solution:start -->
203+
204+
### 方法二:排序 + 双指针
205+
206+
我们首先对数组进行排序,然后我们使用双指针来维护一个滑动窗口,左指针 $l$ 从左到右枚举每个元素 $\textit{nums}[l]$ 作为平衡数组的最小值,右指针 $r$ 不断向右移动,直到 $\textit{nums}[r]$ 大于 $\textit{nums}[l] \times k$,此时平衡数组的长度为 $r - l$,需要移除的元素数量为 $n - (r - l)$,我们记录下最小的移除数量作为答案。
207+
208+
时间复杂度 $O(n \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
209+
210+
<!-- tabs:start -->
211+
212+
#### Python3
213+
214+
```python
215+
class Solution:
216+
def minRemoval(self, nums: List[int], k: int) -> int:
217+
nums.sort()
218+
ans = n = len(nums)
219+
r = 0
220+
for l in range(n):
221+
while r < n and nums[r] <= nums[l] * k:
222+
r += 1
223+
ans = min(ans, n - (r - l))
224+
return ans
225+
```
226+
227+
#### Java
228+
229+
```java
230+
class Solution {
231+
public int minRemoval(int[] nums, int k) {
232+
Arrays.sort(nums);
233+
int n = nums.length;
234+
int ans = n;
235+
int r = 0;
236+
for (int l = 0; l < n; l++) {
237+
while (r < n && nums[r] <= (long) nums[l] * k) {
238+
r++;
239+
}
240+
ans = Math.min(ans, n - (r - l));
241+
}
242+
return ans;
243+
}
244+
}
245+
```
246+
247+
#### C++
248+
249+
```cpp
250+
class Solution {
251+
public:
252+
int minRemoval(vector<int>& nums, int k) {
253+
ranges::sort(nums);
254+
int n = nums.size();
255+
int ans = n;
256+
int r = 0;
257+
for (int l = 0; l < n; l++) {
258+
while (r < n && nums[r] <= (long long) nums[l] * k) {
259+
r++;
260+
}
261+
ans = min(ans, n - (r - l));
262+
}
263+
return ans;
264+
}
265+
};
266+
```
267+
268+
#### TypeScript
269+
270+
```ts
271+
function minRemoval(nums: number[], k: number): number {
272+
nums.sort((a, b) => a - b);
273+
const n = nums.length;
274+
let ans = n;
275+
let r = 0;
276+
for (let l = 0; l < n; l++) {
277+
while (r < n && nums[r] <= nums[l] * k) {
278+
r++;
279+
}
280+
ans = Math.min(ans, n - (r - l));
281+
}
282+
return ans;
283+
}
284+
```
285+
286+
<!-- tabs:end -->
287+
288+
<!-- solution:end -->
289+
202290
<!-- problem:end -->

0 commit comments

Comments
 (0)