코딩테스트_c#/프로그래머스
프로그래머스 ) 롤케이크 자르기 Level.2_C#
노년인생
2024. 11. 27. 16:40
728x90
반응형
롤케이크 자르기 문제
내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int[] topping) {
int answer = 0;
Stack<int> stack = new Stack<int>(topping);
var dic = topping.GroupBy(x => x).ToDictionary(x=>x.Key,x=>x.Count());
HashSet<int> hashSet = new HashSet<int>();
while(stack.Count > 0)
{
int n = stack.Pop();
hashSet.Add(n);
if (dic[n] == 1)
dic.Remove(n);
else
dic[n]--;
if(dic.Count() == hashSet.Count)
answer++;
}
return answer;
}
}
728x90
반응형