๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋กœ๋˜์˜ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„ #77484

by ํ‚ค์œค 2024. 1. 17.

#77484

๐ŸŽ„ Question ?

Description

Lotto 6/45(Hereinafter 'Lotto') is a popular lottery game where six numbers are drawn from a pool of 45 numbers. The lottery prize tiers are as follows1:

Prize TiersRequirement
1 All six numbers match
2 Five numbers match
3 Four numbers match
4 Three numbers match
5 Two numbers match
6 (no prize) All other cases

You bought a lotto ticket and have been waiting for the draw. However, your little brother drew scribbles all over your ticket so that you can't make out some of its numbers. After the draw, you wanted to figure out the highest prize you could have won as well as the lowest prize you could have got.
Suppose that your ticket contained the following set of numbers: 44, 1, 0, 0, 31 25, where 0s represent the numbers on your ticket that you can't make out. If the six winning lottery numbers are 31, 10, 45, 1, 6, and 19, then the following shows the highest and the lowest prize you could have won.

Winning numbers3110451619Result
Highest prize 31 0→10 44 1 0→6 25 four matching numbers, wins the 3rd prize
Lowest prize 31 0→11 44 1 0→7 25 two matching numbers, wins the 5th prize
  • The order in which the numbers are drawn is irrelevant.
  • Had the numbers you can't make out been 10 and 6, you could have won the 3rd prize.
    • There are other possibilities where you could have won the 3rd prize. However, you would not have won a prize higher than the 3rd one.
  • Had the numbers you can't make out been 11 and 7, you could have won the 5th prize.
    • There are other possibilities where you could have won the 5th prize. However, you would not have won any prize (Tier 6).

Suppose parameters lottos and win_nums are given, where lottos is an array containing the numbers on your ticket and win_nums an array containing the winning numbers. Please write a function solution that returns the highest prize tier and the lowest prize tier that you could have won.

Constraints
  • lottos is an integer array whose length is 6.
  • The elements of lottos are integers between 0 and 45.
    • 0 represents the number you can't make out.
    • Other than 0s, no integers in lottos overlap.
    • The elements of lottos may not be arranged in a certain order.
  • win_nums is an integer array whose length is 6.
  • The elements of win_nums are integers between 1 and 45.
    • No integers in win_nums will overlap.
    • The elements of win_nums may not be arranged in a certain order.

https://school.programmers.co.kr/learn/courses/30/lessons/77484

๐Ÿงฉ Thought Process

  1. lottos์˜ ๋ชจ๋“  ๊ฐ’์„ for๋ฌธ์„ ๋Œ๋ ค ํ™•์ธ ํ–ˆ๋‹ค.
  2. 0์„ ์—˜๋ ˆ๋จผํŠธ๋กœ ๊ฐ€์ง„ ๊ฒฝ์šฐ์—๋Š” max_wins์— 1์„ ๋”ํ•ด ์ฃผ์—ˆ๋‹ค. ์ด ๊ฐ’์€ ์ถ”๊ฐ€์ ์œผ๋กœ ๋งž์ถœ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋งํ•œ๋‹ค.
  3. 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ๋กœ๋˜๋‹น์ฒจ ์ˆซ์ž์™€ ๋ชจ๋‘ ๋น„๊ต๋ฅผ ํ•ด์ฃผ๊ณ  ๊ฐ™์€ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด fixed_wins์— 1์„ ๋”ํ•ด์ค€๋‹ค.
  4. ๊ฐ™์€ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ์ตœ์†Œ ๊ฐœ์ˆ˜๋Š” fixed_wins์— ์ €์žฅ๋œ ์ˆ˜์ผ ๊ฒƒ์ด๊ณ  ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” fixed_wins+max_wins์ผ ๊ฒƒ์ด๋‹ค.
  5. ๋ฆฌํ„ด ํ•ด์•ผํ•˜๋Š” ๊ฐ’์€ ๋งž์ถ˜ ๋กœ๋˜ ๊ฐœ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋งž์ถ˜ ๋กœ๋˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ๋‹น์ฒจ์•ก ์ˆœ์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ ์—ˆ๋‹ค.

 

๐ŸŽ€ Answer

class Solution {
    fun solution(lottos: IntArray, win_nums: IntArray): IntArray {
        var fixed_wins:Int = 0
        var max_wins:Int = 0
        for (i in lottos.indices) {
            if (lottos[i] == 0) {
                max_wins += 1
            } else {
                for (j in win_nums.indices) {
                    if (lottos[i] == win_nums[j]) {
                        fixed_wins += 1
                    }
                }
            }
            
            
        }
        
        return intArrayOf(prizeTier(fixed_wins+max_wins), prizeTier(fixed_wins))
    }
    
    fun prizeTier(wins: Int):Int {
        return when(wins) {
            6 -> 1
            5 -> 2
            4 -> 3
            3 -> 4
            2 -> 5
            else -> 6
        }
    }
}

 

๐ŸŽ Result

๐Ÿ† Comment

indices๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด i๊ฐ€ ๊ทธ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์—˜๋ ˆ๋จผํŠธ ๊ฐ’์„ ๊ฐ€์งˆ ๊ฒƒ์ด๋ผ๊ณ  ์ฐฉ๊ฐ์„ ํ•ด์„œ ์ค‘๊ฐ„์— ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

array[i]๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•˜๊ณ  indices๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด i ๋Š” ์ธ๋ฑ์Šค ๋งŒ์„ ๋ฆฌํ„ดํ•œ ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์ฃผ์–ด์•ผ๊ฒ ๋‹ค.