Skip to content

Commit e950e69

Browse files
authored
Added task 2347.
1 parent 334f020 commit e950e69

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
12581258

12591259
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12601260
|-|-|-|-|-|-
1261-
| 1393 |[Capital Gain/Loss](src/main/java/g1301_1400/s1393_capital_gainloss/script.sql)| Medium | LeetCode_Curated_SQL_70, Database | 457 | 79.39
1261+
| 1393 |[Capital Gain/Loss](src/main/java/g1301_1400/s1393_capital_gainloss/script.sql)| Medium | LeetCode_Curated_SQL_70, Database | 428 | 92.01
12621262
| 1407 |[Top Travellers](src/main/java/g1401_1500/s1407_top_travellers/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 682 | 70.16
12631263
| 1158 |[Market Analysis I](src/main/java/g1101_1200/s1158_market_analysis_i/script.sql)| Medium | Database | 1039 | 77.70
12641264

@@ -1848,6 +1848,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2347 |[Best Poker Hand](src/main/java/g2301_2400/s2347_best_poker_hand/Solution.java)| Easy | Array, Hash_Table, Counting | 1 | 76.92
18511852
| 2344 |[Minimum Deletions to Make Array Divisible](src/main/java/g2301_2400/s2344_minimum_deletions_to_make_array_divisible/Solution.java)| Hard | Array, Math, Sorting, Heap_Priority_Queue, Number_Theory | 13 | 88.89
18521853
| 2343 |[Query Kth Smallest Trimmed Number](src/main/java/g2301_2400/s2343_query_kth_smallest_trimmed_number/Solution.java)| Medium | Array, String, Divide_and_Conquer, Sorting, Heap_Priority_Queue, Radix_Sort, Quickselect | 52 | 75.00
18531854
| 2342 |[Max Sum of a Pair With Equal Sum of Digits](src/main/java/g2301_2400/s2342_max_sum_of_a_pair_with_equal_sum_of_digits/Solution.java)| Medium | Array, Hash_Table, Sorting, Heap_Priority_Queue | 99 | 100.00
@@ -2591,7 +2592,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
25912592
| 1396 |[Design Underground System](src/main/java/g1301_1400/s1396_design_underground_system/UndergroundSystem.java)| Medium | String, Hash_Table, Design | 89 | 89.64
25922593
| 1395 |[Count Number of Teams](src/main/java/g1301_1400/s1395_count_number_of_teams/Solution.java)| Medium | Array, Dynamic_Programming, Binary_Indexed_Tree | 18 | 91.75
25932594
| 1394 |[Find Lucky Integer in an Array](src/main/java/g1301_1400/s1394_find_lucky_integer_in_an_array/Solution.java)| Easy | Array, Hash_Table, Counting | 2 | 82.56
2594-
| 1393 |[Capital Gain/Loss](src/main/java/g1301_1400/s1393_capital_gainloss/script.sql)| Medium | LeetCode_Curated_SQL_70, Database, SQL_I_Day_9_Control_of_Flow | 457 | 79.39
2595+
| 1393 |[Capital Gain/Loss](src/main/java/g1301_1400/s1393_capital_gainloss/script.sql)| Medium | LeetCode_Curated_SQL_70, Database, SQL_I_Day_9_Control_of_Flow | 428 | 92.01
25952596
| 1392 |[Longest Happy Prefix](src/main/java/g1301_1400/s1392_longest_happy_prefix/Solution.java)| Hard | String, Hash_Function, String_Matching, Rolling_Hash | 39 | 28.37
25962597
| 1391 |[Check if There is a Valid Path in a Grid](src/main/java/g1301_1400/s1391_check_if_there_is_a_valid_path_in_a_grid/Solution.java)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 14 | 85.37
25972598
| 1390 |[Four Divisors](src/main/java/g1301_1400/s1390_four_divisors/Solution.java)| Medium | Array, Math | 13 | 97.25
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2301_2400.s2347_best_poker_hand;
2+
3+
// #Easy #Array #Hash_Table #Counting
4+
// #2022_07_26_Time_1_ms_(76.92%)_Space_41.6_MB_(30.77%)
5+
6+
import java.util.HashMap;
7+
8+
public class Solution {
9+
public String bestHand(int[] ranks, char[] suits) {
10+
HashMap<Character, Integer> map = new HashMap<>();
11+
for (char suit : suits) {
12+
if (map.containsKey(suit)) {
13+
map.put(suit, map.get(suit) + 1);
14+
if (map.get(suit) == 5) {
15+
return "Flush";
16+
}
17+
} else {
18+
map.put(suit, 1);
19+
}
20+
}
21+
String s = "";
22+
HashMap<Integer, Integer> map2 = new HashMap<>();
23+
for (int rank : ranks) {
24+
if (map2.containsKey(rank)) {
25+
map2.put(rank, map2.get(rank) + 1);
26+
if (map2.get(rank) == 2) {
27+
s = "Pair";
28+
} else if (map2.get(rank) == 3) {
29+
s = "Three of a Kind";
30+
return s;
31+
}
32+
} else {
33+
map2.put(rank, 1);
34+
}
35+
}
36+
return s.isEmpty() ? "High Card" : s;
37+
}
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2347\. Best Poker Hand
2+
3+
Easy
4+
5+
You are given an integer array `ranks` and a character array `suits`. You have `5` cards where the <code>i<sup>th</sup></code> card has a rank of `ranks[i]` and a suit of `suits[i]`.
6+
7+
The following are the types of **poker hands** you can make from best to worst:
8+
9+
1. `"Flush"`: Five cards of the same suit.
10+
2. `"Three of a Kind"`: Three cards of the same rank.
11+
3. `"Pair"`: Two cards of the same rank.
12+
4. `"High Card"`: Any single card.
13+
14+
Return _a string representing the **best** type of **poker hand** you can make with the given cards._
15+
16+
**Note** that the return values are **case-sensitive**.
17+
18+
**Example 1:**
19+
20+
**Input:** ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
21+
22+
**Output:** "Flush"
23+
24+
**Explanation:** The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
25+
26+
**Example 2:**
27+
28+
**Input:** ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
29+
30+
**Output:** "Three of a Kind"
31+
32+
**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".
33+
34+
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
35+
36+
Also note that other cards could be used to make the "Three of a Kind" hand.
37+
38+
**Example 3:**
39+
40+
**Input:** ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
41+
42+
**Output:** "Pair"
43+
44+
**Explanation:** The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
45+
46+
Note that we cannot make a "Flush" or a "Three of a Kind".
47+
48+
**Constraints:**
49+
50+
* `ranks.length == suits.length == 5`
51+
* `1 <= ranks[i] <= 13`
52+
* `'a' <= suits[i] <= 'd'`
53+
* No two cards have the same rank and suit.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2301_2400.s2347_best_poker_hand;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void bestHand() {
11+
assertThat(
12+
new Solution()
13+
.bestHand(new int[] {13, 2, 3, 1, 9}, new char[] {'a', 'a', 'a', 'a', 'a'}),
14+
equalTo("Flush"));
15+
}
16+
17+
@Test
18+
void bestHand2() {
19+
assertThat(
20+
new Solution()
21+
.bestHand(new int[] {4, 4, 2, 4, 4}, new char[] {'d', 'a', 'a', 'b', 'c'}),
22+
equalTo("Three of a Kind"));
23+
}
24+
25+
@Test
26+
void bestHand3() {
27+
assertThat(
28+
new Solution()
29+
.bestHand(
30+
new int[] {10, 10, 2, 12, 9}, new char[] {'a', 'b', 'c', 'a', 'd'}),
31+
equalTo("Pair"));
32+
}
33+
34+
@Test
35+
void bestHand4() {
36+
assertThat(
37+
new Solution()
38+
.bestHand(
39+
new int[] {13, 12, 3, 4, 7}, new char[] {'a', 'd', 'c', 'b', 'c'}),
40+
equalTo("High Card"));
41+
}
42+
}

0 commit comments

Comments
 (0)