코딩공부/프로그래머스

※[프로그래머스]Lv.2 숫자 변환하기 C# (Hashset)

usingsystem 2023. 7. 7. 12:55
728x90

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

 

프로그래머스

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

programmers.co.kr

소스코드

using System;
using System.Collections.Generic;
public class Solution
{
    public int solution(int x, int y, int n)
    {
        int answer = 0;

        List<int> targets = new List<int>
            {
                x
            };

        List<int> results = new List<int>();
        HashSet<int> sames = new HashSet<int>();

        while (targets.Count > 0)
        {
            if (targets.Contains(y))
                return answer;

            answer++;
            results.Clear();

            foreach (var item in targets)
            {
                sames.Add(item);
                int a = item + n;
                int b = item * 2;
                int c = item * 3;

                if (a <= y && sames.Contains(a) == false)
                    results.Add(a);

                if (b <= y && sames.Contains(b) == false)
                    results.Add(b);

                if (c <= y && sames.Contains(c) == false)
                    results.Add(c);
            }

            List<int> temp = targets;
            targets = results;
            results = temp;
        }

        return -1;
    }
}
728x90