NOTE
Poker Straight
Sort five cards, treat jokers as 0, and use the gaps between non-zero cards to determine whether they can form a straight.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Problem Description
LL is in a great mood today because he bought a deck of playing cards and found that it contains 2 big jokers and 2 small jokers (a deck originally has 54 cards ^_^)… He randomly draws 5 cards to test his luck and see whether he can get a straight. If he does, he plans to buy a lottery ticket. “Ace of hearts, 3 of spades, small joker, big joker, 5 of diamonds” — “Oh My God!” It is not a straight… LL is unhappy, so he decides that the big and small jokers can represent any number, with A treated as 1, J as 11, Q as 12, and K as 13. The five cards above can then become “1,2,3,4,5” (the jokers are treated as 2 and 4), “So Lucky!”. Now simulate this process and determine LL’s luck. Output true if the cards can form a straight; otherwise output false. For convenience, treat the jokers as 0.
2. Approach
3. Implementation
import "sort"
// Time complexity: O(NlogN)
// Space complexity: O(1)
func IsContinuous(numbers []int) bool {
if len(numbers) == 0 {
return false
}
sort.Ints(numbers)
notZero := 0
for notZero = 0; notZero < len(numbers); notZero++ {
if numbers[notZero] != 0 {
break
}
}
diff := 0
for i := notZero; i < len(numbers)-1; i++ {
res := numbers[i+1] - numbers[i]
if res == 0 {
return false
}
diff += res
}
return diff < len(numbers)
}
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub