프로그래머스 | K번째수 (정렬)

Algorithm 2019. 8. 23. 12:18
반응형
  • K번째수
  • darklight

    sublimevimemacs

    C# 

문제 설명

배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.

예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면

  1. array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
  2. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
  3. 2에서 나온 배열의 3번째 숫자는 5입니다.

배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.

제한사항

  • array의 길이는 1 이상 100 이하입니다.
  • array의 각 원소는 1 이상 100 이하입니다.
  • commands의 길이는 1 이상 50 이하입니다.
  • commands의 각 원소는 길이가 3입니다.

입출력 예

arraycommandsreturn

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

입출력 예 설명

[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.

 

 

 

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace _07
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new Solution();
            int[] arr = {1,5,2,6,3,7,4};
            int[,] commands = {{2,5,3},{4,4,1},{1,7,3}};
            var result = s.solution(arr, commands);
            for(int i = 0; i<result.Length; i++){
                Console.Write(result[i] + " ");
            }
        }
    }
 
    public class Solution {
        const int MAX_COMMNANS_LENGTH = 50;
        const int MAX_ELEMENT_OF_COMMANDS = 3;
 
        public int[] solution(int[] array, int[,] commands) {
 
            // int[] answer = new int[] {};
            var answerList = new List<int>();
            int totalCommandsLength = commands.Length/MAX_ELEMENT_OF_COMMANDS;
 
            // Console.WriteLine("totalCommandsLength: {0}", totalCommandsLength);
 
            // Console.WriteLine(commands.Length + " , " + commands.Length/MAX_ELEMENT_OF_COMMANDS);
            for(int i = 0; i<totalCommandsLength; i++){
                int[] arrCommand = new int[MAX_ELEMENT_OF_COMMANDS];
                for(int j = 0; j<MAX_ELEMENT_OF_COMMANDS; j++){
                    // Console.Write(commands[i, j] + " ");
                    arrCommand[j] = commands[i, j];
                }
 
                var result = this.SearchValueByIndex(array, arrCommand);
                // Console.WriteLine("result: {0}", result);
                answerList.Add(result);
            }
            
            return answerList.ToArray();
        }
 
        public int SearchValueByIndex(int[] array, int[] commands){
            int startIdx = 0;
            int endIdx = 0;
            int searchIdx = 0;
 
            for(int i = 0; i<3; i++){
                if(i == 0) startIdx = commands[i] - 1;
                else if(i == 1) endIdx = commands[i];
                else if(i == 2) searchIdx = commands[i] - 1;
                //Console.Write(commands[i] + " ");
            }
 
            //1,5,2,6,3,7,4
            // Console.WriteLine("startIdx: {0}, endIdx: {1}, searchIdx: {2}", startIdx, endIdx, searchIdx);
 
            var list = new List<int>();
            for(int i = startIdx; i<endIdx; i++){
                //Console.Write(array[i] + " ");
                list.Add(array[i]);
            }
            list.Sort();
            // foreach(var n in list){
            //     Console.Write(n + " ");
            // }
            // Console.WriteLine();
 
            var foundVal = list[searchIdx];
            // Console.Write("foundVal: {0}", foundVal);
 
            return foundVal;
        }
    }
}
 
 
 

 

 

 

다른사람풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        int[] answer = new int[commands.GetLength(0)];
        List<int> lst = new List<int>(array);
        for (int i = 0; i < commands.GetLength(0); i++)
        {
            int nStart = commands[i, 0];
            int nEnd = commands[i, 1];
            int nIndex = commands[i, 2];
            List<int> lstSub = lst.Where((x, idx) => idx >= nStart - 1 && idx < nEnd).OrderBy(x => x).ToList();
            answer[i] = lstSub[nIndex - 1];
        }
        return answer;
    }
}
 
 
반응형
: