로또의 최고 순위와 최저 순위

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

처음 풀었을 때는 0이 되는 경우와 아닌 경우를 나눠서 생각하지 못해 틀렸었는데

다시 문제를 보니 0아니 아니면 확정적으로 맞은 개수가 나오고 나머지 0에서 최고 최저를 가린다는 것을 알게 된 뒤 수월하게 풀 수 있었다. 특히 나는 배열로 맞춘 개수를 미리 넣어놓은 게 도움이 되었다.

using System;

public class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
            int correct = 0;
            int zeroCount = 0;

            int[] ints = new int[7] { 6, 5, 4, 3, 2, 1, 0 };

            for (int i = 0; i < lottos.Length; i++)
            {
                if (Array.Exists(win_nums, x => x == lottos[i]))
                {
                    correct++;
                }

                if (lottos[i] == 0)
                {
                    zeroCount++;
                }
            }

            // 최대 correct + zeroCount
            // 최소 correct

            int max = correct + zeroCount;
            int min = correct;

            if (max < 2)
            {
                return new int[] { 6, 6 };
            }
            else
            {
                if (min < 2)
                {
                    int maxIndex = Array.IndexOf(ints, max);
                    return new int[] { maxIndex + 1, 6 };
                }
                else
                {
                    int maxIndex = Array.IndexOf(ints, max);
                    int minIndex = Array.IndexOf(ints, min);
                    return new int[] { maxIndex + 1, minIndex + 1 };
                }
            }
    }
}

+ Recent posts