Skip to content

Commit

Permalink
#12 22.06.09 > k번째 수
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Jun 8, 2022
1 parent c3c2601 commit 2d12a69
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/programmers/Lv_1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
|14|[없는 숫자 더하기](./accNoNumbers.js)|22.06.07||다시 풀기|
|15|[크레인 인형뽑기 게임](./pickdolls.js)|22.06.08||다시 풀기|
|16|[음양 더하기](./accPlusMinus.js)|22.06.08|O|X|
|17|[가운데 글자 가져오기](./bringMid.js)|22.06.08|O|X|
|17|[가운데 글자 가져오기](./bringMid.js)|22.06.08|O|X|
|18|[k번째 수](./kNumber.js)|22.06.09|O|X|
37 changes: 37 additions & 0 deletions src/programmers/Lv_1/kNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 내 풀이
function solution(array, commands) {
let answer = [];
for(let x = 0; x < commands.length; x++) {
let i = commands[x][0];
let j = commands[x][1];
let k = commands[x][2];

let cutArray = array.slice(i-1, j).sort((a,b) => a-b);
let value = cutArray.slice(k-1, k);
answer.push(...value);
}

return answer;
}

/*
1) i, j, k 구하기
2) slice로 배열 자르기
3) sort하기
4) slice로 k 구하기
5) answer 배열에 push하기
*/


// 다른 풀이 - 구조분해 할당 이용하기
function solution(array, commands) {
return commands.map(command => {
const [sPosition, ePosition, position] = command
const newArray = array
.filter((value, fIndex) => fIndex >= sPosition - 1 && fIndex <= ePosition - 1)
.sort((a,b) => a - b)

return newArray[position - 1]
})
}

0 comments on commit 2d12a69

Please sign in to comment.