为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode914.一副牌中的X | X of a Kind in a Deck of Cards

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9856704.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

In a deck of cards, each card has an integer written on it.

Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:

  • Each group has exactly X cards.
  • All the cards in each group have the same integer.

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]

Example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.

Example 3:

Input: [1]
Output: false
Explanation: No possible partition.

Example 4:

Input: [1,1]
Output: true
Explanation: Possible partition [1,1]

Example 5:

Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

    1. 1 <= deck.length <= 10000
    2. 0 <= deck[i] < 10000

在一副牌中,每张牌都写有一个整数。

返回true当且仅当你可以选择  X >= 2,使得它可以将整副牌分成1个或多个组的卡,其中:

  • 每组都有X卡片。
  • 每组中的所有卡都具有相同的整数。

例1:

输入:[1,2,3,4,4,3,2,1] 
输出:true
 说明:可能的分区[1,1],[2,2],[3,3],[4,4]

例2:

输入:[1,1,1,2,2,2,3,3] 
输出:false
 说明:没有可能的分区。

例3:

输入:[1] 
输出:false
 说明:没有可能的分区。

例4:

输入:[1,1] 
输出:true
 说明:可能的分区[1,1]

例5:

输入:[1,1,2,2,2,2] 
输出:true
 说明:可能的分区[1,1],[2,2],[2,2]

注意:

    1. 1 <= deck.length <= 10000
    2. 0 <= deck[i] < 10000

40ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         guard deck.count > 1 else {
 4             return false
 5         } 
 6         var d = [Int: Int]()
 7         for k in deck {
 8             d[k, default: 0] += 1
 9         }
10         let dks = Array(d.values).filter {$0 >= 2}
11         guard dks.count == d.values.count else {
12             return false
13         }
14         let m = dks.min() ?? 2
15         for i in 2 ... m  {
16             var t = true
17             for k in dks {
18                 if k % i != 0 {
19                     t = false
20                     break
21                 }    
22             }
23             if t {
24                 return true
25             }
26         }
27         return false
28     }
29 }

44ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         var count = [Int: Int]()
 4         
 5         for d in deck {
 6             if let v = count[d] {
 7                 count[d] = v + 1
 8             } else {
 9                 count[d] = 1
10             }
11         }
12         
13         var smallest = Int.max
14         
15         for (_, v) in count {
16             if v < smallest {
17                 smallest = v
18             }
19         }
20         
21         if 2 > smallest {
22             return false
23         }
24         
25         for i in 2...smallest {
26             var flag = true
27             for (_, v) in count {
28                 if v % i != 0 {
29                     flag = false
30                     break
31                 }
32             }
33             if flag {
34                 return true
35             }
36         }
37         
38         return false
39     }
40 }

48ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         var cache: [Int: Int] = [:]
 4         for card in deck {
 5             var count = 0;
 6             if let card_count = cache[card] {
 7                 count = card_count;
 8             }
 9             cache[card] = count + 1;
10         }
11         var cgcd = 0
12         for card in cache {
13             cgcd = gcd(cgcd > 0 ? cgcd : card.value, card.value);
14         }
15         return cgcd > 1
16     }
17     
18     func gcd(_ a: Int, _ b: Int) -> Int {
19         if a == 0 || b == 0 {
20             return 0
21         }
22         if a == b {
23             return a
24         }
25         if a > b {
26             return gcd(a-b, b)
27         }
28         return gcd(a, b-a);
29     }
30 }

56ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         guard deck.count > 1 else {return false}
 4         let counts = deck.reduce(into: [:]) { counts, card in counts[card, default: 0] += 1 }
 5         let values = counts.values.sorted(by: {$0 < $1})
 6         for i in 1..<values.count {
 7         let gcd = calculateGCD(values.first!, values[i])
 8             if gcd == 1 && values.last! > 1 {
 9                 return false
10             }
11         }
12         return true
13     }
14     
15     func calculateGCD(_ a: Int, _ b:Int) -> Int {
16         let r = a > b ? a % b : b % a
17         if r != 0 {
18             return calculateGCD(b, (a > b ? a % b : b % a))
19         } else {
20             return a > b ? b : a
21         }
22     }
23 }

76ms

 1 class Solution {
 2         func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3             if deck.count == 0 {
 4                 return false
 5             }
 6             var countMap = [Int: Int]()
 7             for num in deck {
 8                 if let count = countMap[num] {
 9                     countMap[num] = count + 1
10                     continue
11                 }
12                 countMap[num] = 1
13             }
14             var min = Int.max
15             for (_, count) in countMap {
16                 if count < min {
17                     min = count
18                 }
19             }
20             if min < 2 {
21                 return false
22             }
23             for (_, count) in countMap {
24                 let remainder = count % min
25                 if remainder == 0 {
26                     continue
27                 }
28                 min = findFactor(max: count, min: min)
29                 if min == 1 {
30                     return false
31                 }
32             }
33             return true
34         }
35         func findFactor(max: Int, min: Int) -> Int {
36             let remainder = max % min
37             if remainder == 0 {
38                 return min
39             }
40             return findFactor(max: min, min: remainder)
41         }
42     }

80ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         if deck.count <= 1 { return false }
 4         
 5         var dict = [Int:Int]()
 6         for i in deck {
 7             if let count = dict[i] {
 8                 dict[i] = count+1
 9             } else {
10                 dict[i] = 1
11             }
12         }
13         var counts = Array(dict.values).sorted()
14         
15         if counts.count == 1 { return true }
16         
17         print(counts)
18         
19         for x in 2...counts.max()! {
20             var xIsS = true
21             for val in counts {
22                 if val % x != 0 {
23                     xIsS = false
24                 }
25             }
26             if xIsS { return true }
27         }
28         
29         return false
30     }
31 }

84ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         if deck.isEmpty {
 4             return false
 5         }
 6         let counter = deck.reduce(into: [:]) { counts, elem in counts[elem, default: 0] += 1 }
 7         let values = counter.values
 8 
 9         let maxCount = values.max()!
10 
11         if values.min()! < 2 {
12             return false
13         }
14 
15         for div in 2...maxCount {
16             if (values.reduce(true) { $0 && ($1 % div == 0) }) {
17                 return true
18             }
19         }
20 
21         return false
22     }
23 }

156ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         var map = [Int: Int]()
 4         if deck.isEmpty || deck.count == 1 {
 5             return false
 6         }
 7         for i in 0..<deck.count {
 8             if let val = map[deck[i]] {
 9                 map[deck[i]] = val + 1
10             } else {
11                 map[deck[i]] = 1
12             }
13         }
14         let set = Array(map.values)
15         var result = set[0]
16         for i in 1..<set.count {
17             result = getHCF(result, set[i])
18         }
19         
20         return result > 1
21     }
22     
23     func getHCF(_ a: Int, _ b: Int) -> Int {
24         if a == 0 {
25             return b
26         }
27         if b == 0 {
28             return a
29         }
30         if a == b {
31             return a
32         }
33         if a <= b {
34             return getHCF(a, b % a)
35         } else {
36             return getHCF(a % b, b)
37         }
38         return 1
39     }
40 }

164ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         func gcd(a: Int, b: Int) -> Int {
 4             return (b > 0 ? gcd(a: b, b: a % b) : a)
 5         }
 6         
 7         //
 8         guard deck.count >= 2 else { return false }
 9         
10         var dict: [Int: Int] = [:]
11         
12         for num in deck {
13             dict[num] = (dict[num] ?? 0) + 1
14         }
15         
16         let counts = dict.keys.compactMap { dict[$0] }
17         
18         var res = 0
19         for count in counts {
20             res = gcd(a: count, b: res)
21         }
22         
23         return res > 1
24     }
25 }

172ms

 1 class Solution {
 2   func gcd(_ a: Int, _ b: Int) -> Int {
 3     let r = a % b
 4     if r != 0 {
 5       return gcd(b, r)
 6     } else {
 7       return b
 8     }
 9   }
10   
11   func hasGroupsSizeX(_ deck: [Int]) -> Bool {
12     var cnt: [Int:Int] = [:]
13     deck.forEach {cnt[$0] = cnt[$0, default:0] + 1}
14     
15     let X = cnt.values.reduce(cnt.first!.value, gcd)
16     return X >= 2
17   }
18 }

180ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         var nums:[Int:Int] = [:]
 4         for c in deck {
 5             if nums[c] == nil {
 6                 nums[c] = 1
 7             } else {
 8                 nums[c]! += 1
 9             }
10         }
11         
12         
13         var arr = nums.map { (a) -> Int in
14             return a.value
15         }
16         
17         if arr.count == 1 && arr[0] == 1 {
18             return false
19         }
20         
21         print(arr)
22   
23         var hcf:Int = arr.removeFirst()
24         
25         func gcd(a:Int, b:Int) -> Int {
26             if a == b {
27                 return a
28             } else {
29                 if a > b {
30                     return gcd(a:a-b,b:b)
31                 } else {
32                     return gcd(a:a,b:b-a)
33                 }
34             }
35         }
36         
37         while !arr.isEmpty {
38             hcf = gcd(a:hcf, b:arr.removeFirst())
39             if hcf == 1 {
40                 return false
41             }
42         }
43         return true
44         
45     }
46 }

276ms

 1 class Solution {
 2     func hasGroupsSizeX(_ deck: [Int]) -> Bool {
 3         var f:[Int] = [Int](repeating: 0,count: 10001)
 4         for d in deck
 5         {
 6             f[d] += 1
 7         }
 8         var g:Int = 0
 9         for v in f
10         {
11             g = gcd(g,v)
12         }
13         return g != 1
14     }
15     func gcd(_ a:Int,_ b:Int) -> Int
16     {
17         var a = a
18         var b = b
19         while(b > 0)
20         {
21             var c:Int = a
22             a = b
23             b = c % b
24         }
25         return a
26     }
27 }

 

posted @ 2018-10-26 15:42  为敢技术  阅读(400)  评论(0编辑  收藏  举报