-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2347-BestPokerHand.go
More file actions
88 lines (77 loc) · 3.37 KB
/
2347-BestPokerHand.go
File metadata and controls
88 lines (77 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
// 2347. Best Poker Hand
// You are given an integer array ranks and a character array suits.
// You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i].
// The following are the types of poker hands you can make from best to worst:
// 1. "Flush": Five cards of the same suit.
// 2. "Three of a Kind": Three cards of the same rank.
// 3. "Pair": Two cards of the same rank.
// 4. "High Card": Any single card.
// Return a string representing the best type of poker hand you can make with the given cards.
// Note that the return values are case-sensitive.
// Example 1:
// Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
// Output: "Flush"
// Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
// Example 2:
// Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
// Output: "Three of a Kind"
// Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
// Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
// Also note that other cards could be used to make the "Three of a Kind" hand.
// Example 3:
// Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
// Output: "Pair"
// Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
// Note that we cannot make a "Flush" or a "Three of a Kind".
// Constraints:
// ranks.length == suits.length == 5
// 1 <= ranks[i] <= 13
// 'a' <= suits[i] <= 'd'
// No two cards have the same rank and suit.
import "fmt"
func bestHand(ranks []int, suits []byte) string {
best, count := 0, 0
freq := [14]int{}
for i := 0; i < len(ranks); i++ {
if suits[i] == suits[0] {
count++
}
freq[ranks[i]]++
if freq[ranks[i]] > best {
best = freq[ranks[i]]
}
}
if count == 5 {
best = count
}
switch best {
case 5:
return "Flush"
case 3,4:
return "Three of a Kind"
case 2:
return "Pair"
}
return "High Card"
}
func main() {
// Example 1:
// Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
// Output: "Flush"
// Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
fmt.Println(bestHand([]int{13,2,3,1,9}, []byte{'a','a','a','a','a'})) // "Flush"
// Example 2:
// Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
// Output: "Three of a Kind"
// Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
// Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
// Also note that other cards could be used to make the "Three of a Kind" hand.
fmt.Println(bestHand([]int{4,4,2,4,4}, []byte{'d','a','a','b','c'})) // "Three of a Kind"
// Example 3:
// Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
// Output: "Pair"
// Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
// Note that we cannot make a "Flush" or a "Three of a Kind".
fmt.Println(bestHand([]int{10,10,2,12,9}, []byte{'a','b','c','a','d'})) // "Pair"
}