Skip to content

Commit d088138

Browse files
committed
feat: add solutions to lc problem: No.3576
No.3576.Transform Array to All Equal Elements
1 parent 26b007e commit d088138

File tree

7 files changed

+288
-8
lines changed

7 files changed

+288
-8
lines changed

solution/3500-3599/3576.Transform Array to All Equal Elements/README.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3576.Tr
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:遍历计数
79+
80+
根据题目描述,要使得数组的所有元素相等,要么所有元素为 $\textit{nums}[0]$,要么所有元素为 $-\textit{nums}[0]$。因此,我们设计一个函数 $\textit{check}$,用于判断在最多 $k$ 次操作后,数组能否变成所有元素为 $\textit{target}$ 的形式。
81+
82+
该函数的思路是遍历数组,记录需要进行操作的次数。一个元素要么修改一次,要么不修改。如果当前元素与目标值相等,则不需要修改,继续遍历下一个元素;如果当前元素与目标值不相等,则需要修改,计数器加一,并将符号切换为负数,表示后续元素需要进行相反的操作。
83+
84+
如果遍历结束后,计数器小于等于 $k$ 且最后一个元素的符号与目标值相同,则返回 $\textit{true}$,否则返回 $\textit{false}$。
85+
86+
最终答案是 $\textit{check}(\textit{nums}[0], k)$ 或 $\textit{check}(-\textit{nums}[0], k)$ 的结果。
87+
88+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
7989

8090
<!-- tabs:start -->
8191

8292
#### Python3
8393

8494
```python
85-
95+
class Solution:
96+
def canMakeEqual(self, nums: List[int], k: int) -> bool:
97+
def check(target: int, k: int) -> bool:
98+
cnt, sign = 0, 1
99+
for i in range(len(nums) - 1):
100+
x = nums[i] * sign
101+
if x == target:
102+
sign = 1
103+
else:
104+
sign = -1
105+
cnt += 1
106+
return cnt <= k and nums[-1] * sign == target
107+
108+
return check(nums[0], k) or check(-nums[0], k)
86109
```
87110

88111
#### Java
89112

90113
```java
91-
114+
class Solution {
115+
public boolean canMakeEqual(int[] nums, int k) {
116+
return check(nums, nums[0], k) || check(nums, -nums[0], k);
117+
}
118+
119+
private boolean check(int[] nums, int target, int k) {
120+
int cnt = 0, sign = 1;
121+
for (int i = 0; i < nums.length - 1; ++i) {
122+
int x = nums[i] * sign;
123+
if (x == target) {
124+
sign = 1;
125+
} else {
126+
sign = -1;
127+
++cnt;
128+
}
129+
}
130+
return cnt <= k && nums[nums.length - 1] * sign == target;
131+
}
132+
}
92133
```
93134

94135
#### C++
95136

96137
```cpp
97-
138+
class Solution {
139+
public:
140+
bool canMakeEqual(vector<int>& nums, int k) {
141+
auto check = [&](int target, int k) -> bool {
142+
int n = nums.size();
143+
int cnt = 0, sign = 1;
144+
for (int i = 0; i < n - 1; ++i) {
145+
int x = nums[i] * sign;
146+
if (x == target) {
147+
sign = 1;
148+
} else {
149+
sign = -1;
150+
++cnt;
151+
}
152+
}
153+
return cnt <= k && nums[n - 1] * sign == target;
154+
};
155+
return check(nums[0], k) || check(-nums[0], k);
156+
}
157+
};
98158
```
99159
100160
#### Go
101161
102162
```go
163+
func canMakeEqual(nums []int, k int) bool {
164+
check := func(target, k int) bool {
165+
cnt, sign := 0, 1
166+
for i := 0; i < len(nums)-1; i++ {
167+
x := nums[i] * sign
168+
if x == target {
169+
sign = 1
170+
} else {
171+
sign = -1
172+
cnt++
173+
}
174+
}
175+
return cnt <= k && nums[len(nums)-1]*sign == target
176+
}
177+
return check(nums[0], k) || check(-nums[0], k)
178+
}
179+
```
103180

