Skip to content

Commit 912f01d

Browse files
committed
2024-04-29 v. 5.5.6: added "2347. Best Poker Hand"
1 parent 3bd1513 commit 912f01d

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
416416
| 2331. Evaluate Boolean Binary Tree | [Link](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [Link](./lib/easy/2331_evaluate_boolean_binary_tree.rb) |
417417
| 2335. Minimum Amount of Time to Fill Cups | [Link](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [Link](./lib/easy/2335_minimum_amount_of_time_to_fill_cups.rb) |
418418
| 2341. Maximum Number of Pairs in Array | [Link](https://leetcode.com/problems/maximum-number-of-pairs-in-array/) | [Link](./lib/easy/2341_maximum_number_of_pairs_in_array.rb) |
419+
| 2347. Best Poker Hand | [Link](https://leetcode.com/problems/best-poker-hand/) | [Link](./lib/easy/2347_best_poker_hand.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '5.5.5'
8+
s.version = '5.5.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/easy/2347_best_poker_hand.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'set'
4+
5+
# https://leetcode.com/problems/best-poker-hand/
6+
# @param {Integer[]} ranks
7+
# @param {Character[]} suits
8+
# @return {String}
9+
def best_hand(ranks, suits)
10+
s = suits.to_set
11+
12+
return 'Flush' if s.length == 1
13+
14+
r = ::Array.new(14, 0)
15+
max = 0
16+
ranks.each do |rank|
17+
r[rank] += 1
18+
max = [max, r[rank]].max
19+
end
20+
21+
return 'Three of a Kind' if max >= 3
22+
23+
return 'Pair' if max == 2
24+
25+
'High Card'
26+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2347_best_poker_hand'
5+
require 'minitest/autorun'
6+
7+
class BestPokerHandTest < ::Minitest::Test
8+
def test_default
9+
assert_equal('Flush', best_hand([13, 2, 3, 1, 9], %w[a a a a a]))
10+
assert_equal('Three of a Kind', best_hand([4, 4, 2, 4, 4], %w[d a a b c]))
11+
assert_equal('Pair', best_hand([10, 10, 2, 12, 9], %w[a b c a d]))
12+
end
13+
14+
def test_additional
15+
assert_equal('High Card', best_hand([13, 2], %w[a b]))
16+
end
17+
end

0 commit comments

Comments
 (0)