코딩공부/프로그래머스
[프로그래머스]Lv.2 할인 행사 C#
usingsystem
2023. 7. 12. 13:36
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/131127
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
소스코드
using System;
using System.Collections.Generic;
public class Solution
{
public int solution(string[] want, int[] number, string[] discount)
{
int answer = 0;
Dictionary<string, int> dicWant = new Dictionary<string, int>();
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < want.Length; i++)
dicWant.TryAdd(want[i], number[i]);
for (int i = 0; i < discount.Length; i++)
dic.TryAdd(discount[i], 0);
foreach (var item in dicWant)
{
if (dic.ContainsKey(item.Key) == false)
return 0;
}
for (int i = 0; i < discount.Length; i++)
{
string key = discount[i];
if (i > 9)
{
string rowKey = discount[i - 10];
dic[rowKey] -= 1;
}
dic[key] += 1;
bool isEquals = true;
foreach (var item in dicWant)
{
if (item.Value != dic[item.Key])
{
isEquals = false;
break;
}
}
if (isEquals)
answer++;
}
return answer;
}
}
728x90