@@ -196,8 +196,6 @@ class Solution(object):
196
196
排序 + 双指针,不需要额外空间。
197
197
198
198
199
-
200
-
201
199
## 454. 四数相加 II
202
200
203
201
[ 原题链接] ( https://leetcode-cn.com/problems/4sum-ii/ )
@@ -233,6 +231,156 @@ class Solution(object):
233
231
return count
234
232
```
235
233
234
+ ## 575. 分糖果
235
+
236
+ [ 原题链接] ( https://leetcode-cn.com/problems/distribute-candies/ )
237
+
238
+ ### 总体思路
239
+
240
+ 题目的意思是把所有的糖果分成两份,其中一份要尽可能拥有最多种类的糖果。
241
+
242
+ 假设糖果一共有 ` x ` 颗,糖果种类有 ` y ` 种,那么分成两份后每份有糖果 ` x / 2 ` :
243
+
244
+ - 如果 ` y >= x / 2 ` ,那么可以直接拿出 ` x / 2 ` 种糖果
245
+ - 如果 ` y < x / 2 ` ,那么最多也只能分配到 ` y ` 种糖果了
246
+
247
+ 所以这道题只要求出糖果的种类 ` y ` 即可。而求出糖果种类的方法也有好几种。
248
+
249
+ ### 解法一:字典
250
+
251
+ 将糖果种类的数字值作为 key 存储在字典中,糖果一旦存在则进行标记,并将糖果种类加一。
252
+
253
+ <!-- tabs:start -->
254
+
255
+ #### ** Python**
256
+
257
+ ``` python
258
+ class Solution :
259
+ def distributeCandies (self , candies : List[int ]) -> int :
260
+ length = len (candies)
261
+ each = length // 2
262
+ type_dict = dict ()
263
+ type_cnt = 0
264
+
265
+ for c in candies:
266
+ if type_dict.get(c, 0 ) == 0 :
267
+ type_dict[c] = 1
268
+ type_cnt += 1
269
+
270
+ if type_cnt >= each:
271
+ return each
272
+ else :
273
+ return type_cnt
274
+ ```
275
+
276
+ #### ** Go**
277
+
278
+ ``` go
279
+ func distributeCandies (candies []int ) int {
280
+ var candiesMap map [int ]bool
281
+ candiesMap = make (map [int ]bool )
282
+ var candiesCount = 0
283
+ for i := 0 ; i < len (candies); i++ {
284
+ candie := candies[i]
285
+ _ , ok := candiesMap[candie]
286
+ // candie 不存在,开始计数
287
+ if !ok {
288
+ candiesMap[candie] = true
289
+ candiesCount += 1
290
+ }
291
+ }
292
+ var each = len (candies) / 2
293
+ if candiesCount >= each {
294
+ return each
295
+ } else {
296
+ return candiesCount
297
+ }
298
+ }
299
+ ```
300
+
301
+ <!-- tabs:end -->
302
+
303
+ - 时间复杂度 O(n)
304
+ - 空间复杂度 O(n)
305
+
306
+ ### 解法二:集合
307
+
308
+ 利用集合无重复元素的特性,将数据存入集合,从而求出糖果的种类。
309
+
310
+ ``` python
311
+ class Solution :
312
+ def distributeCandies (self , candies : List[int ]) -> int :
313
+ each = len (candies) // 2
314
+ candies_set = set (candies)
315
+ return each if each <= len (candies_set) else len (candies_set)
316
+ ```
317
+
318
+ - 时间复杂度 O(n)
319
+ - 空间复杂度 O(n)
320
+
321
+ ### 解法三:先排序
322
+
323
+ 先对数组进行排序,然后将相邻的数字进行对比,如果相邻数字不同,则说明出现了新种类的糖果。
324
+
325
+ <!-- tabs:start -->
326
+
327
+ #### ** Python**
328
+
329
+ ``` python
330
+ class Solution :
331
+ def distributeCandies (self , candies : List[int ]) -> int :
332
+ length = len (candies)
333
+ each = length // 2
334
+ candies.sort()
335
+ count = 1
336
+ for i in range (1 , length):
337
+ if candies[i] != candies[i - 1 ]:
338
+ count += 1
339
+ return each if each <= count else count
340
+ ```
341
+
342
+ #### ** Go**
343
+
344
+ ``` go
345
+ import (
346
+ " sort"
347
+ )
348
+
349
+ func distributeCandies (candies []int ) int {
350
+ var length = len (candies)
351
+ var each = length / 2
352
+ var count = 1
353
+ sort.Ints (candies)
354
+ for i := 1 ; i < length; i ++ {
355
+ if candies[i] != candies[i - 1 ] {
356
+ count++
357
+ }
358
+ }
359
+
360
+ if count >= each {
361
+ return each
362
+ } else {
363
+ return count
364
+ }
365
+ }
366
+ ```
367
+
368
+ <!-- tabs:end -->
369
+
370
+ - 时间复杂度:O(nlogn)
371
+ - 空间复杂度:O(1)
372
+
373
+ ### 解法四:Python Counter
374
+
375
+ ``` python
376
+ from collections import Counter
377
+
378
+ class Solution :
379
+ def distributeCandies (self , candies : List[int ]) -> int :
380
+ each = len (candies) // 2
381
+ candies_count = len (Counter(candies))
382
+ return each if candies_count >= each else candies_count
383
+ ```
236
384
237
385
## 594. 最长和谐子序列
238
386
0 commit comments