로또의 최고 순위와 최저 순위
https://school.programmers.co.kr/learn/courses/30/lessons/77484
처음 풀었을 때는 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 };
}
}
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 - 옹알이 (2) (0) | 2023.09.06 |
---|---|
프로그래머스 - 숫자 짝궁 (0) | 2023.09.06 |
프로그래머스 - 시저 암호 (0) | 2023.08.30 |
프로그래머스 - 최대공약수와 최소공배수 (0) | 2023.08.28 |
프로그래머스 - 정수 제곱근 판별하기 (0) | 2023.08.25 |