728x90
https://school.programmers.co.kr/learn/courses/30/lessons/131128
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
처음에 소스코드1과같이 제출을 하였는데 시간복잡도 오류가 났다 이를 해결 하기위해
소스코드1(오류)
using System;
public class Solution
{
public string solution(string X, string Y)
{
string answer = "";
for (int i = 0; i < X.Length; i++)
{
for (int j = 0; j < Y.Length; j++)
{
if (X[i] == Y[j])
{
answer += Y[j];
Y = Y.Remove(j, 1);
break;
}
}
}
char[] temp = answer.ToCharArray();
Array.Sort(temp);
Array.Reverse(temp);
answer = new string(temp);
if (answer.Length == 0)
answer = "-1";
else if (answer[0] == '0')
answer = "0";
return answer;
}
}
소스코드2(개선)
using System;
using System.Linq;
public class Solution
{
public string solution(string X, string Y)
{
string answer = "";
int[] xArray = new int[10];
int[] yArray = new int[10];
for (int i = 0; i < X.Length; i++)
{
xArray[int.Parse(X[i].ToString())]++;
}
for (int i = 0; i < Y.Length; i++)
{
yArray[int.Parse(Y[i].ToString())]++;
}
for (int i = 9; i >= 0; i--)
{
if (xArray[i] == 0 || yArray[i] == 0)
continue;
int min = Math.Min(xArray[i], yArray[i]);
answer += string.Concat(Enumerable.Repeat(i, min));
}
if (answer.Length == 0)
answer = "-1";
else if (answer[0] == '0')
answer = "0";
return answer;
}
}728x90
'코딩공부 > 프로그래머스' 카테고리의 다른 글
| ※[프로그래머스]Lv.1 신고 결과 받기 C# (0) | 2023.06.15 |
|---|---|
| ※[프로그래머스]Lv.1 성격 유형 검사하기 C# (0) | 2023.06.14 |
| ※[프로그래머스]Lv.1 삼총사C# (나중에 DPS로 풀어보기) (0) | 2023.06.14 |
| [프로그래머스]Lv.1 콜라 문제 C# (0) | 2023.06.14 |
| [프로그래머스]Lv.1 옹알이 (2) C# (0) | 2023.06.14 |