코딩공부/프로그래머스
※[프로그래머스]Lv.1 모의고사 C# (나머지 값 활용)
usingsystem
2023. 6. 15. 21:50
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
소스코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] answers) {
int[] first = new int[] { 1, 2, 3, 4, 5 };
int[] second = new int[] { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] third = new int[] { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
Dictionary<int, int> counts = new Dictionary<int, int>
{
{ 1, 0 },
{ 2, 0 },
{ 3, 0 }
};
for (int i = 0; i < answers.Length; i++)
{
counts[1] += first[i % first.Length] == answers[i] ? 1 : 0;
counts[2] += second[i % second.Length] == answers[i] ? 1 : 0;
counts[3] += third[i % third.Length] == answers[i] ? 1 : 0;
}
int max = counts.Select(x => x.Value).Max();
var answer = counts.Where(w => w.Value == max).Select(s => s.Key).ToArray();
return answer;
}
}
728x90