코딩테스트_c#/프로그래머스
프로그래머스 ) 연습 문제 - 할인행사 Level.2_C#
노년인생
2024. 3. 8. 17:33
728x90
반응형
딕셔너리와 함수를 만들어서 푼 문제.
+4점
할인 행사 문제
내 코드
using System; // +4
using System.Collections.Generic;
public class Solution {
public int solution(string[] want, int[] number, string[] discount) {
int answer = 0;
Dictionary<string,int> map = new Dictionary<string, int>();
for (int i = 0; i < want.Length; i++)
{
map[want[i]] = number[i];
}
//10일연속으로 구매성공해야 되니까 할인되는 날짜에서 -9
for (int i = 0; i < discount.Length-9; i++)
{
answer += CheckStart(map, discount, i);
}
return answer;
}
//열흘간 원하는 제품 다 구매 가능한지 확인 함수
public int CheckStart(Dictionary<string, int> dic, string[] str, int index)
{
Dictionary<string, int> map = new Dictionary<string, int>(dic);
for (int i = index; i < index + 10; i++)
{
if (map.ContainsKey(str[i]))
{
map[str[i]]--;
}
else
return 0;
}
bool isZero = false; //원하는 재료 다 구매됬는지 체크 bool 값
foreach(int n in map.Values)
{
if(n == 0)
{
isZero = true;
}
else
{
isZero = false; //한개라도 재료 남아있으면 false
break;
}
}
if (isZero)
{
return 1;
}
else
return 0;
}
}
728x90
반응형