코딩공부/프로그래머스

※[프로그래머스]Lv.2 롤케이크 자르기C# (순차적으로 줄여나가기)

usingsystem 2023. 7. 7. 16:50
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/132265

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

소스코드

using System;
using System.Collections.Generic;

public class Solution
{
    public int solution(int[] topping)
    {
        int answer = 0;

        Dictionary<int, int> dicL = new Dictionary<int, int>();
        Dictionary<int, int> dicR = new Dictionary<int, int>();

        foreach (int i in topping)
        {
            if (dicR.TryAdd(i, 1) == false)
                dicR[i] += 1;
        }

        foreach (var key in topping)
        {
            if (dicL.ContainsKey(key) == false)
                dicL.Add(key, 0);

            dicL[key] += 1;
            dicR[key] -= 1;

            if (dicR[key] == 0)
                dicR.Remove(key);

            if (dicL.Count == dicR.Count)
                answer++;

        }
        return answer;
    }
}
728x90