Programmer:)
[ 프로그래머스 / C# ] 숫자 카드 나누기 본문
반응형
문제 :
https://school.programmers.co.kr/learn/courses/30/lessons/135807
내가 제출한 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int[] arrayA, int[] arrayB) {
int answer = 0;
var a = from f in arrayA.Distinct()
orderby f ascending
select f;
var b = from f in arrayB.Distinct()
orderby f ascending
select f;
List<int> A = a.ToList();
List<int> B = b.ToList();
int abExceptCount = A.Intersect(B).Count();
if (abExceptCount > 0)
return 0;
int a_answer = Processes(A, B);
int b_answer = Processes(B, A);
answer = a_answer <= b_answer ? b_answer : a_answer;
return answer;
}
private int Processes(List<int> A, List<int> B)
{
int mainNum = A[0];
List<int> a0divisorList = new List<int>();
List<int> divisorSelectList = new List<int>();
int result = 0;
for (int i = 1; i < mainNum+1; i++)
{
if (mainNum % i == 0)
a0divisorList.Add(i);
}
for (int i = 0; i < a0divisorList.Count; i++)
{
int divisor = a0divisorList[i];
for (int j = 0; j < A.Count; j++)
{
if (A[j] % divisor != 0)
break;
if (j == A.Count - 1)
divisorSelectList.Add(divisor);
}
}
divisorSelectList.Sort();
for (int i = divisorSelectList.Count - 1; i >= 0; i--)
{
int divisor = divisorSelectList[i];
for (int j = 0; j < B.Count; j++)
{
if (B[j] % divisor == 0)
break;
if (j == B.Count-1)
{
result = divisor;
return result;
}
}
}
return result;
}
}
테스트케이스 30,31번이 계속 실패서 좀 해맸다... (배열값이 1개일 경우에대한 고려를 하지 않아서 실패함)
좀 더 최적화 할 수 있을 듯.
다음 문제 부시러 갑니당
반응형
'프로그래머스 정복 > C#' 카테고리의 다른 글
[ 프로그래머스 / C# ] 옹알이 (1) (0) | 2022.11.24 |
---|---|
[ 프로그래머스 / C# ] 혼자놀기의 달인 (0) | 2022.11.18 |
[ 프로그래머스 / C# ] 과일 장수 (0) | 2022.11.16 |
[ 프로그래머스 / C# ] K번째 수 (0) | 2022.03.29 |
[ 프로그래머스 / C# ] 음양 더하기 (0) | 2021.10.25 |
Comments