https://school.programmers.co.kr/learn/courses/30/lessons/131127
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int solution(string[] want, int[] number, string[] discount)
{
int answer = 0;
int n = discount.Length;
int day = 10;
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < want.Length; i++)
{
dic.Add(want[i], number[i]);
}
for(int i = 0; i <= n - day; i++)
{
Dictionary<string, int> copyDic = new Dictionary<string, int>(dic);
for(int j = i; j < i + day; j++)
{
if (copyDic.ContainsKey(discount[j]))
{
copyDic[discount[j]]--;
if (copyDic[discount[j]] == 0)
copyDic.Remove(discount[j]);
}
}
if(copyDic.Count == 0)
answer++;
}
return answer;
}
public static void Main(string[] args)
{
Solution s = new Solution();
string[] want = {"banana", "apple", "rice", "pork", "pot"};
int[] number = { 3, 2, 2, 2, 1 };
string[] discount = { "chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana" };
s.solution(want, number, discount);
Console.WriteLine(s.solution(want, number, discount));
}
}
n - day를 하는이유는 할인날짜와 원하는 날짜가 필요한 날의 경우의 수만 추출하기 위한 계산이다.
그렇게 하여 해당 날짜부터 반복하여 원하는 물품이 다 있는지 dictionary로 계산하는 식이다.
예를 들어, 10일이고 할인날이 총 14일이면 0 ~ 9, 1 ~ 10, 2 ~ 11 일 ... 4 ~ 13일 이런식으로 계산되는 것이다.
'백준 등 알고리즘' 카테고리의 다른 글
[C#] 프로그래머스 LV.2 k진수에서 소수 개수 구하기 (0) | 2024.12.04 |
---|---|
[C#] 프로그래머스 LV.2 게임 맵 최단거리 (1) | 2024.11.27 |
[C#] 프로그래머스 LV.2 타겟 넘버 (DFS) (0) | 2024.11.23 |
[C#] 프로그래머스 Lv.2 프로세스 (Queue 튜플) (0) | 2024.11.22 |
[C#] 프로그래머스 Lv.2 귤 고르기 (0) | 2024.11.21 |