181+
#### TypeScript
182+
183+
```ts
184+
function canMakeEqual(nums: number[], k: number): boolean {
185+
function check(target: number, k: number): boolean {
186+
let [cnt, sign] = [0, 1];
187+
for (let i = 0; i < nums.length - 1; i++) {
188+
const x = nums[i] * sign;
189+
if (x === target) {
190+
sign = 1;
191+
} else {
192+
sign = -1;
193+
cnt++;
194+
}
195+
}
196+
return cnt <= k && nums[nums.length - 1] * sign === target;
197+
}
198+
199+
return check(nums[0], k) || check(-nums[0], k);
200+
}
104201
```
105202

106203
<!-- tabs:end -->

solution/3500-3599/3576.Transform Array to All Equal Elements/README_EN.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3576.Tr
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Traversal and Counting
77+
78+
According to the problem description, to make all elements in the array equal, all elements must be either $\textit{nums}[0]$ or $-\textit{nums}[0]$. Therefore, we design a function $\textit{check}$ to determine whether the array can be transformed into all elements equal to $\textit{target}$ with at most $k$ operations.
79+
80+
The idea of this function is to traverse the array and count the number of operations needed. Each element is either modified once or not at all. If the current element is equal to the target value, no modification is needed and we continue to the next element. If the current element is not equal to the target value, an operation is needed, increment the counter, and flip the sign, indicating that subsequent elements need the opposite operation.
81+
82+
After the traversal, if the counter is less than or equal to $k$ and the sign of the last element matches the target value, return $\textit{true}$; otherwise, return $\textit{false}$.
83+
84+
The final answer is the result of $\textit{check}(\textit{nums}[0], k)$ or $\textit{check}(-\textit{nums}[0], k)$.
85+
86+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
7787

7888
<!-- tabs:start -->
7989

8090
#### Python3
8191

8292
```python
83-
93+
class Solution:
94+
def canMakeEqual(self, nums: List[int], k: int) -> bool:
95+
def check(target: int, k: int) -> bool:
96+
cnt, sign = 0, 1
97+
for i in range(len(nums) - 1):
98+
x = nums[i] * sign
99+
if x == target:
100+
sign = 1
101+
else:
102+
sign = -1
103+
cnt += 1
104+
return cnt <= k and nums[-1] * sign == target
105+
106+
return check(nums[0], k) or check(-nums[0], k)
84107
```
85108

86109
#### Java
87110

88111
```java
89-
112+
class Solution {
113+
public boolean canMakeEqual(int[] nums, int k) {
114+
return check(nums, nums[0], k) || check(nums, -nums[0], k);
115+
}
116+
117+
private boolean check(int[] nums, int target, int k) {
118+
int cnt = 0, sign = 1;
119+
for (int i = 0; i < nums.length - 1; ++i) {
120+
int x = nums[i] * sign;
121+
if (x == target) {
122+
sign = 1;
123+
} else {
124+
sign = -1;
125+
++cnt;
126+
}
127+
}
128+
return cnt <= k && nums[nums.length - 1] * sign == target;
129+
}
130+
}
90131
```
91132

92133
#### C++
93134

94135
```cpp
95-
136+
class Solution {
137+
public:
138+
bool canMakeEqual(vector<int>& nums, int k) {
139+
auto check = [&](int target, int k) -> bool {
140+
int n = nums.size();
141+
int cnt = 0, sign = 1;
142+
for (int i = 0; i < n - 1; ++i) {
143+
int x = nums[i] * sign;
144+
if (x == target) {
145+
sign = 1;
146+
} else {
147+
sign = -1;
148+
++cnt;
149+
}
150+
}
151+
return cnt <= k && nums[n - 1] * sign == target;
152+
};
153+
return check(nums[0], k) || check(-nums[0], k);
154+
}
155+
};
96156
```
97157
98158
#### Go
99159
100160
```go
161+
func canMakeEqual(nums []int, k int) bool {
162+
check := func(target, k int) bool {
163+
cnt, sign := 0, 1
164+
for i := 0; i < len(nums)-1; i++ {
165+
x := nums[i] * sign
166+
if x == target {
167+
sign = 1
168+
} else {
169+
sign = -1
170+
cnt++
171+
}
172+
}
173+
return cnt <= k && nums[len(nums)-1]*sign == target
174+
}
175+
return check(nums[0], k) || check(-nums[0], k)
176+
}
177+
```
101178

