Skip to content

Instantly share code, notes, and snippets.

@kumrzz
Created August 11, 2023 09:02
Show Gist options
  • Save kumrzz/5d11e7a02cce68dcf9f55df977fcc052 to your computer and use it in GitHub Desktop.
Save kumrzz/5d11e7a02cce68dcf9f55df977fcc052 to your computer and use it in GitHub Desktop.
Leetcode 914 hasGroupsSizeX
#mostly from https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjqkOKIodKAAxV3T0EAHbWTCxkQFnoECBIQAQ&url=https%3A%2F%2Fleetcode.com%2Fproblems%2Fx-of-a-kind-in-a-deck-of-cards%2Fsolutions%2F2857069%2Fpython-easy-solution-faster-than-99-22%2F&usg=AOvVaw0OEHJEjy6pL2XqRoSZTmAj&opi=89978449
#import collections
class Solution:
def hasGroupsSizeX(self, deck) -> bool:
print(deck)
#count = collections.Counter(deck)
count = dict.fromkeys(deck,0)
for card in deck:
count[card]+=1
print(f"count: {count}")
val = count.values()
print(f"val: {val}")
import math
m = math.gcd(*val)
if m >= 2:
return True
else:
return False
# answer 1
deck = [1,2,3,4,4,3,2,1]
print(Solution.hasGroupsSizeX(1,deck))
# answer 2
deck = [1,1,1,2,2,2,3,3]
print(Solution.hasGroupsSizeX(1,deck))
# answer 3
deck = [0,0,0,1,1,1,2,2,2]
print(Solution.hasGroupsSizeX(1,deck))
# answer 4
deck = [1,1,2,2,2,2]
print(Solution.hasGroupsSizeX(1,deck))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment