//구글콘솔 광고 추가가
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
반응형

+ Recent posts