4747
4848<!-- 这里可写通用的实现逻辑 -->
4949
50- 不需要修改数组,只统计不符合规则的元素数量即可。
51- 52- ``` txt
53- COUNT(A){
54- n = A.length
55- i = 0
56- r = 0
57- while i < n - 1
58- if nums[i] == nums[i + 1]
59- r += 1
60- i += 1
61- else
62- i += 2
63- return r
64- ```
50+ ** 方法一:贪心**
51+ 52+ 根据题目描述,我们知道,一个美丽数组有偶数个元素,且如果我们把这个数组中每相邻两个元素划分为一组,那么每一组中的两个元素都不相等。这意味着,组内的元素不能重复,但组与组之间的元素可以重复。
53+ 54+ 因此,我们考虑从左到右遍历数组,只要遇到相邻两个元素相等,我们就将其中的一个元素删除,即删除数加一;否则,我们可以保留这两个元素。
55+ 56+ 最后,我们判断删除后的数组长度是否为偶数,如果不是,则说明我们需要再删除一个元素,使得最终的数组长度为偶数。
6557
66- 完成统计后,计算删除元素之后的数组长度是否为奇数,若为奇数,还需要进行一次删除(返回值 + 1) 。
58+ 时间复杂度 $O(n),ドル其中 $n$ 是数组的长度。我们只需要遍历数组一次。空间复杂度 $O(1)$ 。
6759
6860<!-- tabs:start -->
6961
@@ -82,8 +74,22 @@ class Solution:
8274 i += 1
8375 else :
8476 i += 2
85- if (n - ans) % 2 :
86- ans += 1
77+ ans += (n - ans) % 2
78+ return ans
79+ ```
80+ 81+ ``` python
82+ class Solution :
83+ def minDeletion (self , nums : List[int ]) -> int :
84+ n = len (nums)
85+ ans = i = 0
86+ while i < n:
87+ j = i + 1
88+ while j < n and nums[j] == nums[i]:
89+ j += 1
90+ ans += 1
91+ i = j + 1
92+ ans += (n - ans) % 2
8793 return ans
8894```
8995
@@ -103,56 +109,27 @@ class Solution {
103109 ++ i;
104110 }
105111 }
106- if ((n - ans) % 2 == 1 ) {
107- ++ ans;
108- }
112+ ans += (n - ans) % 2 ;
109113 return ans;
110114 }
111115}
112116```
113117
114- ### ** TypeScript**
115- 116- ``` ts
117- function minDeletion(nums : number []): number {
118- const n = nums .length ;
119- let res = 0 ;
120- let i = 0 ;
121- while (i < n - 1 ) {
122- if (nums [i ] === nums [i + 1 ]) {
123- i ++ ;
124- res ++ ;
125- } else {
126- i += 2 ;
127- }
128- }
129- if ((n - res ) % 2 === 1 ) {
130- res ++ ;
131- }
132- return res ;
133- }
134- ```
135- 136- ### ** Rust**
137- 138- ``` rust
139- impl Solution {
140- pub fn min_deletion (nums : Vec <i32 >) -> i32 {
141- let n = nums . len ();
142- let mut res = 0 ;
143- let mut i = 0 ;
144- while i < n - 1 {
145- if nums [i ] == nums [i + 1 ] {
146- res += 1 ;
147- i += 1 ;
148- } else {
149- i += 2 ;
118+ ``` java
119+ class Solution {
120+ public int minDeletion (int [] nums ) {
121+ int n = nums. length;
122+ int ans = 0 ;
123+ for (int i = 0 ; i < n;) {
124+ int j = i + 1 ;
125+ while (j < n && nums[j] == nums[i]) {
126+ ++ j;
127+ ++ ans;
150128 }
129+ i = j + 1 ;
151130 }
152- if (n - res ) % 2 == 1 {
153- res += 1 ;
154- }
155- res as i32
131+ ans += (n - ans) % 2 ;
132+ return ans;
156133 }
157134}
158135```
@@ -172,7 +149,27 @@ public:
172149 ++i;
173150 }
174151 }
175- if ((n - ans) % 2) ++ans;
152+ ans += (n - ans) % 2;
153+ return ans;
154+ }
155+ };
156+ ```
157+
158+ ```cpp
159+ class Solution {
160+ public:
161+ int minDeletion(vector<int>& nums) {
162+ int n = nums.size();
163+ int ans = 0;
164+ for (int i = 0; i < n;) {
165+ int j = i + 1;
166+ while (j < n && nums[j] == nums[i]) {
167+ ++j;
168+ ++ans;
169+ }
170+ i = j + 1;
171+ }
172+ ans += (n - ans) % 2;
176173 return ans;
177174 }
178175};
@@ -181,20 +178,108 @@ public:
181178### ** Go**
182179
183180``` go
184- func minDeletion(nums []int) int {
181+ func minDeletion (nums []int ) ( ans int ) {
185182 n := len (nums)
186- ans := 0
187183 for i := 0 ; i < n-1 ; i++ {
188184 if nums[i] == nums[i+1 ] {
189185 ans++
190186 } else {
191187 i++
192188 }
193189 }
194- if (n-ans)%2 == 1 {
195- ans++
190+ ans += (n - ans) % 2
191+ return
192+ }
193+ ```
194+ 195+ ``` go
196+ func minDeletion (nums []int ) (ans int ) {
197+ n := len (nums)
198+ for i := 0 ; i < n; {
199+ j := i + 1
200+ for ; j < n && nums[j] == nums[i]; j++ {
201+ ans++
202+ }
203+ i = j + 1
196204 }
197- return ans
205+ ans += (n - ans) % 2
206+ return
207+ }
208+ ```
209+ 210+ ### ** TypeScript**
211+ 212+ ``` ts
213+ function minDeletion(nums : number []): number {
214+ const n = nums .length ;
215+ let ans = 0 ;
216+ for (let i = 0 ; i < n - 1 ; ++ i ) {
217+ if (nums [i ] === nums [i + 1 ]) {
218+ ++ ans ;
219+ } else {
220+ ++ i ;
221+ }
222+ }
223+ ans += (n - ans ) % 2 ;
224+ return ans ;
225+ }
226+ ```
227+ 228+ ``` ts
229+ function minDeletion(nums : number []): number {
230+ const n = nums .length ;
231+ let ans = 0 ;
232+ for (let i = 0 ; i < n ; ) {
233+ let j = i + 1 ;
234+ for (; j < n && nums [j ] === nums [i ]; ++ j ) {
235+ ++ ans ;
236+ }
237+ i = j + 1 ;
238+ }
239+ ans += (n - ans ) % 2 ;
240+ return ans ;
241+ }
242+ ```
243+ 244+ ### ** Rust**
245+ 246+ ``` rust
247+ impl Solution {
248+ pub fn min_deletion (nums : Vec <i32 >) -> i32 {
249+ let n = nums . len ();
250+ let mut ans = 0 ;
251+ let mut i = 0 ;
252+ while i < n - 1 {
253+ if nums [i ] == nums [i + 1 ] {
254+ ans += 1 ;
255+ i += 1 ;
256+ } else {
257+ i += 2 ;
258+ }
259+ }
260+ ans += (n - ans ) % 2 ;
261+ ans as i32
262+ }
263+ }
264+ ```
265+ 266+ ``` rust
267+ impl Solution {
268+ pub fn min_deletion (nums : Vec <i32 >) -> i32 {
269+ let n = nums . len ();
270+ let mut ans = 0 ;
271+ let mut i = 0 ;
272+ while i < n {
273+ let mut j = i + 1 ;
274+ while j < n && nums [j ] == nums [i ] {
275+ ans += 1 ;
276+ j += 1 ;
277+ }
278+ i = j + 1 ;
279+ }
280+ ans += (n - ans ) % 2 ;
281+ ans as i32
282+ }
198283}
199284```
200285
0 commit comments