Jun 개발노트

프로그래머스 - K번째수

January 10, 2020

K번째 수

const sliceArr = (arr, comm) => {
    const targetArr = arr.slice(comm[0] - 1, comm[1]);
    return targetArr.sort((a, b) => {
        return a - b;
    })[comm[2] - 1];
};

const findNum = (arr, command) => {
    const commandArr = command;
    const answer = [];
    for (let comm of commandArr) {
        answer.push(sliceArr(arr, comm));
    }
    return answer;
};

test("k번째 수 구하기", () => {
    expect(
        findNum(
            [1, 5, 2, 6, 3, 7, 4],
            [
                [2, 5, 3],
                [4, 4, 1],
                [1, 7, 3]
            ]
        )
    ).toEqual([5, 6, 3]);
});

Written by Junho You 배운것을 기록하자