Programmer:)

[ 프로그래머스 / C# ] K번째 수 본문

프로그래머스 정복/C#

[ 프로그래머스 / C# ] K번째 수

ryeggg 2022. 3. 29. 21:32
반응형

 

문제

https://programmers.co.kr/learn/courses/30/lessons/42748

 

코딩테스트 연습 - K번째수

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr


내가 제출한 코드

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] array, int[,] commands)
    {
        List<int> temp = new List<int>();
        List<int> answer = new List<int>();

        for (int i = 0; i < commands.GetLongLength(0); i++)
        {
            int min = commands[i, 0];
            int max = commands[i, 1];
            int index = commands[i,2];

            for (int j = min-1; j < max; j++)
            {
                temp.Add(array[j]);
            }

            temp.Sort();
            answer.Add(temp[index-1]);

            temp.Clear();
        }

        return answer.ToArray();
    }
}

 

 

반응형
Comments