티스토리 뷰


title: "K번째수"
category: 프로그래머스[Level-1]
tags: [C++, JavaScript, 프로그래머스]
date: "2021-01-19"


문제 링크

K번째 수

C++

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;

    for(auto item: commands){
        vector<int> _vec;
        for(int i=item[0]-1; i<item[1]; i++){
            _vec.push_back(array[i]);       // 새로운 배열
        }
        stable_sort(_vec.begin(), _vec.end());  // 오름차순 정렬
        answer.push_back(_vec[item[2]-1]);      // 숫자 추출
    }

    return answer;
}

JavaScript

function solution(array, commands) {
  var answer = [];

  commands.forEach((item) => {
    let newArray = array.slice(item[0] - 1, item[1]); // 부분 배열
    newArray.sort((a, b) => {
      // 오름차순 정렬
      return a < b ? -1 : a === b ? 0 : 1;
    });
    console.log(newArray);
    answer.push(newArray[item[2] - 1]); // 결과 담기
  });

  return answer;
}
728x90
반응형
댓글
05-21 15:56
링크