코딩공부/프로그래머스

[프로그래머스]Lv.1 로또의 최고 순위와 최저 순위 C#

usingsystem 2023. 6. 15. 15:30
728x90

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

 

프로그래머스

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

programmers.co.kr

소스코드

using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
    public int[] solution(int[] lottos, int[] win_nums)
    {
        Dictionary<int, int> rank = new Dictionary<int, int>()
            {
                { 6, 1 }, { 5, 2 },
                { 4, 3 }, { 3, 4 },
                { 2, 5 }, { 1, 6 },
                { 0, 6 },
            };

        Dictionary<int, int> sameNumber = lottos.Where(w => w != 0).ToDictionary((data => data), (data => 0));

        for (int i = 0; i < win_nums.Length; i++)
        {
            int num = 0;
            if (sameNumber.TryGetValue(win_nums[i], out num))
                sameNumber[win_nums[i]]++;
        }

        int sameCount = sameNumber.Where(x => x.Value > 0).Count();
        int zeroCount = 6 - sameNumber.Count();

        int max = sameCount + zeroCount;
        int min = sameCount;

        return new int[] { rank[max], rank[min] };
    }
}
728x90