blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Best and Worst Lotto Rank (77484)

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. 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.

[Programmers / JAVA] Level 1 Best and Worst Lotto Rank (77484)

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. 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.
RWB0104
@RWBwritten at 2021-12-14 00:10:14
Programmers

시리즈 모아보기

Programmers

1 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Best and Worst Lotto Rank

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.

RankWinning Condition
16 numbers matched
25 numbers matched
34 numbers matched
43 numbers matched
52 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 number3110451619Result
Best-rank number310→104410→6254 numbers matched, rank 3
Worst-rank number310→114410→7252 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.
lottoswin_numsresult
[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 number381920401525Result
Best-rank number0→380→190→200→400→150→256 numbers matched, rank 1
Worst-rank number0→210→220→230→240→260→270 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.

# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08