func isNStraightHand(_ hand: [Int], _ W: Int) -> Bool {
if hand.count % W != 0 {
return false
}
var dict = [Int: Int]()
for num in hand {
dict[num, default: 0] += 1
}
var sortedKey = dict.keys.sorted()
while !sortedKey.isEmpty {
let first = sortedKey[0]
for i in stride(from: 0, to: W, by: 1) {
if let value = dict[first+i] {
dict[first+i] = value - 1
if value == 1 {
if let index = sortedKey.firstIndex(of: first+i) {
sortedKey.remove(at: index)
}
}
} else {
return false
}
}
}
return true
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme