[Programmers / JAVA] Level 1 Best and Worst Lotto Rank (77484)
[Programmers / JAVA] Level 1 Best and Worst Lotto Rank (77484)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
Lotto 6/45 (hereafter 'Lotto') is a well-known lottery in which you pick 6 numbers out of 1 to 45 and try to match them. Below is how Lotto rankings are determined.
| Rank | Winning Condition |
|---|---|
| 1 | 6 numbers matched |
| 2 | 5 numbers matched |
| 3 | 4 numbers matched |
| 4 | 3 numbers matched |
| 5 | 2 numbers matched |
| 6 (no win) | Otherwise |
Minwoo, who bought a Lotto ticket, was eagerly awaiting the day the winning numbers would be announced. However, Minwoo's younger sibling scribbled on the ticket, making some of the numbers unreadable. After the winning numbers were announced, Minwoo wanted to figure out the best and worst possible ranks his ticket could have won.
Let's say the unreadable numbers are marked as 0, and the 6 Lotto numbers Minwoo bought are 44, 1, 0, 0, 31, 25. If the 6 winning numbers are 31, 10, 45, 1, 6, 19, here's one example of the best and worst possible ranks.
| Winning number | 31 | 10 | 45 | 1 | 6 | 19 | Result |
|---|---|---|---|---|---|---|---|
| Best-rank number | 31 | 0→10 | 44 | 1 | 0→6 | 25 | 4 numbers matched, rank 3 |
| Worst-rank number | 31 | 0→11 | 44 | 1 | 0→7 | 25 | 2 numbers matched, rank 5 |
- Regardless of order, if the purchased Lotto ticket contains a number matching a winning number, it counts as a match.
- If the two unreadable numbers are assumed to be 10 and 6 respectively, the ticket could win rank 3.
- There are other ways to make rank 3 as well. However, it's impossible to make rank 2 or higher.
- If the two unreadable numbers are assumed to be 11 and 7 respectively, the ticket could win rank 5.
- There are other ways to make rank 5 as well. However, it's impossible to make rank 6 (no win).
You're given an array lottos containing the Lotto numbers Minwoo bought, and an array win_nums containing the winning numbers, as parameters. Complete the solution function so that it returns an array containing the best possible rank and the worst possible rank, in that order.
- lottos is an integer array of length 6.
- Every element of lottos is an integer between 0 and 45 inclusive.
- 0 means an unreadable number.
- Aside from 0, no other number appears more than once in lottos.
- The elements of lottos may not be sorted.
- win_nums is an integer array of length 6.
- Every element of win_nums is an integer between 1 and 45 inclusive.
- No number appears more than once in win_nums.
- The elements of win_nums may not be sorted.
| lottos | win_nums | result |
|---|---|---|
| [44, 1, 0, 0, 31, 25] | [31, 10, 45, 1, 6, 19] | [3, 5] |
| [0, 0, 0, 0, 0, 0] | [38, 19, 20, 40, 15, 25] | [1, 6] |
| [45, 4, 35, 20, 3, 9] | [20, 9, 3, 45, 4, 35] | [1, 1] |
Input/Output Example #1
Same as the problem example.
Input/Output Example #2
If the unreadable numbers were as shown below, the ticket could win rank 1 or rank 6.
| Winning number | 38 | 19 | 20 | 40 | 15 | 25 | Result |
|---|---|---|---|---|---|---|---|
| Best-rank number | 0→38 | 0→19 | 0→20 | 0→40 | 0→15 | 0→25 | 6 numbers matched, rank 1 |
| Worst-rank number | 0→21 | 0→22 | 0→23 | 0→24 | 0→26 | 0→27 | 0 numbers matched, rank 6 |
Input/Output Example #3
Since the numbers Minwoo bought match all the winning numbers, both the best and worst rank are rank 1.
Some of the numbers on the Lotto ticket are erased, and in this case we need to find the highest and lowest rank it could achieve.
- Highest rank
- Number of confirmed matches among the clearly displayed numbers + number of erased numbers
- Lowest rank
- Number of confirmed matches among the clearly displayed numbers
In other words, the key is distinguishing, among the clearly displayed numbers, which ones are winning numbers and which are erased. From there, we can compute the answer using these counts.
Since erased numbers are marked as 0, if a number is 0, mark it as an erased number; if it's a valid number, compare it against win_nums to check whether it matches.
JAVA
for (int lotto : lottos) { // If the number is readable if (lotto > 0) { for (int win_num : win_nums) { // If it's a matching number if (lotto == win_num) { // Count the matched number break; } } } // If not else { // Count the erased number } }
Just build the logic as shown above.
JAVA
/** * Best and Worst Lotto Rank class * * @author RWB * @since 2021.12.06 Mon 23:35:13 */ class Solution { /** * Method that returns the answer * * @param lottos: [int[]] Lotto numbers * @param win_nums: [int[]] Winning numbers * * @return [int[]] The answer */ public int[] solution(int[] lottos, int[] win_nums) { int answer = 0; int zeros = 0; for (int lotto : lottos) { // If the number is readable if (lotto > 0) { for (int win_num : win_nums) { // If it's a matching number if (lotto == win_num) { answer++; break; } } } // If not else { zeros++; } } return new int[] { prize(answer + zeros), prize(answer) }; } /** * Method that returns the rank * * @param num: [int] Number of matches * * @return [int] Rank */ private int prize(int num) { return switch (num) { case 6 -> 1; case 5 -> 2; case 4 -> 3; case 3 -> 4; case 2 -> 5; default -> 6; }; } }
That's the code. It counts matched numbers in answer, and erased numbers in zeros.
Then it computes the highest rank as answer + zeros and the lowest rank as answer, and returns them as an array.
The rank is obtained using the prize method.

![[NextJS] Blog Overhaul Journey - 5. Improving Code Block Design Using marked](https://user-images.githubusercontent.com/50317129/134931033-89954c3d-5e00-4b3b-85aa-54a1dfa29e46.png)