179+
#### TypeScript
180+
181+
```ts
182+
function canMakeEqual(nums: number[], k: number): boolean {
183+
function check(target: number, k: number): boolean {
184+
let [cnt, sign] = [0, 1];
185+
for (let i = 0; i < nums.length - 1; i++) {
186+
const x = nums[i] * sign;
187+
if (x === target) {
188+
sign = 1;
189+
} else {
190+
sign = -1;
191+
cnt++;
192+
}
193+
}
194+
return cnt <= k && nums[nums.length - 1] * sign === target;
195+
}
196+
197+
return check(nums[0], k) || check(-nums[0], k);
198+
}
102199
```
103200

104201
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool canMakeEqual(vector<int>& nums, int k) {
4+
auto check = [&](int target, int k) -> bool {
5+
int n = nums.size();
6+
int cnt = 0, sign = 1;
7+
for (int i = 0; i < n - 1; ++i) {
8+
int x = nums[i] * sign;
9+
if (x == target) {
10+
sign = 1;
11+
} else {
12+
sign = -1;
13+
++cnt;
14+
}
15+
}
16+
return cnt <= k && nums[n - 1] * sign == target;
17+
};
18+
return check(nums[0], k) || check(-nums[0], k);
19+
}
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func canMakeEqual(nums []int, k int) bool {
2+
check := func(target, k int) bool {
3+
cnt, sign := 0, 1
4+
for i := 0; i < len(nums)-1; i++ {
5+
x := nums[i] * sign
6+
if x == target {
7+
sign = 1
8+
} else {
9+
sign = -1
10+
cnt++
11+
}
12+
}
13+
return cnt <= k && nums[len(nums)-1]*sign == target
14+
}
15+
return check(nums[0], k) || check(-nums[0], k)
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean canMakeEqual(int[] nums, int k) {
3+
return check(nums, nums[0], k) || check(nums, -nums[0], k);
4+
}
5+
6+
private boolean check(int[] nums, int target, int k) {
7+
int cnt = 0, sign = 1;
8+
for (int i = 0; i < nums.length - 1; ++i) {
9+
int x = nums[i] * sign;
10+
if (x == target) {
11+
sign = 1;
12+
} else {
13+
sign = -1;
14+
++cnt;
15+
}
16+
}
17+
return cnt <= k && nums[nums.length - 1] * sign == target;
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def canMakeEqual(self, nums: List[int], k: int) -> bool:
3+
def check(target: int, k: int) -> bool:
4+
cnt, sign = 0, 1
5+
for i in range(len(nums) - 1):
6+
x = nums[i] * sign
7+
if x == target:
8+
sign = 1
9+
else:
10+
sign = -1
11+
cnt += 1
12+
return cnt <= k and nums[-1] * sign == target
13+
14+
return check(nums[0], k) or check(-nums[0], k)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function canMakeEqual(nums: number[], k: number): boolean {
2+
function check(target: number, k: number): boolean {
3+
let [cnt, sign] = [0, 1];
4+
for (let i = 0; i < nums.length - 1; i++) {
5+
const x = nums[i] * sign;
6+
if (x === target) {
7+
sign = 1;
8+
} else {
9+
sign = -1;
10+
cnt++;
11+
}
12+
}
13+
return cnt <= k && nums[nums.length - 1] * sign === target;
14+
}
15+
16+
return check(nums[0], k) || check(-nums[0], k);
17+
}

0 commit comments

Comments
 (0)