문제 풀기/C#
89. 할인 행사
kagan-draca
2025. 3. 11. 17:32

기본 틀 :
using System;
public class Solution {
public int solution(string[] want, int[] number, string[] discount) {
int answer = 0;
return answer;
}
}
이 문제는 want에 대한 dictionary와 10일 기준 discount에 대한 dictionary를 만들어
두 dictionary를 비교하면 되는 문제다.
이때, 입출력 예 2번과 같이
want나 discount에 대한 dictionary에 해당 key 값이 없어 오류가 발생할 수 있다.
그래서, if문에 !dictionary.ContainsKey() 를 사용해서 오류가 발생하지 않도록 해주면 된다.
using System;
using System.Linq;
using System.Collections.Generic;
public class Solution {
public int solution(string[] want, int[] number, string[] discount) {
int answer = 0;
Dictionary<string,int> dict = new Dictionary<string,int>();
for(int i = 0; i < want.Length; i++)
dict[want[i]] = number[i];
for(int i = 0; i <= discount.Length - 10; i++)
{
Dictionary<string, int> totalSale = new Dictionary<string, int>();
for(int j = i; j < i + 10; j++)
{
if(!totalSale.ContainsKey(discount[j])) totalSale[discount[j]] = 1;
else totalSale[discount[j]]++;
}
bool check = true;
for(int j = 0; j < want.Length; j++)
{
if(!totalSale.ContainsKey(want[j]) || dict[want[j]] != totalSale[want[j]])
{
check = false;
break;
}
}
if(check) answer++;
}
return answer;
}
}
