코딩공부/프로그래머스

※[프로그래머스]Lv.2 피로도 C#(순열)

usingsystem 2023. 7. 20. 09:36
728x90

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

 

프로그래머스

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

programmers.co.kr

소스코드

미뤄두고 있던 순열을 공부하기에 좋았던 문제였다.

 

using System;
using System.Collections.Generic;
public class Solution
{
    List<int[]> list = new List<int[]>();
    public int solution(int k, int[,] dungeons)
    {
        int answer = -1;
        int size = dungeons.GetLength(0);
        int[] array = new int[size];

        for (int i = 0; i < size; i++)
            array[i] = i;

        Perm(array, 0, size);

        for (int i = 0; i < list.Count; i++)
        {
            int kTemp = k;
            int count = 0;
            int[] permArr = list[i];

            for (int j = 0; j < permArr.Length; j++)
            {
                if (kTemp < dungeons[permArr[j], 0])
                    break;

                kTemp -= dungeons[permArr[j], 1];
                count++;
            }

            if (answer < count)
                answer = count;
        }

        return answer;
    }

    void Perm(int[] arr, int depth, int k)
    {
        if (depth == k)
        {
            list.Add(arr.Clone() as int[]);
        }
        else
        {
            for (int i = depth; i < k; i++)
            {
                int temp = arr[depth];
                arr[depth] = arr[i];
                arr[i] = temp;

                Perm(arr, depth + 1, k);

                temp = arr[depth];
                arr[depth] = arr[i];
                arr[i] = temp;
            }
        }
    }
}
728x90